-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.tmp
187 lines (174 loc) · 3.95 KB
/
type.tmp
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
185
186
187
package nbt
// GENERATED CODE: Do not edit. See taggen/main.go and tag.go.
import (
"fmt"
"io"
)
{{range . -}}
// {{.}} represents the NBT type TAG_{{.}}
// Type() tells you that {{.}} represents Type{{.}}.
func ({{.}}) Type() Type { return Type{{.}} }
{{if ne . "End" -}}
// Get{{.}} performs a type-assertion that n is of type Type{{.}}. If
// it is, and there is a payload, you get the results of a type-assertion
// of payload to {{.}}, otherwise you get a zero-valued {{.}} and ok is
// false.
func Get{{.}}(t Tag) (out {{.}}, ok bool) {
if t.Type() != Type{{.}} {
return out, false
}
out, ok = t.({{.}})
return out, ok
}
// Get{{.}}List performs a type-assertion that l is a list of {{.}},
// and returns the corresponding slice.
func (l List) Get{{.}}List() (out []{{.}}, ok bool) {
if l.Contents != Type{{.}} {
return out, false
}
out, ok = l.data.([]{{.}})
return out, ok
}
// Make{{.}}List creates a list of the appropriate type of payload.
func Make{{.}}List(in []{{.}}) (l List) {
l.Contents = Type{{.}}
l.data = in
return l
}
{{else}}{{/* End is a special case; nothing to type-assert. */ -}}
func GetEnd(t Tag) (out End, ok bool) {
if t.Type() != TypeEnd {
return out, false
}
return out, true
}
{{end}}
{{end}}
func (l List) storeData(w io.Writer) (err error) {
switch raw := l.data.(type) {
{{range . -}}
{{if ne . "End"}}
case []{{.}}:
count := len(raw)
for i := 0; i < count; i++ {
err = raw[i].store(w)
if err != nil {
return err
}
}
{{else}}
case []End: // no data to store
return nil
{{end}}
{{- end}}
default:
return fmt.Errorf("unhandled tag type in List.storeData: %v", l.Contents)
}
return nil
}
// loadData loads the "raw" data array, which we'll later use to build
// the interface array.
func (l *List) loadData(r io.Reader, count int) (err error) {
switch l.Contents {
{{range . -}}
{{if ne . "End"}}
case Type{{.}}:
raw := make([]{{.}}, count)
for i := 0; i < count; i++ {
raw[i], err = load{{.}}(r)
if err!= nil {
raw = raw[:i]
break
}
}
l.data = raw
return err
{{else}}
case TypeEnd: // nothing to load
l.data = nil
return nil
{{end}}
{{- end}}
default:
return fmt.Errorf("unhandled tag type in List.loadData: %v", l.Contents)
}
}
// Iterate iterates over the list, passing each item in the list (as a Payload)
// to the given function. If fn returns a non-nil error, Iterate stops and returns
// the error.
func (l List) Iterate(fn func(int, Tag) error) (err error) {
switch raw := l.data.(type) {
{{range .}}
case []{{.}}:
count := len(raw)
for i := 0; i < count; i++ {
{{if ne . "End" -}}
err = fn(i, raw[i])
{{else}}
err = fn(i, End{})
{{- end}}
if err != nil {
break
}
}
{{- end}}
default:
return fmt.Errorf("unhandled tag type in List.Iterate: %v", l.Contents)
}
return err
}
// Length returns the length of the list, if applicable. Note, a list of End
// is (I think) always of length 0, if it's even valid at all.
func (l List) Length() int {
switch raw := l.data.(type) {
{{range .}}
case []{{.}}:
{{ if ne . "End" }}
return len(raw)
{{else}}
return 0
{{end}}
{{- end}}
default:
return 0
}
}
// MakeList makes a list given a slice of any kind of payload object. Note,
// not a slice of Tags, a slice of any of the specific concrete types
// implement tag and aren't End.
func MakeList(in interface{}) (l List, err error) {
switch in.(type) {
{{range .}}
case []{{.}}:
{{- if ne . "End" }}
l.Contents = Type{{.}}
l.data = in
{{- else}}
// We don't allow non-empty lists of Ends
l.Contents = TypeEnd
l.data = nil
{{- end}}
return l, err
{{- end}}
default:
return l, fmt.Errorf("can't MakeList on %T", in)
}
}
// Element gives the ith element of l.
func (l List)Element(i int) (out Tag, ok bool) {
switch data := l.data.(type) {
{{range .}}
case []{{.}}:
{{- if ne . "End" }}
if i > 0 && i < len(data) {
return data[i], true
}
return nil, false
{{- else}}
return End{}, false
{{- end}}
{{- end}}
default:
return nil, false
}
}