-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenums.go
151 lines (128 loc) · 3.65 KB
/
enums.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
143
144
145
146
147
148
149
150
151
package yarql
import (
"errors"
"fmt"
"reflect"
"sort"
h "github.com/mjarkk/yarql/helpers"
)
type enum struct {
contentType reflect.Type
contentKind reflect.Kind
typeName string
entries []enumEntry
qlType qlType
}
type enumEntry struct {
keyBytes []byte
key string
value reflect.Value
}
func (s *Schema) getEnum(t reflect.Type) (int, *enum) {
if len(t.PkgPath()) == 0 || len(t.Name()) == 0 || !validEnumType(t) {
return -1, nil
}
for i, enum := range s.definedEnums {
if enum.typeName == t.Name() {
return i, &enum
}
}
return -1, nil
}
func validEnumType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// All int kinds are allowed
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// All uint kinds are allowed
return true
case reflect.String:
// Strings are allowed
return true
default:
return false
}
}
// RegisterEnum registers a new enum type
func (s *Schema) RegisterEnum(enumMap interface{}) (added bool, err error) {
if s.parsed {
return false, errors.New("(*yarql.Schema).RegisterEnum() cannot be ran after (*yarql.Schema).Parse()")
}
enum, err := registerEnumCheck(enumMap)
if enum == nil || err != nil {
return false, err
}
s.definedEnums = append(s.definedEnums, *enum)
return true, nil
}
func registerEnumCheck(enumMap interface{}) (*enum, error) {
mapReflection := reflect.ValueOf(enumMap)
invalidTypeMsg := fmt.Errorf("RegisterEnum input must be of type map[string]CustomType(int..|uint..|string) as input, %+v given", enumMap)
if enumMap == nil || mapReflection.IsZero() || mapReflection.IsNil() {
return nil, invalidTypeMsg
}
mapType := mapReflection.Type()
if mapType.Kind() != reflect.Map {
// Tye input type must be a map
return nil, invalidTypeMsg
}
if mapType.Key().Kind() != reflect.String {
// The map key must be a string
return nil, invalidTypeMsg
}
contentType := mapType.Elem()
if !validEnumType(contentType) {
return nil, invalidTypeMsg
}
if contentType.PkgPath() == "" || contentType.Name() == "" {
return nil, errors.New("RegisterEnum input map value must have a global custom type value (type Animals string) or (type Rules uint64)")
}
inputLen := mapReflection.Len()
if inputLen == 0 {
// No point in registering enums with 0 items
return nil, nil
}
entries := make([]enumEntry, inputLen)
qlTypeEnumValues := make([]qlEnumValue, inputLen)
iter := mapReflection.MapRange()
i := 0
for iter.Next() {
k := iter.Key()
keyStr := k.Interface().(string)
if keyStr == "" {
return nil, errors.New("RegisterEnum input map cannot contain empty keys")
}
err := validGraphQlName([]byte(keyStr))
if err != nil {
return nil, errors.New(`RegisterEnum map key must start with an alphabetic character (lower or upper) followed by the same or a "_", key given: ` + keyStr)
}
entries[i] = enumEntry{
keyBytes: []byte(keyStr),
key: keyStr,
value: iter.Value(),
}
qlTypeEnumValues[i] = qlEnumValue{
Name: keyStr,
Description: h.PtrToEmptyStr,
IsDeprecated: false,
DeprecationReason: nil,
}
i++
}
sort.Slice(qlTypeEnumValues, func(a int, b int) bool { return qlTypeEnumValues[a].Name < qlTypeEnumValues[b].Name })
name := contentType.Name()
qlType := qlType{
Kind: typeKindEnum,
Name: &name,
Description: h.PtrToEmptyStr,
EnumValues: func(args isDeprecatedArgs) []qlEnumValue { return qlTypeEnumValues },
}
return &enum{
contentType: contentType,
contentKind: contentType.Kind(),
entries: entries,
typeName: name,
qlType: qlType,
}, nil
}