-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
99 lines (81 loc) · 2.09 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package acal
import "fmt"
const (
UnknownValueName = "Unknown"
)
// TypedValue is a Value bounded by a particular type.
//
//go:generate mockery --name=TypedValue --case underscore --inpackage
type TypedValue[T any] interface {
Value
// GetTypedValue returns the typed value this TypedValue contains.
GetTypedValue() T
}
// Value is a wrapper that gives an identity to any variables that are used in our code.
//
//go:generate mockery --name=Value --case underscore --inpackage
type Value interface {
identifiable
extractable
syntaxOperandProvider
// IsNil returns whether this Value is nil.
IsNil() bool
// GetValue returns the untyped value this Value contains.
GetValue() any
// Stringify returns the value this Value contains as a string.
Stringify() string
}
type identifiable interface {
// GetName return the name of this identifiable.
GetName() string
// GetAlias return the alias of this identifiable.
GetAlias() string
// SetAlias updates the alias of this identifiable to the provided string.
SetAlias(string)
// HasIdentity returns whether this identifiable was given an identity.
HasIdentity() bool
// Identify returns the identity that was given to this identifiable.
Identify() string
}
type namedValue struct {
name string
alias string
}
// GetName ...
func (v *namedValue) GetName() string {
return v.name
}
// GetAlias ...
func (v *namedValue) GetAlias() string {
return v.alias
}
// SetAlias ...
func (v *namedValue) SetAlias(alias string) {
v.alias = alias
}
// HasIdentity ...
func (v *namedValue) HasIdentity() bool {
return v.GetName() != "" || v.GetAlias() != ""
}
// Identify ...
func (v *namedValue) Identify() string {
if v.GetAlias() != "" {
return v.GetAlias()
}
return v.GetName()
}
type valueFormatter[T any] struct {
formatFn func(T) string
}
func (f *valueFormatter[T]) formatValue(v T) string {
if f.formatFn == nil {
return fmt.Sprintf("%v", v)
}
return f.formatFn(v)
}
func (f *valueFormatter[T]) GetFormatFn() func(T) string {
return f.formatFn
}
func (f *valueFormatter[T]) WithFormatFn(formatFn func(T) string) {
f.formatFn = formatFn
}