-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
100 lines (81 loc) · 2.12 KB
/
template.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
package entfga
import (
"strings"
"text/template"
"entgo.io/ent/entc/gen"
"github.com/stoewer/go-strcase"
)
// extractObjectType gets the key that is used for the object type
func extractObjectType(val any) string {
objectType, ok := val.(string)
if !ok {
return ""
}
return objectType
}
// extractIDField gets the key that is used for the id field
func extractIDField(val any) string {
idField, ok := val.(string)
if !ok {
return ""
}
if idField == "" {
return "ID"
}
return idField
}
// extractNillableIDField gets the key that is used for the nillable id field
func extractNillableIDField(val any) bool {
nillable, ok := val.(bool)
if !ok {
return false
}
return nillable
}
// extractOrgOwnedField gets the key that is used for the org owned field
func extractOrgOwnedField(val any) bool {
orgOwned, ok := val.(bool)
if !ok {
return false
}
return orgOwned
}
// hasCreateID checks if the input would have the ID to check permissions
func hasCreateID(val any) bool {
idField, ok := val.(string)
if !ok {
return false
}
if idField == "" || idField == "ID" {
return false
}
return true
}
// extractIncludeHooks gets the key that is used to determine if the hooks should be included
func extractIncludeHooks(val any) bool {
includeHooks, ok := val.(bool)
if !ok {
return true
}
return includeHooks
}
// useSoftDeletes checks the config properties for the Soft Delete setting
func useSoftDeletes(config Config) bool {
return config.SoftDeletes
}
// parseTemplate parses the template and sets values in the template
func parseTemplate(name, path string) *gen.Template {
t := gen.NewTemplate(name)
t.Funcs(template.FuncMap{
"extractObjectType": extractObjectType,
"extractIDField": extractIDField,
"extractNillableIDField": extractNillableIDField,
"extractOrgOwnedField": extractOrgOwnedField,
"hasCreateID": hasCreateID,
"extractIncludeHooks": extractIncludeHooks,
"useSoftDeletes": useSoftDeletes,
"ToUpperCamel": strcase.UpperCamelCase,
"ToLower": strings.ToLower,
})
return gen.MustParse(t.ParseFS(_templates, path))
}