forked from goal-web/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupports.go
142 lines (115 loc) · 3.67 KB
/
supports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package contracts
import (
"reflect"
"sort"
)
type Context interface {
// Get 从上下文中检索数据
// retrieves data from the context.
Get(key string) interface{}
// Set 在上下文中保存数据
// saves data in the context.
Set(key string, val interface{})
}
type Fields map[string]interface{}
type Interface interface {
reflect.Type
GetType() reflect.Type
IsSubClass(class interface{}) bool
// ClassName 获取类名
ClassName() string
}
type Class interface {
Interface
// New 通过 Fields
New(fields Fields) interface{}
NewByTag(fields Fields, tag string) interface{}
}
type FieldsProvider interface {
Fields() Fields
}
type Json interface {
ToJson() string
}
type Getter interface {
GetString(key string) string
GetInt64(key string) int64
GetInt(key string) int
GetFloat64(key string) float64
GetFloat(key string) float32
GetBool(key string) bool
GetFields(key string) Fields
}
type OptionalGetter interface {
StringOption(key string, defaultValue string) string
Int64Option(key string, defaultValue int64) int64
IntOption(key string, defaultValue int) int
Float64Option(key string, defaultValue float64) float64
FloatOption(key string, defaultValue float32) float32
BoolOption(key string, defaultValue bool) bool
FieldsOption(key string, defaultValue Fields) Fields
}
type Collection interface {
Json
// sort
sort.Interface
Sort(sorter interface{}) Collection
IsEmpty() bool
// filter
Map(filter interface{}) Collection
Filter(filter interface{}) Collection
Skip(filter interface{}) Collection
Where(field string, args ...interface{}) Collection
WhereLt(field string, arg interface{}) Collection
WhereLte(field string, arg interface{}) Collection
WhereGt(field string, arg interface{}) Collection
WhereGte(field string, arg interface{}) Collection
WhereIn(field string, arg interface{}) Collection
WhereNotIn(field string, arg interface{}) Collection
// keys、values
// Pluck 数据类型为 []map、[]struct 的时候起作用
Pluck(key string) Fields
// Only 数据类型为 []map、[]struct 的时候起作用
Only(keys ...string) Collection
// First 获取首个元素, []struct或者[]map可以获取指定字段
First(keys ...string) interface{}
// Last 获取最后一个元素, []struct或者[]map可以获取指定字段
Last(keys ...string) interface{}
// union、merge...
// Prepend 从开头插入元素
Prepend(item ...interface{}) Collection
// Push 从最后插入元素
Push(items ...interface{}) Collection
// Pull 从尾部获取并移出一个元素
Pull(defaultValue ...interface{}) interface{}
// Shift 从头部获取并移出一个元素
Shift(defaultValue ...interface{}) interface{}
// Put 替换一个元素,如果 index 不存在会执行 Push,返回新集合
Put(index int, item interface{}) Collection
// Offset 替换一个元素,如果 index 不存在会执行 Push
Offset(index int, item interface{}) Collection
// Merge 合并其他集合
Merge(collections ...Collection) Collection
// Reverse 返回一个顺序翻转后的集合
Reverse() Collection
// Chunk 分块,handler 返回 error 表示中断
Chunk(size int, handler func(collection Collection, page int) error) error
// Random 随机返回n个元素,默认1个
Random(size ...uint) Collection
// aggregate
Sum(key ...string) float64
Max(key ...string) float64
Min(key ...string) float64
Avg(key ...string) float64
Count() int
// convert
ToIntArray() (results []int)
ToInt64Array() (results []int64)
ToInterfaceArray() []interface{}
ToFloat64Array() (results []float64)
ToFloatArray() (results []float32)
ToBoolArray() (results []bool)
ToStringArray() (results []string)
ToFields() Fields
ToArrayFields() []Fields
}