-
Notifications
You must be signed in to change notification settings - Fork 123
/
ast_directive_definition.go
157 lines (136 loc) · 5.31 KB
/
ast_directive_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
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
152
153
154
155
156
157
package ast
import (
"bytes"
"github.com/wundergraph/graphql-go-tools/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/lexer/position"
)
// DirectiveDefinition
// example:
// directive @example on FIELD
type DirectiveDefinition struct {
Description Description // optional, describes the directive
DirectiveLiteral position.Position // directive
At position.Position // @
Name ByteSliceReference // e.g. example
HasArgumentsDefinitions bool
ArgumentsDefinition InputValueDefinitionList // optional, e.g. (if: Boolean)
On position.Position // on
DirectiveLocations DirectiveLocations // e.g. FIELD
Repeatable Repeatable
}
type Repeatable struct {
IsRepeatable bool
Position position.Position
}
func (d *Document) DirectiveDefinitionNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.DirectiveDefinitions[ref].Name)
}
func (d *Document) DirectiveDefinitionNameString(ref int) string {
return unsafebytes.BytesToString(d.Input.ByteSlice(d.DirectiveDefinitions[ref].Name))
}
func (d *Document) DirectiveDefinitionDescriptionBytes(ref int) ByteSlice {
if !d.DirectiveDefinitions[ref].Description.IsDefined {
return nil
}
return d.Input.ByteSlice(d.DirectiveDefinitions[ref].Description.Content)
}
func (d *Document) DirectiveDefinitionDescriptionString(ref int) string {
return unsafebytes.BytesToString(d.DirectiveDefinitionDescriptionBytes(ref))
}
func (d *Document) DirectiveArgumentInputValueDefinition(directiveName ByteSlice, argumentName ByteSlice) int {
for i := range d.DirectiveDefinitions {
if bytes.Equal(directiveName, d.Input.ByteSlice(d.DirectiveDefinitions[i].Name)) {
for _, j := range d.DirectiveDefinitions[i].ArgumentsDefinition.Refs {
if bytes.Equal(argumentName, d.Input.ByteSlice(d.InputValueDefinitions[j].Name)) {
return j
}
}
}
}
return -1
}
func (d *Document) DirectiveDefinitionArgumentDefaultValueString(directiveName, argumentName string) string {
inputValueDefinition := d.DirectiveArgumentInputValueDefinition(unsafebytes.StringToBytes(directiveName), unsafebytes.StringToBytes(argumentName))
if inputValueDefinition == -1 {
return ""
}
defaultValue := d.InputValueDefinitionDefaultValue(inputValueDefinition)
if defaultValue.Kind != ValueKindString {
return ""
}
return d.StringValueContentString(defaultValue.Ref)
}
func (d *Document) DirectiveDefinitionArgumentDefaultValueBool(directiveName, argumentName string) bool {
inputValueDefinition := d.DirectiveArgumentInputValueDefinition(unsafebytes.StringToBytes(directiveName), unsafebytes.StringToBytes(argumentName))
if inputValueDefinition == -1 {
return false
}
defaultValue := d.InputValueDefinitionDefaultValue(inputValueDefinition)
if defaultValue.Kind != ValueKindBoolean {
return false
}
return bool(d.BooleanValue(defaultValue.Ref))
}
func (d *Document) DirectiveDefinitionArgumentDefaultValueInt64(directiveName, argumentName string) int64 {
inputValueDefinition := d.DirectiveArgumentInputValueDefinition(unsafebytes.StringToBytes(directiveName), unsafebytes.StringToBytes(argumentName))
if inputValueDefinition == -1 {
return -1
}
defaultValue := d.InputValueDefinitionDefaultValue(inputValueDefinition)
if defaultValue.Kind != ValueKindInteger {
return -1
}
return d.IntValueAsInt(defaultValue.Ref)
}
func (d *Document) DirectiveDefinitionArgumentDefaultValueFloat32(directiveName, argumentName string) float32 {
inputValueDefinition := d.DirectiveArgumentInputValueDefinition(unsafebytes.StringToBytes(directiveName), unsafebytes.StringToBytes(argumentName))
if inputValueDefinition == -1 {
return -1
}
defaultValue := d.InputValueDefinitionDefaultValue(inputValueDefinition)
if defaultValue.Kind != ValueKindFloat {
return -1
}
return d.FloatValueAsFloat32(defaultValue.Ref)
}
func (d *Document) AddDirectiveDefinition(directiveDefinition DirectiveDefinition) (ref int) {
d.DirectiveDefinitions = append(d.DirectiveDefinitions, directiveDefinition)
return len(d.DirectiveDefinitions) - 1
}
func (d *Document) ImportDirectiveDefinition(name, description string, argsRefs []int, locations []string) (ref int) {
directiveLocations := DirectiveLocations{}
for _, location := range locations {
_ = directiveLocations.SetFromRaw([]byte(location))
}
definition := DirectiveDefinition{
Description: d.ImportDescription(description),
Name: d.Input.AppendInputString(name),
HasArgumentsDefinitions: len(argsRefs) > 0,
ArgumentsDefinition: InputValueDefinitionList{
Refs: argsRefs,
},
DirectiveLocations: directiveLocations,
}
ref = d.AddDirectiveDefinition(definition)
d.ImportRootNode(ref, NodeKindDirectiveDefinition)
return
}
func (d *Document) DirectiveDefinitionByName(name string) (int, bool) {
for i := range d.DirectiveDefinitions {
if name == d.Input.ByteSliceString(d.DirectiveDefinitions[i].Name) {
return i, true
}
}
return -1, false
}
func (d *Document) DirectiveDefinitionByNameBytes(name []byte) (int, bool) {
for i := range d.DirectiveDefinitions {
if bytes.Equal(name, d.Input.ByteSlice(d.DirectiveDefinitions[i].Name)) {
return i, true
}
}
return -1, false
}
func (d *Document) DirectiveDefinitionIsRepeatable(ref int) bool {
return d.DirectiveDefinitions[ref].Repeatable.IsRepeatable
}