feat(Go-Tool):

2020/12/16: 添加YAML和结构体转换工具
This commit is contained in:
Huangzj
2020-12-16 14:23:38 +08:00
parent 61966551cd
commit dc80b557e3
32 changed files with 1700 additions and 324 deletions
+47 -4
View File
@@ -19,11 +19,33 @@ func (q Query) Where(predicate func(interface{}) bool) Query {
}
}
// WhereIndexed filters a collection of values based on a predicate.
// Each element's index is used in the logic of the predicate function.
// WhereT is the typed version of Where.
//
// The first argument represents the zero-based index of the element within collection.
// The second argument of predicate represents the element to test.
// - predicateFn is of type "func(TSource)bool"
//
// NOTE: Where has better performance than WhereT.
func (q Query) WhereT(predicateFn interface{}) Query {
predicateGenericFunc, err := newGenericFunc(
"WhereT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.Where(predicateFunc)
}
// WhereIndexed filters a collection of values based on a predicate. Each
// element's index is used in the logic of the predicate function.
//
// The first argument represents the zero-based index of the element within
// collection. The second argument of predicate represents the element to test.
func (q Query) WhereIndexed(predicate func(int, interface{}) bool) Query {
return Query{
Iterate: func() Iterator {
@@ -44,3 +66,24 @@ func (q Query) WhereIndexed(predicate func(int, interface{}) bool) Query {
},
}
}
// WhereIndexedT is the typed version of WhereIndexed.
//
// - predicateFn is of type "func(int,TSource)bool"
//
// NOTE: WhereIndexed has better performance than WhereIndexedT.
func (q Query) WhereIndexedT(predicateFn interface{}) Query {
predicateGenericFunc, err := newGenericFunc(
"WhereIndexedT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(index int, item interface{}) bool {
return predicateGenericFunc.Call(index, item).(bool)
}
return q.WhereIndexed(predicateFunc)
}