-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_ops.go
140 lines (116 loc) · 2.91 KB
/
value_ops.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
package acal
// IValueOps ...
//
//go:generate mockery --name=IValueOps --case underscore --inpackage
type IValueOps interface {
// IsNilValue ...
IsNilValue(v Value) bool
// HasIdentity ...
HasIdentity(v Value) bool
// Identify ...
Identify(v Value) string
// Stringify ...
Stringify(v Value) string
// Describe ...
Describe(v Value) string
// DescribeValueAsFormula ...
DescribeValueAsFormula(v Value) func() *SyntaxNode
}
type valueOpsImpl struct{}
// IsNilValue ...
func (valueOpsImpl) IsNilValue(v Value) bool {
return v == nil || v.IsNil()
}
// HasIdentity ...
func (valueOpsImpl) HasIdentity(v Value) bool {
if IsNilValue(v) {
return false
}
return v.HasIdentity()
}
// Identify ...
func (valueOpsImpl) Identify(v Value) string {
if IsNilValue(v) {
return UnknownValueName
}
return v.Identify()
}
// Stringify ...
func (valueOpsImpl) Stringify(v Value) string {
if IsNilValue(v) {
return ""
}
return v.Stringify()
}
// Describe ...
func (valueOpsImpl) Describe(v Value) string {
if IsNilValue(v) {
return "?[?]"
}
identity := Identify(v)
if identity == "" {
return "?[" + v.Stringify() + "]"
}
return identity + "[" + v.Stringify() + "]"
}
// DescribeValueAsFormula ...
func (valueOpsImpl) DescribeValueAsFormula(v Value) func() *SyntaxNode {
if HasIdentity(v) {
return func() *SyntaxNode {
return &SyntaxNode{
category: OpCategoryAssignVariable,
op: OpTransparent,
operands: []any{
v,
},
}
}
}
fp, ok := v.(formulaProvider)
if ok && fp.HasFormula() {
return fp.GetFormulaFn()
}
return func() *SyntaxNode {
return &SyntaxNode{
category: OpCategoryAssignStatic,
op: OpTransparent,
opDesc: Describe(v),
}
}
}
var valueOps IValueOps = valueOpsImpl{}
// IsNilValue returns whether the given Value is nil.
func IsNilValue(v Value) bool {
return valueOps.IsNilValue(v)
}
// HasIdentity returns whether the given Value is anchored.
func HasIdentity(v Value) bool {
return valueOps.HasIdentity(v)
}
// Identify returns the identity of the given Value, which is its
// alias if not nil. Otherwise, the result should be its name.
func Identify(v Value) string {
return valueOps.Identify(v)
}
// Stringify returns the value this Value contains as a string.
func Stringify(v Value) string {
return valueOps.Stringify(v)
}
// Describe returns a full description of the given Value, including
// its identity and current value in string format.
func Describe(v Value) string {
return valueOps.Describe(v)
}
// DescribeValueAsFormula returns a full description of the given Value,
// in the form of a formula.
func DescribeValueAsFormula[T any](v TypedValue[T]) func() *SyntaxNode {
return valueOps.DescribeValueAsFormula(PreProcessOperand(v))
}
// ExtractTypedValue returns the typed value the given TypedValue contains.
func ExtractTypedValue[T any](tv TypedValue[T]) T {
if IsNilValue(tv) {
var temp T
return temp
}
return tv.GetTypedValue()
}