-
Notifications
You must be signed in to change notification settings - Fork 118
/
ast_enum_type_definition.go
92 lines (77 loc) · 2.83 KB
/
ast_enum_type_definition.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
package ast
import (
"bytes"
"github.com/wundergraph/graphql-go-tools/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
)
// EnumTypeDefinition
// example:
//
// enum Direction {
// NORTH
// EAST
// SOUTH
// WEST
// }
type EnumTypeDefinition struct {
Description Description // optional, describes enum
EnumLiteral position.Position // enum
Name ByteSliceReference // e.g. Direction
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
HasEnumValuesDefinition bool
EnumValuesDefinition EnumValueDefinitionList // optional, e.g. { NORTH EAST }
}
func (d *Document) EnumTypeDefinitionNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.EnumTypeDefinitions[ref].Name)
}
func (d *Document) EnumTypeDefinitionNameString(ref int) string {
return unsafebytes.BytesToString(d.Input.ByteSlice(d.EnumTypeDefinitions[ref].Name))
}
func (d *Document) EnumTypeDefinitionDescriptionBytes(ref int) ByteSlice {
if !d.EnumTypeDefinitions[ref].Description.IsDefined {
return nil
}
return d.Input.ByteSlice(d.EnumTypeDefinitions[ref].Description.Content)
}
func (d *Document) EnumTypeDefinitionDescriptionString(ref int) string {
return unsafebytes.BytesToString(d.EnumTypeDefinitionDescriptionBytes(ref))
}
func (d *Document) EnumTypeDefinitionHasDirectives(ref int) bool {
return d.EnumTypeDefinitions[ref].HasDirectives
}
func (d *Document) EnumTypeDefinitionHasEnumValueDefinition(ref int) bool {
return d.EnumTypeDefinitions[ref].HasEnumValuesDefinition
}
func (d *Document) EnumTypeDefinitionContainsEnumValue(enumTypeDef int, valueName ByteSlice) bool {
for _, i := range d.EnumTypeDefinitions[enumTypeDef].EnumValuesDefinition.Refs {
if bytes.Equal(valueName, d.EnumValueDefinitionNameBytes(i)) {
return true
}
}
return false
}
func (d *Document) AddEnumTypeDefinition(definition EnumTypeDefinition) (ref int) {
d.EnumTypeDefinitions = append(d.EnumTypeDefinitions, definition)
return len(d.EnumTypeDefinitions) - 1
}
func (d *Document) ImportEnumTypeDefinition(name, description string, valueRefs []int) (ref int) {
return d.ImportEnumTypeDefinitionWithDirectives(name, description, valueRefs, nil)
}
func (d *Document) ImportEnumTypeDefinitionWithDirectives(name, description string, valueRefs []int, directiveRefs []int) (ref int) {
definition := EnumTypeDefinition{
Description: d.ImportDescription(description),
Name: d.Input.AppendInputString(name),
HasEnumValuesDefinition: len(valueRefs) > 0,
EnumValuesDefinition: EnumValueDefinitionList{
Refs: valueRefs,
},
HasDirectives: len(directiveRefs) > 0,
Directives: DirectiveList{
Refs: directiveRefs,
},
}
ref = d.AddEnumTypeDefinition(definition)
d.ImportRootNode(ref, NodeKindEnumTypeDefinition)
return
}