feat(Go-Tool):2020/12/14:添加validator.v8源码解析和使用示例

This commit is contained in:
Huangzj
2020-12-14 15:15:58 +08:00
parent 23d7d6e226
commit fa31b23cca
51 changed files with 4619 additions and 1662 deletions
+7 -52
View File
@@ -1,7 +1,6 @@
package linq
// Take returns a specified number of contiguous elements from the start of a
// collection.
// Take returns a specified number of contiguous elements from the start of a collection.
func (q Query) Take(count int) Query {
return Query{
Iterate: func() Iterator {
@@ -20,8 +19,8 @@ func (q Query) Take(count int) Query {
}
}
// TakeWhile returns elements from a collection as long as a specified condition
// is true, and then skips the remaining elements.
// TakeWhile returns elements from a collection as long as a specified condition is true,
// and then skips the remaining elements.
func (q Query) TakeWhile(predicate func(interface{}) bool) Query {
return Query{
Iterate: func() Iterator {
@@ -50,33 +49,10 @@ func (q Query) TakeWhile(predicate func(interface{}) bool) Query {
}
}
// TakeWhileT is the typed version of TakeWhile.
//
// - predicateFn is of type "func(TSource)bool"
//
// NOTE: TakeWhile has better performance than TakeWhileT.
func (q Query) TakeWhileT(predicateFn interface{}) Query {
predicateGenericFunc, err := newGenericFunc(
"TakeWhileT", "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.TakeWhile(predicateFunc)
}
// TakeWhileIndexed returns elements from a collection as long as a specified
// condition is true. The element's index is used in the logic of the predicate
// function. The first argument of predicate represents the zero-based index of
// the element within collection. The second argument represents the element to
// test.
// TakeWhileIndexed returns elements from a collection as long as a specified condition
// is true. The element's index is used in the logic of the predicate function.
// The first argument of predicate represents the zero-based index of the element
// within collection. The second argument represents the element to test.
func (q Query) TakeWhileIndexed(predicate func(int, interface{}) bool) Query {
return Query{
Iterate: func() Iterator {
@@ -106,24 +82,3 @@ func (q Query) TakeWhileIndexed(predicate func(int, interface{}) bool) Query {
},
}
}
// TakeWhileIndexedT is the typed version of TakeWhileIndexed.
//
// - predicateFn is of type "func(int,TSource)bool"
//
// NOTE: TakeWhileIndexed has better performance than TakeWhileIndexedT.
func (q Query) TakeWhileIndexedT(predicateFn interface{}) Query {
whereFunc, err := newGenericFunc(
"TakeWhileIndexedT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(index int, item interface{}) bool {
return whereFunc.Call(index, item).(bool)
}
return q.TakeWhileIndexed(predicateFunc)
}