-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfield_array.go
70 lines (62 loc) · 1.15 KB
/
field_array.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
package parco
import "io"
type (
BasicArrayField[T, U any] struct {
id string
size int
inner ArrayType[U]
setter Setter[T, SliceView[U]]
getter Getter[T, SliceView[U]]
}
)
func (s BasicArrayField[T, U]) ID() string {
return s.id
}
func (s BasicArrayField[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 BasicArrayField[T, U]) Compile(item *T, w io.Writer) error {
value := s.getter(item)
return s.inner.Compile(value, w)
}
func ArrayField[T, U any](
length int,
inner Type[U],
setter Setter[T, SliceView[U]],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return BasicArrayField[T, U]{
inner: Array[U](length, inner),
setter: setter,
getter: getter,
}
}
func ArrayFieldGetter[T any, U any](
length int,
inner Type[U],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return ArrayField[T, U](
length,
inner,
nil,
getter,
)
}
func ArrayFieldSetter[T, U any](
length int,
inner Type[U],
setter Setter[T, SliceView[U]],
) Field[T, U] {
return ArrayField[T, U](
length,
inner,
setter,
nil,
)
}