-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
319 lines (287 loc) · 8.04 KB
/
slice.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package types
import (
"fmt"
"log"
"reflect"
"strings"
"github.com/cruffinoni/xml-generator/xml/interface"
"github.com/cruffinoni/xml-generator/xml/saver/xmlFile"
"github.com/cruffinoni/xml-generator/xml/types/primary"
"github.com/cruffinoni/xml-generator/xml"
"github.com/cruffinoni/xml-generator/xml/attributes"
"github.com/cruffinoni/xml-generator/xml/saver"
"github.com/cruffinoni/xml-generator/xml/unmarshal"
)
type sliceData[T any] struct {
data T
// str is a string representation of data
str string
attr attributes.Attributes
fmt.Stringer
tag string
hidden bool
kind reflect.Kind
}
func (s *sliceData[T]) Assign(e *xml.Element) error {
var err error
//log.Printf("Assign on slicedata called: %v (%v) > %T", e.XMLPath(), e.Attr, s.data)
s.kind = reflect.TypeOf(s.data).Kind()
if s.kind == reflect.Ptr {
err = unmarshal.Element(e, s.data)
s.hidden = reflect.ValueOf(s.data).IsZero()
//log.Printf("Kind is ptr. Is it hidden ? %v", s.hidden)
} else if utils.IsReflectPrimaryType(s.kind) {
s.hidden = e.Data == nil
switch s.kind {
case reflect.String:
s.data = castTemplate[T](e.Data.GetString())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s.data = castTemplate[T](e.Data.GetInt64())
case reflect.Bool:
s.data = castTemplate[T](e.Data.GetBool())
case reflect.Float32, reflect.Float64:
s.data = castTemplate[T](e.Data.GetFloat64())
default:
return fmt.Errorf("sliceData.Assign: can't assign primary type %T to %T", e.Data.GetData(), s.data)
}
} else {
err = unmarshal.Element(e, &s.data)
}
if err != nil {
return err
}
s.UpdateStringRepresentation()
return err
}
func (s *sliceData[T]) GetPath() string {
return ""
}
func (s *sliceData[T]) SetAttributes(attributes attributes.Attributes) {
s.attr = attributes
}
func (s *sliceData[T]) GetAttributes() attributes.Attributes {
return s.attr
}
func (s *sliceData[T]) UpdateStringRepresentation() {
t := reflect.TypeOf(s.data)
// We check if the type T implements the interface fmt.Stringer and has a
// custom String() method.
if ok := t.Implements(reflect.TypeOf(new(fmt.Stringer)).Elem()); ok {
var m reflect.Method
// If it's the case, we get the method String() of the type T and
// call it.
if m, ok = t.MethodByName("String"); ok {
s.str = m.Func.Call([]reflect.Value{reflect.ValueOf(s.data)})[0].String()
}
} else {
// Otherwise we use a basic string representation.
s.str = fmt.Sprintf("'%+v'", s.data)
}
}
func (s *sliceData[T]) String() string {
return s.str
}
func (s *sliceData[T]) GetXMLTag() []byte {
return nil
}
func (s *sliceData[T]) TransformToXML(b *saver.Buffer) error {
//log.Printf("sliceData.TransformToXML => %v (? %T) [Hidden: %v | %v]", s.tag, s.data, s.hidden, s.attr)
//log.Printf("Is hidden: %v (%T)", s.hidden, s.data)
_, okEmpty := any(s.data).(*primary.Empty)
elem, okElem := any(s.data).(*xml.Element)
if implFieldValidating, ok := castToInterface[_interface.FieldValidator](s.data); ok && implFieldValidating.CountValidatedField() == 0 || okEmpty || okElem && elem.IsEmpty() {
b.WriteEmptyTag(s.tag, s.attr)
return nil
}
b.OpenTag(s.tag, s.attr)
if err := xmlFile.Save(s.data, b, ""); err != nil {
return err
}
if utils.IsReflectPrimaryType(s.kind) {
if s.kind == reflect.String && strings.Contains(reflect.ValueOf(s.data).String(), "\n") {
b.CloseTagWithIndent(s.tag)
}
b.CloseTag(s.tag)
return nil
}
b.CloseTagWithIndent(s.tag)
return nil
}
// Slice is a slice of data that is represented by sliceData.
// The main difference with a normal slice is that it can get and set attributes.
// Like a normal slice, the slice is a slice of T and you can iterate over it.
type Slice[T any] struct {
data []sliceData[T]
attr attributes.Attributes
repeatingTag string
name string
cap int
}
func (s *Slice[T]) TransformToXML(b *saver.Buffer) error {
//if s.repeatingTag == "" {
// log.Print("Slice.TransformToXML: No repeating tag specified.")
// return nil
//}
// log.Printf("Name: '%v' w/ cap %d w/ %s / %v", s.name, s.cap, s.repeatingTag, s.data)
if s.cap == 0 {
return xmlFile.ErrEmptyValue
}
for _, v := range s.data {
if err := v.TransformToXML(b); err != nil {
return err
}
_, _ = b.Write([]byte("\n"))
}
//b.WriteString("\n")
//b.CloseTagWithIndent(s.repeatingTag)
//b.WriteString("\n")
return nil
}
func (s *Slice[T]) GetXMLTag() []byte {
return []byte(s.repeatingTag)
}
func (s *Slice[T]) Capacity() int {
return s.cap
}
func (s *Slice[T]) Set(value T, attr attributes.Attributes, idx int) {
old := s.data[idx]
if attr == nil {
attr = old.attr
}
if idx >= s.cap || idx < 0 {
panic("Slice index out of bounds")
}
d := sliceData[T]{
data: value,
attr: attr,
tag: old.tag,
kind: old.kind,
}
d.UpdateStringRepresentation()
if utils.IsReflectPrimaryType(d.kind) {
d.hidden = true
}
}
func (s *Slice[T]) Add(value T, attr attributes.Attributes) {
d := sliceData[T]{
data: value,
attr: attr,
tag: s.repeatingTag,
kind: reflect.TypeOf(value).Kind(),
}
d.UpdateStringRepresentation()
if utils.IsReflectPrimaryType(d.kind) {
d.hidden = true
}
s.data = append(s.data, d)
}
func (s *Slice[T]) At(idx int) T {
if idx < 0 || idx >= len(s.data) {
panic("out of bound/At: overflow/underflow")
}
return s.data[idx].data
}
func (s *Slice[T]) Remove(idx int) {
if idx < 0 || idx >= len(s.data) {
panic("out of bound/Remove: overflow/underflow")
}
if idx == s.cap-1 {
s.data = s.data[:idx]
} else {
s.data = append(s.data[:idx], s.data[idx+1:]...)
}
s.cap--
}
func (s *Slice[T]) Reset() {
s.cap = 0
s.data = make([]sliceData[T], 0)
}
func (s *Slice[T]) Assign(e *xml.Element) error {
if s.cap == 0 {
s.data = make([]sliceData[T], 0)
}
n := e
if n == nil {
log.Printf("n is nil")
return nil
}
if n.Parent != nil {
s.name = n.Parent.GetName()
//log.Printf("Slice.Assign: Parent is %s", n.Parent.GetName())
} else {
log.Printf("Slice.Assign: Assigning to slice without parent")
s.name = "unknown"
}
s.repeatingTag = n.GetName()
//if !strings.Contains(reflect.TypeOf(zero[T]()).Name(), "types.Slice") {
// for n.Child != nil && n.Child.Child != nil {
// n = n.Child
// }
//}
//log.Printf("Assigning: %v / %v", e.XMLPath(), e.Attr)
for n != nil {
sd := sliceData[T]{
tag: n.GetName(),
}
// Set sd.data to zero depending on the type of T. Either a pointer or a
// value.
//log.Printf("Type: %v / %v", reflect.TypeOf(sd.data).Kind(), n.Child)
switch tType := reflect.TypeOf(sd.data).Kind(); tType {
case reflect.Ptr, reflect.Interface, reflect.Struct, reflect.Map, reflect.Slice:
sd.data = reflect.New(reflect.TypeOf(*new(T)).Elem()).Interface().(T)
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
sd.data = zero[T]()
}
//log.Printf("Child ? %v", n.Child != nil)
if n.Child != nil {
if err := unmarshal.Element(n.Child, &sd); err != nil {
return err
}
} else {
if err := unmarshal.Element(n, &sd); err != nil {
return err
}
}
//log.Printf("Slice.Assign: %+v", sd.data)
sd.SetAttributes(n.Attr)
s.data = append(s.data, sd)
n = n.Next
}
s.cap = len(s.data)
//log.Printf("Slice.Assign: end => %s", s)
//for _, d := range s.data {
// log.Printf("Slice.Assign: %v", d)
//}
return nil
}
func (s *Slice[T]) String() string {
b := strings.Builder{}
b.WriteString("[")
for i, d := range s.data {
if i > 0 {
b.WriteString(", " + d.String())
} else {
b.WriteString(d.String())
}
}
b.WriteString("]")
return b.String()
}
func (s *Slice[T]) GetPath() string {
// We avoid to use GetPath in generics types like Slice or Map
return ""
}
func (s *Slice[T]) SetAttributes(attributes attributes.Attributes) {
s.attr = attributes
}
func (s *Slice[T]) GetAttributes() attributes.Attributes {
return s.attr
}
func (s *Slice[T]) ValidateField(_ string) {
}
func (s *Slice[T]) IsValidField(_ string) bool {
return true
}
func (s *Slice[T]) CountValidatedField() int {
return s.cap
}