-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable.go
79 lines (71 loc) · 1.8 KB
/
variable.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
package humus
import (
"errors"
"fmt"
"strconv"
"strings"
)
/*
Variable represents a value variable. This static type enforces a
'val' in the query generation in e.g. functions and filters.
//TODO, not implemented yet.
*/
type Variable string
//processInterface takes the type and returns what variable it is as well as a string representation of it.
//this function is the reason why using the default generated values is important since it includes the static type
//of predicate/uid.,
//note that UID and Variable types are handled separately.
func processInterface(value interface{}) (string, varType) {
switch a := value.(type) {
case int:
return strconv.Itoa(a), typeInt
case int64:
return strconv.FormatInt(a, 10), typeInt
case string:
return a, typeString
case Predicate:
return string(a), typePred
case UID:
return string(a), typeUid
case float32:
return strconv.FormatFloat(float64(a), 'f', 16, 32), typeFloat
case float64:
return strconv.FormatFloat(a, 'f', 16, 64), typeFloat
case Variable:
return string(a), typeVar
default:
return fmt.Sprintf("%s", a), typeString
}
}
//variable represents the struct for a field variable.
type variable struct {
name string
value string
alias bool
}
func (v variable) canApply(mt modifierSource) bool {
return true
}
func (v variable) apply(root *GeneratedQuery, meta FieldMeta, mt modifierSource, sb *strings.Builder) error {
if v.value == "" && v.name == "" {
return errors.New("missing values in graphVariable")
}
sb.WriteByte(' ')
sb.WriteString(v.name)
if v.name != "" {
if v.alias {
sb.WriteString(" : ")
} else {
sb.WriteString(" as ")
}
}
sb.WriteString(v.value)
sb.WriteByte(' ')
return nil
}
func (v variable) priority() modifierType {
return modifierVariable
}
func (v variable) parenthesis() bool {
return false
}