forked from JesseCoretta/go-schemax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext.go
207 lines (176 loc) · 4.13 KB
/
ext.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package schemax
/*
NewExtensions initializes and returns a new instance of [Extensions].
*/
func NewExtensions(o ...Option) (e Extensions) {
e = newExtensions(o...)
e.cast().SetPushPolicy(e.canPush)
return
}
func (r Extensions) Push(x any) error {
return r.push(x)
}
func (r Extensions) push(extn any) (err error) {
if extn == nil {
err = ErrNilInput
return
}
r.cast().Push(extn)
return
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r Extension) IsZero() bool {
return r.extension == nil
}
func (r Extensions) canPush(x ...any) (err error) {
if len(x) == 0 {
return
}
for i := 0; i < len(x) && err == nil; i++ {
instance := x[i]
if e, ok := instance.(Extension); !ok || e.IsZero() {
err = ErrTypeAssert
}
}
return
}
/*
Definition returns the [Definition] instance to which the receiver
instance is assigned.
*/
func (r Extensions) Definition() Definition {
_m := r.cast().Auxiliary()[`def`]
m, _ := _m.(Definition)
return m
}
/*
setDefinition assigns input [Definition] x to the receiver instance.
This is a fluent method.
*/
func (r Extensions) setDefinition(x Definition) Extensions {
r.cast().Auxiliary()[`def`] = x
return r
}
/*
String is a stringer method that returns a properly formatted
quoted descriptor list based on the number of string values
within the receiver instance.
*/
func (r Extensions) String() (s string) {
for i := 0; i < r.Len(); i++ {
s += r.Index(i).String()
}
return
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r Extensions) IsZero() bool {
return r.cast().IsZero()
}
/*
Set assigns the provided key and values instances to the receiver instance,
thereby specifying a new extension value as described in RFC 4512.
*/
func (r Extensions) Set(key string, values ...string) {
_key := uc(key)
var hi, se bool
if def := r.Definition(); def != nil {
if sch := def.Schema(); !sch.IsZero() {
opts := sch.Options()
hi = opts.Positive(HangingIndents)
se = opts.Positive(SortExtensions)
}
}
if _values, found := r.get(_key); !found {
_values = newQStringList(`extensions`)
for v := 0; v < len(values); v++ {
_values.cast().Push(values[v])
}
if _values.cast().Len() > 0 {
ext := new(extension)
ext.XString = key
ext.Values = _values
ext.hindent = hi
ext.sortExts = se
r.Push(Extension{ext})
}
} else {
for i := 0; i < len(values); i++ {
_values.cast().Push(values[i])
}
}
}
/*
Exists returns a Boolean value indicative of whether the specified key
exists within the receiver instance. Case is not significant in the
matching process.
*/
func (r Extensions) Exists(key string) bool {
return r.exists(key)
}
func (r Extensions) exists(key string) (found bool) {
_, found = r.get(key)
return
}
/*
Len returns the current integer length of the receiver instance.
*/
func (r Extensions) Len() int {
return r.len()
}
func (r Extensions) len() int {
return r.cast().Len()
}
/*
Get returns [QuotedStringList] and Boolean instances based
on a successful retrieval of values associated with key in
the receiver instance. The Boolean value is indicative of
a positive match. Case is not significant in the matching
process.
*/
func (r Extensions) Get(key string) (QuotedStringList, bool) {
return r.get(key)
}
func (r Extensions) get(key string) (values QuotedStringList, found bool) {
for i := 0; i < r.len() && values.cast().Len() == 0; i++ {
if eq(r.index(i).XString, key) {
values = r.index(i).Values
}
}
found = values.cast().Len() > 0
return
}
/*
Keys returns all keys found within the underlying map instance.
*/
func (r Extensions) Keys() (keys []string) {
for i := 0; i < r.len(); i++ {
keys = append(keys, r.index(i).XString)
}
return
}
func (r Extensions) tmplFunc() (e string) {
if !r.IsZero() {
e = r.String()
}
return
}
/*
String returns the string representation of the receiver instance.
*/
func (r Extension) String() (s string) {
if !r.IsZero() {
switch r.Values.Len() {
case 0:
break
case 1:
s = hindent(r.hindent) + r.XString + ` ` + `'` + r.Values.Index(0) + `'`
default:
s = hindent(r.hindent) + r.XString + ` ` + r.Values.String()
}
}
return
}