-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
field_struct.go
85 lines (75 loc) · 1.32 KB
/
field_struct.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
package parco
import "io"
type (
structTypeI[T any] interface {
ParserType[T]
CompilerType[T]
}
structField[T, U any] struct {
id string
inner StructType[U]
setter Setter[T, U]
getter Getter[T, U]
}
)
func (s structField[T, U]) ID() string {
return s.id
}
func (s structField[T, U]) Parse(item *T, r io.Reader) error {
model, err := s.inner.Parse(r)
if err != nil {
return err
}
s.setter(item, model)
return nil
}
func (s structField[T, U]) Compile(item *T, w io.Writer) error {
value := s.getter(item)
return s.inner.Compile(value, w)
}
func newStructField[T, U any](
setter Setter[T, U],
getter Getter[T, U],
compiler CompilerType[U],
parser ParserType[U],
) Field[T, U] {
return structField[T, U]{
inner: StructParco[U](parser, compiler),
setter: setter,
getter: getter,
}
}
func StructField[T, U any](
getter Getter[T, U],
setter Setter[T, U],
inner structTypeI[U],
) Field[T, U] {
return newStructField[T, U](
setter,
getter,
inner,
inner,
)
}
func StructFieldGetter[T, U any](
getter Getter[T, U],
compiler CompilerType[U],
) Field[T, U] {
return newStructField[T, U](
nil,
getter,
compiler,
nil,
)
}
func StructFieldSetter[T, U any](
setter Setter[T, U],
parser ParserType[U],
) Field[T, U] {
return newStructField[T, U](
setter,
nil,
nil,
parser,
)
}