/
tuple.go
184 lines (164 loc) · 4.2 KB
/
tuple.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package sb
import (
"fmt"
"io"
"reflect"
"github.com/reusee/e5"
)
type Tuple []any
var _ SBMarshaler = Tuple{}
func (t Tuple) MarshalSB(ctx Ctx, cont Proc) Proc {
return MarshalAsTuple(ctx, t, cont)
}
func MarshalAsTuple(ctx Ctx, tuple []any, cont Proc) Proc {
var marshal Proc
marshal = func(token *Token) (Proc, error) {
if len(tuple) == 0 {
token.Kind = KindTupleEnd
return cont, nil
}
value := tuple[0]
tuple = tuple[1:]
return ctx.Marshal(
ctx,
reflect.ValueOf(value),
marshal,
), nil
}
return func(token *Token) (Proc, error) {
token.Kind = KindTuple
return marshal, nil
}
}
var _ SBUnmarshaler = new(Tuple)
func (t *Tuple) UnmarshalSB(ctx Ctx, cont Sink) Sink {
return func(token *Token) (Sink, error) {
if token.Invalid() {
return nil, we.With(WithPath(ctx), e5.With(io.ErrUnexpectedEOF))(UnmarshalError)
}
if token.Kind != KindTuple {
return nil, we.With(WithPath(ctx), e5.With(TypeMismatch(token.Kind, reflect.Func)))(UnmarshalError)
}
return t.unmarshal(0, ctx, cont), nil
}
}
func (t *Tuple) unmarshal(i int, ctx Ctx, cont Sink) Sink {
return func(token *Token) (Sink, error) {
if token.Invalid() {
return nil, we.With(WithPath(ctx), e5.With(io.ErrUnexpectedEOF))(UnmarshalError)
}
if token.Kind == KindTupleEnd {
return cont, nil
}
if i < len(*t) {
var elem reflect.Value
if (*t)[i] != nil {
elem = reflect.New(reflect.TypeOf((*t)[i]))
elem.Elem().Set(reflect.ValueOf((*t)[i]))
} else {
var value any
elem = reflect.ValueOf(&value)
}
return ctx.Unmarshal(
ctx.WithPath(i),
elem,
func(token *Token) (Sink, error) {
(*t)[i] = elem.Elem().Interface()
return t.unmarshal(i+1, ctx, cont)(token)
},
)(token)
} else {
var value any
return ctx.Unmarshal(
ctx.WithPath(i),
reflect.ValueOf(&value),
func(token *Token) (Sink, error) {
*t = append(*t, value)
return t.unmarshal(i+1, ctx, cont)(token)
},
)(token)
}
}
}
func TupleTypes(typeSpec any) []reflect.Type {
var types []reflect.Type
spec := reflect.ValueOf(typeSpec)
specType := spec.Type()
switch specType.Kind() {
case reflect.Func:
for i := 0; i < specType.NumIn(); i++ {
types = append(types, specType.In(i))
}
case reflect.Struct:
for i := 0; i < specType.NumField(); i++ {
types = append(types, specType.Field(i).Type)
}
default: // NOCOVER
panic(fmt.Errorf("bad type: %T", typeSpec))
}
return types
}
func UnmarshalTupleTyped(ctx Ctx, types []reflect.Type, target *Tuple, cont Sink) Sink {
return func(token *Token) (Sink, error) {
if token.Invalid() {
return nil, we.With(WithPath(ctx), e5.With(io.ErrUnexpectedEOF))(UnmarshalError)
}
if token.Kind != KindTuple {
return nil, we.With(WithPath(ctx), e5.With(TypeMismatch(token.Kind, reflect.Func)))(UnmarshalError)
}
return unmarshalTupleTyped(ctx, types, target, cont), nil
}
}
type TypedTuple struct {
Types []reflect.Type
Values Tuple
}
var _ SBUnmarshaler = new(TypedTuple)
func (t *TypedTuple) UnmarshalSB(ctx Ctx, cont Sink) Sink {
return UnmarshalTupleTyped(ctx, t.Types, &t.Values, cont)
}
func unmarshalTupleTyped(ctx Ctx, types []reflect.Type, target *Tuple, cont Sink) Sink {
var sink Sink
i := 0
sink = func(token *Token) (Sink, error) {
if token.Invalid() {
return nil, we.With(WithPath(ctx), e5.With(io.ErrUnexpectedEOF))(UnmarshalError)
}
if token.Kind == KindTupleEnd {
if i != len(types) {
return nil, we.With(WithPath(ctx), e5.With(TooFewElement))(UnmarshalError)
}
return cont, nil
}
if i == len(types) {
return nil, we.With(WithPath(ctx), e5.With(TooManyElement))(UnmarshalError)
}
if i < len(*target) {
elem := reflect.New(types[i])
if (*target)[i] != nil {
elem.Elem().Set(reflect.ValueOf((*target)[i]))
}
return ctx.Unmarshal(
ctx.WithPath(i),
elem,
func(token *Token) (Sink, error) {
(*target)[i] = elem.Elem().Interface()
i++
return sink(token)
},
)(token)
} else {
elem := reflect.New(types[i])
return ctx.Unmarshal(
ctx.WithPath(i),
elem,
func(token *Token) (Sink, error) {
*target = append(*target, elem.Elem().Interface())
i++
return sink(token)
},
)(token)
}
}
return sink
}