-
Notifications
You must be signed in to change notification settings - Fork 1
/
hints.go
108 lines (89 loc) · 2.49 KB
/
hints.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
package jtdinfer
// Wildcard represents the character that matches any value for hints.
const Wildcard = "-"
// Hints contains the default number type to use and all the hints for enums,
// values and discriminators.
type Hints struct {
DefaultNumType NumType
Enums HintSet
Values HintSet
Discriminator HintSet
}
// WithoutHints is a shorthand to return empty hints.
func WithoutHints() Hints {
return Hints{}
}
// SubHints will return the sub hints for all hint sets for the passed key.
func (h Hints) SubHints(key string) Hints {
return Hints{
DefaultNumType: h.DefaultNumType,
Enums: h.Enums.SubHints(key),
Values: h.Values.SubHints(key),
Discriminator: h.Discriminator.SubHints(key),
}
}
// IsEnumActive checks if the enum hint set is active.
func (h Hints) IsEnumActive() bool {
return h.Enums.IsActive()
}
// IsValuesActive checks if the values hint set is active.
func (h Hints) IsValuesActive() bool {
return h.Values.IsActive()
}
// PeekActiveDiscriminator will peek the currently active discriminator, if any.
// The returned boolean tells if there is an active discriminator.
func (h Hints) PeekActiveDiscriminator() (string, bool) {
return h.Discriminator.PeekActive()
}
// HintSet represents a list of paths (lists) to match for hints.
type HintSet struct {
Values [][]string
}
// NewHintSet creates a new empty `HintSet`.
func NewHintSet() HintSet {
return HintSet{
Values: [][]string{},
}
}
// Add will add a path (slice) to the `HintSet`.
func (h HintSet) Add(v []string) HintSet {
h.Values = append(h.Values, v)
return h
}
// SubHints will filter all the current sets and keep those who's first element
// matches the passed key or wildcard.
func (h HintSet) SubHints(key string) HintSet {
filteredValues := [][]string{}
for _, values := range h.Values {
if len(values) == 0 {
continue
}
first := values[0]
if first == Wildcard || first == key {
filteredValues = append(filteredValues, values[1:])
}
}
return HintSet{
Values: filteredValues,
}
}
// IsActive returns true if any set in the hint set his active.
func (h HintSet) IsActive() bool {
for _, valueList := range h.Values {
if len(valueList) == 0 {
return true
}
}
return false
}
// PeekActive returns the currently active value if any. The returned boolean
// tells if a value was found.
func (h HintSet) PeekActive() (string, bool) {
for _, values := range h.Values {
if len(values) != 1 {
continue
}
return values[0], true
}
return "", false
}