feat(Go-Tool):
2020/12/16: 添加YAML和结构体转换工具
This commit is contained in:
+121
-20
@@ -8,18 +8,17 @@ type order struct {
|
||||
desc bool
|
||||
}
|
||||
|
||||
// OrderedQuery is the type returned from OrderBy, OrderByDescending
|
||||
// ThenBy and ThenByDescending functions.
|
||||
// OrderedQuery is the type returned from OrderBy, OrderByDescending ThenBy and
|
||||
// ThenByDescending functions.
|
||||
type OrderedQuery struct {
|
||||
Query
|
||||
original Query
|
||||
orders []order
|
||||
}
|
||||
|
||||
// OrderBy sorts the elements of a collection in ascending order.
|
||||
// Elements are sorted according to a key.
|
||||
func (q Query) OrderBy(
|
||||
selector func(interface{}) interface{}) OrderedQuery {
|
||||
// OrderBy sorts the elements of a collection in ascending order. Elements are
|
||||
// sorted according to a key.
|
||||
func (q Query) OrderBy(selector func(interface{}) interface{}) OrderedQuery {
|
||||
return OrderedQuery{
|
||||
orders: []order{{selector: selector}},
|
||||
original: q,
|
||||
@@ -43,10 +42,30 @@ func (q Query) OrderBy(
|
||||
}
|
||||
}
|
||||
|
||||
// OrderByT is the typed version of OrderBy.
|
||||
//
|
||||
// - selectorFn is of type "func(TSource) TKey"
|
||||
//
|
||||
// NOTE: OrderBy has better performance than OrderByT.
|
||||
func (q Query) OrderByT(selectorFn interface{}) OrderedQuery {
|
||||
selectorGenericFunc, err := newGenericFunc(
|
||||
"OrderByT", "selectorFn", selectorFn,
|
||||
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
selectorFunc := func(item interface{}) interface{} {
|
||||
return selectorGenericFunc.Call(item)
|
||||
}
|
||||
|
||||
return q.OrderBy(selectorFunc)
|
||||
}
|
||||
|
||||
// OrderByDescending sorts the elements of a collection in descending order.
|
||||
// Elements are sorted according to a key.
|
||||
func (q Query) OrderByDescending(
|
||||
selector func(interface{}) interface{}) OrderedQuery {
|
||||
func (q Query) OrderByDescending(selector func(interface{}) interface{}) OrderedQuery {
|
||||
return OrderedQuery{
|
||||
orders: []order{{selector: selector, desc: true}},
|
||||
original: q,
|
||||
@@ -70,9 +89,28 @@ func (q Query) OrderByDescending(
|
||||
}
|
||||
}
|
||||
|
||||
// ThenBy performs a subsequent ordering of the elements in a collection
|
||||
// in ascending order. This method enables you to specify multiple sort criteria
|
||||
// by applying any number of ThenBy or ThenByDescending methods.
|
||||
// OrderByDescendingT is the typed version of OrderByDescending.
|
||||
// - selectorFn is of type "func(TSource) TKey"
|
||||
// NOTE: OrderByDescending has better performance than OrderByDescendingT.
|
||||
func (q Query) OrderByDescendingT(selectorFn interface{}) OrderedQuery {
|
||||
selectorGenericFunc, err := newGenericFunc(
|
||||
"OrderByDescendingT", "selectorFn", selectorFn,
|
||||
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
selectorFunc := func(item interface{}) interface{} {
|
||||
return selectorGenericFunc.Call(item)
|
||||
}
|
||||
|
||||
return q.OrderByDescending(selectorFunc)
|
||||
}
|
||||
|
||||
// ThenBy performs a subsequent ordering of the elements in a collection in
|
||||
// ascending order. This method enables you to specify multiple sort criteria by
|
||||
// applying any number of ThenBy or ThenByDescending methods.
|
||||
func (oq OrderedQuery) ThenBy(
|
||||
selector func(interface{}) interface{}) OrderedQuery {
|
||||
return OrderedQuery{
|
||||
@@ -98,11 +136,29 @@ func (oq OrderedQuery) ThenBy(
|
||||
}
|
||||
}
|
||||
|
||||
// ThenByDescending performs a subsequent ordering of the elements in a collection
|
||||
// in descending order. This method enables you to specify multiple sort criteria
|
||||
// by applying any number of ThenBy or ThenByDescending methods.
|
||||
func (oq OrderedQuery) ThenByDescending(
|
||||
selector func(interface{}) interface{}) OrderedQuery {
|
||||
// ThenByT is the typed version of ThenBy.
|
||||
// - selectorFn is of type "func(TSource) TKey"
|
||||
// NOTE: ThenBy has better performance than ThenByT.
|
||||
func (oq OrderedQuery) ThenByT(selectorFn interface{}) OrderedQuery {
|
||||
selectorGenericFunc, err := newGenericFunc(
|
||||
"ThenByT", "selectorFn", selectorFn,
|
||||
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
selectorFunc := func(item interface{}) interface{} {
|
||||
return selectorGenericFunc.Call(item)
|
||||
}
|
||||
|
||||
return oq.ThenBy(selectorFunc)
|
||||
}
|
||||
|
||||
// ThenByDescending performs a subsequent ordering of the elements in a
|
||||
// collection in descending order. This method enables you to specify multiple
|
||||
// sort criteria by applying any number of ThenBy or ThenByDescending methods.
|
||||
func (oq OrderedQuery) ThenByDescending(selector func(interface{}) interface{}) OrderedQuery {
|
||||
return OrderedQuery{
|
||||
orders: append(oq.orders, order{selector: selector, desc: true}),
|
||||
original: oq.original,
|
||||
@@ -126,10 +182,32 @@ func (oq OrderedQuery) ThenByDescending(
|
||||
}
|
||||
}
|
||||
|
||||
// Sort returns a new query by sorting elements with provided less function
|
||||
// in ascending order. The comparer function should return true if the parameter i
|
||||
// is less than j. While this method is uglier than chaining OrderBy, OrderByDescending,
|
||||
// ThenBy and ThenByDescending methods, it's performance is much better.
|
||||
// ThenByDescendingT is the typed version of ThenByDescending.
|
||||
// - selectorFn is of type "func(TSource) TKey"
|
||||
// NOTE: ThenByDescending has better performance than ThenByDescendingT.
|
||||
func (oq OrderedQuery) ThenByDescendingT(selectorFn interface{}) OrderedQuery {
|
||||
selectorFunc, ok := selectorFn.(func(interface{}) interface{})
|
||||
if !ok {
|
||||
selectorGenericFunc, err := newGenericFunc(
|
||||
"ThenByDescending", "selectorFn", selectorFn,
|
||||
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
selectorFunc = func(item interface{}) interface{} {
|
||||
return selectorGenericFunc.Call(item)
|
||||
}
|
||||
}
|
||||
return oq.ThenByDescending(selectorFunc)
|
||||
}
|
||||
|
||||
// Sort returns a new query by sorting elements with provided less function in
|
||||
// ascending order. The comparer function should return true if the parameter i
|
||||
// is less than j. While this method is uglier than chaining OrderBy,
|
||||
// OrderByDescending, ThenBy and ThenByDescending methods, it's performance is
|
||||
// much better.
|
||||
func (q Query) Sort(less func(i, j interface{}) bool) Query {
|
||||
return Query{
|
||||
Iterate: func() Iterator {
|
||||
@@ -150,6 +228,25 @@ func (q Query) Sort(less func(i, j interface{}) bool) Query {
|
||||
}
|
||||
}
|
||||
|
||||
// SortT is the typed version of Sort.
|
||||
// - lessFn is of type "func(TSource,TSource) bool"
|
||||
// NOTE: Sort has better performance than SortT.
|
||||
func (q Query) SortT(lessFn interface{}) Query {
|
||||
lessGenericFunc, err := newGenericFunc(
|
||||
"SortT", "lessFn", lessFn,
|
||||
simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(bool))),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
lessFunc := func(i, j interface{}) bool {
|
||||
return lessGenericFunc.Call(i, j).(bool)
|
||||
}
|
||||
|
||||
return q.Sort(lessFunc)
|
||||
}
|
||||
|
||||
type sorter struct {
|
||||
items []interface{}
|
||||
less func(i, j interface{}) bool
|
||||
@@ -173,6 +270,10 @@ func (q Query) sort(orders []order) (r []interface{}) {
|
||||
r = append(r, item)
|
||||
}
|
||||
|
||||
if len(r) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for i, j := range orders {
|
||||
orders[i].compare = getComparer(j.selector(r[0]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user