-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_entry.go
155 lines (137 loc) · 2.98 KB
/
pre_entry.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
// Copyright 2021 The June Authors. All rights reserved.
// Use of this source code is governed by Apache License
// that can be found in the LICENSE file.
package index
import (
"fmt"
"io"
"reflect"
"github.com/reusee/sb"
)
type PreEntry struct {
Key Key
Type any
Tuple sb.Tuple
}
func NewPreEntry(key Key, indexType any, args ...any) PreEntry {
var types []reflect.Type
v, ok := specsByType.Load(reflect.TypeOf(indexType))
if ok {
types = v.(Spec).Fields
}
entry := PreEntry{
Key: key,
Type: indexType,
}
if len(types) > 0 {
if len(args) <= len(types) {
entry.Tuple = args
} else {
panic(fmt.Errorf("bad argument count: %T", indexType))
}
} else {
entry.Tuple = args
}
return entry
}
func (e PreEntry) Clone() PreEntry {
newEntry := PreEntry{
Key: e.Key,
Type: e.Type,
}
if len(e.Tuple) > 0 {
tuple := make(sb.Tuple, len(e.Tuple))
copy(tuple, e.Tuple)
newEntry.Tuple = tuple
}
return newEntry
}
var _ sb.SBMarshaler = PreEntry{}
func (e PreEntry) MarshalSB(ctx sb.Ctx, cont sb.Proc) sb.Proc {
var name string
v, ok := specsByType.Load(reflect.TypeOf(e.Type))
if !ok {
name = nameOfIdxUnknown
} else {
name = v.(Spec).Name
}
tuple := append(sb.Tuple{e.Key, name}, e.Tuple...)
return ctx.Marshal(ctx,
reflect.ValueOf(tuple),
cont,
)
}
var _ sb.SBUnmarshaler = new(PreEntry)
func (e *PreEntry) UnmarshalSB(ctx sb.Ctx, cont sb.Sink) sb.Sink {
var name string
var types []reflect.Type
var entry PreEntry
var unmarshalTyped sb.Sink
unmarshalTyped = func(token *sb.Token) (sb.Sink, error) {
if token == nil {
return nil, we(io.ErrUnexpectedEOF)
}
if token.Kind == sb.KindTupleEnd {
*e = entry
return cont, nil
}
value := reflect.New(types[0])
types = types[1:]
return ctx.Unmarshal(
ctx,
value,
func(token *sb.Token) (sb.Sink, error) {
entry.Tuple = append(entry.Tuple, value.Elem().Interface())
return unmarshalTyped(token)
},
)(token)
}
var unmarshalUnknown sb.Sink
unmarshalUnknown = func(token *sb.Token) (sb.Sink, error) {
if token == nil {
return nil, we(io.ErrUnexpectedEOF)
}
if token.Kind == sb.KindTupleEnd {
*e = entry
return cont, nil
}
var value any
return ctx.Unmarshal(
ctx,
reflect.ValueOf(&value),
func(token *sb.Token) (sb.Sink, error) {
entry.Tuple = append(entry.Tuple, value)
return unmarshalUnknown(token)
},
)(token)
}
unmarshalName := ctx.Unmarshal(
ctx,
reflect.ValueOf(&name),
func(token *sb.Token) (sb.Sink, error) {
var spec Spec
v, ok := specsByName.Load(name)
if !ok {
entry.Type = idxUnknown(name)
return unmarshalUnknown(token)
} else {
spec = v.(Spec)
}
entry.Type = reflect.New(spec.Type).Elem().Interface()
types = spec.Fields
return unmarshalTyped(token)
},
)
unmarshalKey := ctx.Unmarshal(
ctx,
reflect.ValueOf(&entry.Key),
unmarshalName,
)
return sb.ExpectKind(
ctx,
sb.KindTuple,
unmarshalKey,
)
}
func (_ PreEntry) IsSelectOption() {}
func (_ PreEntry) IsIterOption() {}