-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
46 lines (39 loc) · 894 Bytes
/
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
package astconf
import (
"reflect"
)
type sliceEncoder struct {
elemEnc encoderFunc
}
func newSliceEncoder(elemEnc encoderFunc) encoderFunc {
enc := sliceEncoder{elemEnc: elemEnc}
return enc.encode
}
func (s sliceEncoder) encode(v reflect.Value, e *Encoder) error {
for i, n := 0, v.Len(); i < n; i++ {
e := e.derive()
if err := s.elemEnc(v.Index(i), e); err != nil {
return err
}
}
return nil
}
type commaSeparatedSliceEncoder struct {
elemEnc encoderFunc
}
func newCommaSeparatedSliceEncoder(elemEnc encoderFunc) encoderFunc {
enc := commaSeparatedSliceEncoder{elemEnc: elemEnc}
return enc.encode
}
func (s commaSeparatedSliceEncoder) encode(v reflect.Value, e *Encoder) error {
out := e.Printer()
for i, n := 0, v.Len(); i < n; i++ {
if i > 0 {
out.Write([]byte(","))
}
if err := s.elemEnc(v.Index(i), e); err != nil {
return err
}
}
return nil
}