-
Notifications
You must be signed in to change notification settings - Fork 118
/
ast_val_float_value.go
58 lines (48 loc) · 1.51 KB
/
ast_val_float_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
package ast
import (
"bytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/internal/unsafebytes"
"github.com/wundergraph/graphql-go-tools/v2/pkg/lexer/position"
)
// FloatValue
// example:
// 13.37 / -13.37
type FloatValue struct {
Negative bool // indicates if the value is negative
NegativeSign position.Position // optional -
Raw ByteSliceReference // e.g. 13.37
}
func (d *Document) CopyFloatValue(ref int) int {
return d.AddFloatValue(FloatValue{
Negative: d.FloatValues[ref].Negative,
Raw: d.copyByteSliceReference(d.FloatValues[ref].Raw),
})
}
func (d *Document) FloatValueAsFloat32(ref int) (out float32) {
in := d.Input.ByteSlice(d.FloatValues[ref].Raw)
out = unsafebytes.BytesToFloat32(in)
if d.FloatValues[ref].Negative {
out = -out
}
return
}
func (d *Document) FloatValueIsNegative(ref int) bool {
return d.FloatValues[ref].Negative
}
func (d *Document) FloatValueRaw(ref int) ByteSlice {
return d.Input.ByteSlice(d.FloatValues[ref].Raw)
}
func (d *Document) FloatValuesAreEqual(left, right int) bool {
return d.FloatValueIsNegative(left) == d.FloatValueIsNegative(right) &&
bytes.Equal(d.FloatValueRaw(left), d.FloatValueRaw(right))
}
func (d *Document) AddFloatValue(value FloatValue) (ref int) {
d.FloatValues = append(d.FloatValues, value)
return len(d.FloatValues) - 1
}
func (d *Document) ImportFloatValue(raw ByteSlice, isNegative bool) (ref int) {
return d.AddFloatValue(FloatValue{
Negative: isNegative,
Raw: d.Input.AppendInputBytes(raw),
})
}