-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
field_map.go
76 lines (68 loc) · 1.31 KB
/
field_map.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
package parco
import "io"
type (
mapField[T any, K comparable, V any] struct {
id string
inner mapType[K, V]
setter Setter[T, map[K]V]
getter Getter[T, map[K]V]
pooler Pooler
}
)
func (s mapField[T, K, V]) ID() string {
return s.id
}
func (s mapField[T, K, V]) Parse(item *T, r io.Reader) error {
values, err := s.inner.Parse(r)
if err != nil {
return err
}
s.setter(item, values)
return nil
}
func (s mapField[T, K, V]) Compile(item *T, w io.Writer) error {
value := s.getter(item)
return s.inner.Compile(value, w)
}
func MapField[T any, K comparable, V any](
header IntType,
keyType Type[K],
valueType Type[V],
setter Setter[T, map[K]V],
getter Getter[T, map[K]V],
) Field[T, map[K]V] {
return mapField[T, K, V]{
inner: MapType[K, V](header, keyType, valueType),
setter: setter,
getter: getter,
pooler: SinglePool,
}
}
func MapFieldGetter[T any, K comparable, V any](
header IntType,
keyType Type[K],
valueType Type[V],
getter Getter[T, map[K]V],
) Field[T, map[K]V] {
return MapField[T, K, V](
header,
keyType,
valueType,
nil,
getter,
)
}
func MapFieldSetter[T any, K comparable, V any](
header IntType,
keyType Type[K],
valueType Type[V],
setter Setter[T, map[K]V],
) Field[T, map[K]V] {
return MapField[T, K, V](
header,
keyType,
valueType,
setter,
nil,
)
}