-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
108 lines (91 loc) · 2.63 KB
/
functions.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
package enput
import (
"strings"
"entgo.io/ent/entc/gen"
"github.com/iancoleman/strcase"
)
var (
pascal = gen.Funcs["pascal"].(func(string) string)
kebab = strcase.ToKebab
snake = gen.Funcs["snake"].(func(string) string)
buggyCamel = gen.Funcs["camel"].(func(string) string)
camel = func(s string) string { return buggyCamel(snake(s)) }
strCase = snake
)
func (e *Extension) initFunctions() {
if e.data.Config.Case == Camel {
strCase = camel
} else if e.data.Config.Case == Pascal {
strCase = pascal
}
is_comparable := func(field Field) bool {
return has_prefixes(field.Type, []string{"int", "uint", "string", "float", "time.Time"})
}
orderable := func(f Field) bool {
return has_prefixes(f.Type, []string{
"string",
"int",
"uint",
"float",
"time.Time",
"bool",
})
}
ignore_query := func(s Schema) bool {
return in(s.Name, e.data.Config.IgnoreSchemas.Query)
}
ignore_create := func(s Schema) bool {
return in(s.Name, e.data.Config.IgnoreSchemas.Create)
}
ignore_update := func(s Schema) bool {
return in(s.Name, e.data.Config.IgnoreSchemas.Update)
}
ignore_delete := func(s Schema) bool {
return in(s.Name, e.data.Config.IgnoreSchemas.Delete)
}
order_fields := func(s Schema) string {
fields := []string{}
for _, f := range s.Fields {
if orderable(f) {
fields = append(fields, snake(f.Name))
}
}
return "\"" + strings.Join(fields, "\" | \"") + "\""
}
select_fields := func(s Schema) string {
fields := []string{}
for _, f := range s.Fields {
fields = append(fields, snake(f.Name))
}
return "\"" + strings.Join(fields, "\" | \"") + "\""
}
// aggregate_fields := func(s Schema) string {
// fields := []string{}
// for _, f := range s.Fields {
// if f.Type != "json.RawMessage" {
// fields = append(fields, snake(f.Name))
// }
// }
// return "\"" + strings.Join(fields, "\" | \"") + "\""
// }
// group_by_fields := func(s Schema) string {
// fields := []string{}
// for _, f := range s.Fields {
// fields = append(fields, snake(f.Name))
// }
// return "\"" + strings.Join(fields, "\" | \"") + "\""
// }
gen.Funcs["order_fields"] = order_fields
gen.Funcs["select_fields"] = select_fields
// gen.Funcs["aggregate_fields"] = aggregate_fields
// gen.Funcs["group_by_fields"] = group_by_fields
gen.Funcs["is_comparable"] = is_comparable
gen.Funcs["ignore_query"] = ignore_query
gen.Funcs["ignore_create"] = ignore_create
gen.Funcs["ignore_update"] = ignore_update
gen.Funcs["ignore_delete"] = ignore_delete
gen.Funcs["case"] = strCase
gen.Funcs["pascal"] = pascal
gen.Funcs["kebab"] = kebab
gen.Funcs["camel"] = camel
}