-
Notifications
You must be signed in to change notification settings - Fork 67
/
enum.go
65 lines (55 loc) · 1.11 KB
/
enum.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
package zed
import (
"errors"
"strings"
"github.com/brimdata/zed/zcode"
)
type TypeEnum struct {
id int
Symbols []string
}
func NewTypeEnum(id int, symbols []string) *TypeEnum {
return &TypeEnum{id, symbols}
}
func (t *TypeEnum) ID() int {
return t.id
}
func (t *TypeEnum) Symbol(index int) (string, error) {
if index < 0 || index >= len(t.Symbols) {
return "", ErrEnumIndex
}
return t.Symbols[index], nil
}
func (t *TypeEnum) Lookup(symbol string) int {
for k, s := range t.Symbols {
if s == symbol {
return k
}
}
return -1
}
func (t *TypeEnum) Marshal(zv zcode.Bytes) (interface{}, error) {
return TypeUint64.Marshal(zv)
}
func (t *TypeEnum) String() string {
var b strings.Builder
b.WriteByte('<')
for k, s := range t.Symbols {
if k > 0 {
b.WriteByte(',')
}
b.WriteString(QuotedName(s))
}
b.WriteByte('>')
return b.String()
}
func (t *TypeEnum) Format(zv zcode.Bytes) string {
id, err := DecodeUint(zv)
if id >= uint64(len(t.Symbols)) || err != nil {
if err == nil {
err = errors.New("enum index out of range")
}
return badZng(err, t, zv)
}
return t.Symbols[id]
}