-
Notifications
You must be signed in to change notification settings - Fork 6
/
enum.go
105 lines (86 loc) · 2.09 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
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
package types
import (
"fmt"
"github.com/gowebapi/webidlparser/ast"
)
// Enum type
type Enum struct {
standardType
basic BasicInfo
Values []EnumValue
// target language prefix and suffix for enum values
Prefix, Suffix string
}
// Enum need to implement Type
var _ Type = &Enum{}
// EnumValue is a single enum value
type EnumValue struct {
MethodName
}
func (t *extractTypes) convertEnum(in *ast.Enum) *Enum {
ref := createRef(in, t)
t.warningTrue(len(in.Annotations) == 0, ref, "unsupported annotation")
ret := &Enum{
standardType: standardType{
ref: ref,
needRelease: false,
},
basic: fromIdlToTypeName(t.main.setup.Package, in.Name, "enum"),
Values: []EnumValue{},
}
ret.Suffix = ret.basic.Def
for i, v := range in.Values {
if b, ok := v.(*ast.BasicLiteral); ok {
idl := b.Value
idl = clipString(idl)
lang := idl
if lang == "" {
lang = fmt.Sprintf("empty_string_%d", i)
}
lang = convertIntoValidVariableName(lang)
ret.Values = append(ret.Values, EnumValue{
MethodName: MethodName{
Idl: idl,
Def: fixLangName(toCamelCase(lang, true)),
},
})
} else {
t.failing(ref, "unsupported literal: %T: %#V", v, v)
}
}
return ret
}
func (t *Enum) Basic() BasicInfo {
return TransformBasic(t, t.basic)
}
func (t *Enum) DefaultParam() (info *TypeInfo, inner TypeRef) {
return t.Param(false, false, false)
}
func (t *Enum) key() string {
return t.basic.Idl
}
func (t *Enum) lessThan(b *Enum) bool {
return t.basic.lessThan(&b.basic)
}
func (t *Enum) link(conv *Convert, inuse inuseLogic) TypeRef {
t.inuse = true
return t
}
func (t *Enum) Param(nullable, option, variadic bool) (info *TypeInfo, inner TypeRef) {
return newTypeInfo(t.Basic(), nullable, option, variadic, false, false, false), t
}
func (t *Enum) TypeID() TypeID {
return TypeEnum
}
func (t *Enum) SetBasic(basic BasicInfo) {
t.basic = basic
}
func (t *EnumValue) Name() *MethodName {
return &t.MethodName
}
func (t *EnumValue) GetType() TypeRef {
panic("unsupported")
}
func (t *EnumValue) SetType(value TypeRef) string {
return "enum values can't change type"
}