Skip to content

Commit

Permalink
v0.0.2 comment
Browse files Browse the repository at this point in the history
  • Loading branch information
李文龙 committed Mar 7, 2023
1 parent cd11b12 commit c903d0a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
20 changes: 20 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

// GetArrayIndex get value index with slice, if not found return -1
// 获取数据下标,不存在返回-1
func GetArrayIndex[T comparable](params []T, value T) int {
for index, param := range params {
if param == value {
Expand All @@ -9,6 +11,8 @@ func GetArrayIndex[T comparable](params []T, value T) int {
return -1
}

// InArray
// 判断数据是否存在 compare
func InArray[T comparable](params []T, value T) bool {
for _, param := range params {
if param == value {
Expand All @@ -18,6 +22,8 @@ func InArray[T comparable](params []T, value T) bool {
return false
}

// InArrayCompare
// 判断数据是否存在 ICompare
func InArrayCompare[T ICompare](params []T, value T) bool {
for _, param := range params {
if param.Compare(value) {
Expand All @@ -27,6 +33,8 @@ func InArrayCompare[T ICompare](params []T, value T) bool {
return false
}

// ArrayMap
// 返回输入数组中某个单一列的值的map string interface
func ArrayMap(params []map[string]interface{}, key string) map[string]map[string]interface{} {
if len(params) < 1 {
return nil
Expand All @@ -42,6 +50,8 @@ func ArrayMap(params []map[string]interface{}, key string) map[string]map[string
return res
}

// ArrayMapCompare
// 返回输入数组中某个单一列的值的map interface
func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[T]interface{} {
if len(params) < 1 {
return nil
Expand All @@ -57,6 +67,8 @@ func ArrayMapCompare[T comparable](params []map[T]interface{}, key T) map[T]map[
return res
}

// ArrayMapCompareValue
// 返回输入数组中某个单一列的值的map
func ArrayMapCompareValue[K comparable, V any](params []map[K]V, key K) map[K]map[K]V {
if len(params) < 1 {
return nil
Expand All @@ -72,6 +84,8 @@ func ArrayMapCompareValue[K comparable, V any](params []map[K]V, key K) map[K]ma
return res
}

// ArrayColumns
// 返回输入数组中某个单一列的值
func ArrayColumns[K comparable, V any](params []map[K]V, key K) []V {
if len(params) < 1 {
return nil
Expand All @@ -88,6 +102,8 @@ func ArrayColumns[K comparable, V any](params []map[K]V, key K) []V {
return res
}

// ArrayColumnValues array convert to slice
// 将数组合并到一个map
func ArrayColumnValues[K comparable, V any](params []map[K]interface{}, key, VKey K) map[K]V {
if len(params) < 1 {
return nil
Expand All @@ -111,6 +127,8 @@ func ArrayColumnValues[K comparable, V any](params []map[K]interface{}, key, VKe
return res
}

// ArrayUnique unique with slice
// 对切片进行去重
func ArrayUnique[V comparable](params []V) []V {
mp := map[V]struct{}{}
res := make([]V, len(params))
Expand All @@ -126,6 +144,8 @@ func ArrayUnique[V comparable](params []V) []V {
return res[:index]
}

// ArrayDiff diff with slice values
// 取多个切片的差集
func ArrayDiff[V comparable](params ...[]V) []V {
var all []V
mp := map[V]int8{}
Expand Down
1 change: 1 addition & 0 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestArrayMapCompare(t *testing.T) {
func TestArrayMapCompareValue(t *testing.T) {
fmt.Println(ArrayMapCompareValue[string, ICompare]([]map[string]ICompare{
{"a": cp{value: "dasdas"}},
{"b": cp{value: "dasdas"}},
}, "a"))
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module item

go 1.19
go 1.18
11 changes: 6 additions & 5 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package item

import "reflect"

func GetItemMap[K comparable,V any,Item any](values []Item, fieldK, fieldV string) map[K]V {
// GetItemMap get struct item with fieldK
func GetItemMap[K comparable, V any, Item any](values []Item, fieldK, fieldV string) map[K]V {
if len(values) == 0 {
return nil
}

res := make(map[K]V)
for _,v := range values {
for _, v := range values {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Pointer{
if rv.Kind() == reflect.Pointer {
if rv.IsNil() {
continue
}
rv = rv.Elem()
}

rk := rv.FieldByName(fieldK)
k,ok := rk.Interface().(K)
k, ok := rk.Interface().(K)
if !ok {
continue
}

rv1 := rv.FieldByName(fieldV)
value,ok := rv1.Interface().(V)
value, ok := rv1.Interface().(V)
if !ok {
continue
}
Expand Down

0 comments on commit c903d0a

Please sign in to comment.