-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
field_slice.go
71 lines (63 loc) · 1.18 KB
/
field_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
package parco
import "io"
type (
BasicSliceField[T, U any] struct {
id string
header IntType
inner SliceType[U]
setter Setter[T, SliceView[U]]
getter Getter[T, SliceView[U]]
}
)
func (s BasicSliceField[T, U]) ID() string {
return s.id
}
func (s BasicSliceField[T, U]) Parse(item *T, r io.Reader) error {
values, err := s.inner.Parse(r)
if err != nil {
return err
}
s.setter(item, values.Unwrap())
return nil
}
func (s BasicSliceField[T, U]) Compile(item *T, w io.Writer) error {
value := s.getter(item)
return s.inner.Compile(value, w)
}
func SliceField[T, U any](
header IntType,
inner Type[U],
setter Setter[T, SliceView[U]],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return BasicSliceField[T, U]{
header: header,
inner: Slice[U](header, inner),
setter: setter,
getter: getter,
}
}
func SliceFieldGetter[T any, U any](
header IntType,
inner Type[U],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return SliceField[T, U](
header,
inner,
nil,
getter,
)
}
func SliceFieldSetter[T, U any](
header IntType,
inner Type[U],
setter Setter[T, SliceView[U]],
) Field[T, U] {
return SliceField[T, U](
header,
inner,
setter,
nil,
)
}