1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
dudaodong ca373b00a7 feat: add GetOrSet 2024-06-24 17:29:12 +08:00
dudaodong a220220f09 feat: add GetOrSet 2024-06-24 17:28:51 +08:00
dudaodong aeef0418a4 fix: fix bug of CopyDir 2024-06-24 17:09:31 +08:00
6 changed files with 142 additions and 35 deletions
+40
View File
@@ -55,6 +55,8 @@ import (
- [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete) - [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete)
- [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range) - [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1483,3 +1485,41 @@ func main() {
}) })
} }
``` ```
### <span id="GetOrSet">GetOrSet</span>
<p>返回给定键的值,如果不存在则设置该值。</p>
<b>函数签名:</b>
```go
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
```
<b>示例:<span style="float:right;display:inline-block;"></span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
}
result1 := maputil.GetOrSet(m, 1, "1")
result2 := maputil.GetOrSet(m, 2, "b")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// a
// b
}
```
+38
View File
@@ -55,6 +55,7 @@ import (
- [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete) - [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete)
- [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range) - [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1501,3 +1502,40 @@ func main() {
}) })
} }
``` ```
### <span id="GetOrSet">GetOrSet</span>
<p>Returns value of the given key or set the given value value if not present.</p>
<b>Signature:</b>
```go
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
```
<b>Example:<span style="float:right;display:inline-block;"></span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
}
result1 := maputil.GetOrSet(m, 1, "1")
result2 := maputil.GetOrSet(m, 2, "b")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// a
// b
}
```
+20 -35
View File
@@ -119,58 +119,43 @@ func CreateDir(absPath string) error {
// if dstPath exists, it will return an error. // if dstPath exists, it will return an error.
// Play: https://go.dev/play/p/YAyFTA_UuPb // Play: https://go.dev/play/p/YAyFTA_UuPb
func CopyDir(srcPath string, dstPath string) error { func CopyDir(srcPath string, dstPath string) error {
if !IsDir(srcPath) { srcInfo, err := os.Stat(srcPath)
return errors.New("source path is not a directory")
}
var err error
srcPath, err = filepath.Abs(srcPath)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get source directory info: %w", err)
}
if IsExist(dstPath) {
return errors.New("destination path already exists")
}
dstPath, err = filepath.Abs(dstPath)
if err != nil {
return err
} }
// get srcPath's file info if !srcInfo.IsDir() {
srcFileInfo, err := os.Stat(srcPath) return fmt.Errorf("source path is not a directory: %s", srcPath)
if err != nil {
return err
} }
// create dstPath with srcPath's mode err = os.MkdirAll(dstPath, 0755)
err = os.MkdirAll(dstPath, srcFileInfo.Mode())
if err != nil { if err != nil {
return err return fmt.Errorf("failed to create destination directory: %w", err)
} }
err = filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error { entries, err := os.ReadDir(srcPath)
if srcPath == path { if err != nil {
return nil return fmt.Errorf("failed to read source directory: %w", err)
} }
curDstPath := filepath.Join(dstPath, filepath.Base(path))
if info.IsDir() { for _, entry := range entries {
err = CopyDir(path, curDstPath) srcDir := filepath.Join(srcPath, entry.Name())
dstDir := filepath.Join(dstPath, entry.Name())
if entry.IsDir() {
err := CopyDir(srcDir, dstDir)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
err = CopyFile(path, curDstPath) err := CopyFile(srcDir, dstDir)
if err != nil {
return err
}
err = os.Chmod(curDstPath, info.Mode())
if err != nil { if err != nil {
return err return err
} }
} }
return err }
})
return err return nil
} }
// IsDir checks if the path is directory or not. // IsDir checks if the path is directory or not.
+12
View File
@@ -436,3 +436,15 @@ func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator fun
return keys, sortedValues return keys, sortedValues
} }
// GetOrSet returns value of the given key or set the given value value if not present.
// Play: todo
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
if v, ok := m[key]; ok {
return v
}
m[key] = value
return value
}
+16
View File
@@ -524,3 +524,19 @@ func ExampleToSortedSlicesWithComparator() {
// [3 2 1] // [3 2 1]
// [c b a] // [c b a]
} }
func ExampleGetOrSet() {
m := map[int]string{
1: "a",
}
result1 := GetOrSet(m, 1, "1")
result2 := GetOrSet(m, 2, "b")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// a
// b
}
+16
View File
@@ -691,3 +691,19 @@ func TestToSortedSlicesWithComparator(t *testing.T) {
}) })
} }
} }
func TestGetOrSet(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestGetOrSet")
m := map[int]string{
1: "a",
}
result1 := GetOrSet(m, 1, "1")
result2 := GetOrSet(m, 2, "b")
assert.Equal("a", result1)
assert.Equal("b", result2)
}