-
Notifications
You must be signed in to change notification settings - Fork 104
/
serializer.go
66 lines (54 loc) · 1.57 KB
/
serializer.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
package manta
import (
"fmt"
"strings"
)
type serializer struct {
name string
version int32
fields []*field
}
func (s *serializer) id() string {
return serializerId(s.name, s.version)
}
func (s *serializer) getNameForFieldPath(fp *fieldPath, pos int) []string {
return s.fields[fp.path[pos]].getNameForFieldPath(fp, pos+1)
}
func (s *serializer) getTypeForFieldPath(fp *fieldPath, pos int) *fieldType {
return s.fields[fp.path[pos]].getTypeForFieldPath(fp, pos+1)
}
func (s *serializer) getDecoderForFieldPath(fp *fieldPath, pos int) fieldDecoder {
index := fp.path[pos]
if len(s.fields) <= index {
_panicf("serializer %s: field path %s has no field (%d)", s.name, fp, index)
}
return s.fields[index].getDecoderForFieldPath(fp, pos+1)
}
func (s *serializer) getFieldForFieldPath(fp *fieldPath, pos int) *field {
return s.fields[fp.path[pos]].getFieldForFieldPath(fp, pos+1)
}
func (s *serializer) getFieldPathForName(fp *fieldPath, name string) bool {
for i, f := range s.fields {
if name == f.varName {
fp.path[fp.last] = i
return true
}
if strings.HasPrefix(name, f.varName+".") {
fp.path[fp.last] = i
fp.last++
return f.getFieldPathForName(fp, name[len(f.varName)+1:])
}
}
return false
}
func (s *serializer) getFieldPaths(fp *fieldPath, state *fieldState) []*fieldPath {
results := make([]*fieldPath, 0, 4)
for i, f := range s.fields {
fp.path[fp.last] = i
results = append(results, f.getFieldPaths(fp, state)...)
}
return results
}
func serializerId(name string, version int32) string {
return fmt.Sprintf("%s(%d)", name, version)
}