-
Notifications
You must be signed in to change notification settings - Fork 118
/
ast_val_int_value.go
76 lines (63 loc) · 1.86 KB
/
ast_val_int_value.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
package ast
import (
"bytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/position"
)
// IntValue
// example:
// 123 / -123
type IntValue struct {
Negative bool // indicates if the value is negative
NegativeSign position.Position // optional -
Raw ByteSliceReference // e.g. 123
}
func (d *Document) CopyIntValue(ref int) int {
return d.AddIntValue(IntValue{
Negative: d.IntValues[ref].Negative,
Raw: d.copyByteSliceReference(d.IntValues[ref].Raw),
})
}
func (d *Document) IntValueAsInt(ref int) (out int64) {
in := d.Input.ByteSlice(d.IntValues[ref].Raw)
out = unsafebytes.BytesToInt64(in)
if d.IntValues[ref].Negative {
out = -out
}
return
}
func (d *Document) IntValueAsInt32(ref int) (out int32) {
in := d.Input.ByteSlice(d.IntValues[ref].Raw)
out = unsafebytes.BytesToInt32(in)
if d.IntValues[ref].Negative {
out = -out
}
return
}
func (d *Document) IntValueValidInt32(ref int) bool {
in := d.Input.ByteSlice(d.IntValues[ref].Raw)
return unsafebytes.BytesIsValidInt32(in)
}
func (d *Document) IntValue(ref int) IntValue {
return d.IntValues[ref]
}
func (d *Document) IntValueIsNegative(ref int) bool {
return d.IntValues[ref].Negative
}
func (d *Document) IntValueRaw(ref int) ByteSlice {
return d.Input.ByteSlice(d.IntValues[ref].Raw)
}
func (d *Document) IntValuesAreEquals(left, right int) bool {
return d.IntValueIsNegative(left) == d.IntValueIsNegative(right) &&
bytes.Equal(d.IntValueRaw(left), d.IntValueRaw(right))
}
func (d *Document) AddIntValue(value IntValue) (ref int) {
d.IntValues = append(d.IntValues, value)
return len(d.IntValues) - 1
}
func (d *Document) ImportIntValue(raw ByteSlice, isNegative bool) (ref int) {
return d.AddIntValue(IntValue{
Negative: isNegative,
Raw: d.Input.AppendInputBytes(raw),
})
}