-
Notifications
You must be signed in to change notification settings - Fork 10
/
enums.go
62 lines (51 loc) · 1.19 KB
/
enums.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
package main
import (
"github.com/32leaves/bel"
)
// StringEnum is an enumeration with string values
type StringEnum string
const (
// OptionOne is an enum value
OptionOne StringEnum = "option-one"
// OptionTwo is an enum value
OptionTwo StringEnum = "option-two"
// OptionThree is an enum value
OptionThree StringEnum = "option-three"
)
// UintEnum demonstrates which kind of enums do not work
// For the enum detection to work the values need to be given explicitly.
// This UintEnum will not work with bel
type UintEnum uint32
const (
// OtherOptA is an enum value
OtherOptA UintEnum = iota
// OtherOptB is an enum value
OtherOptB
)
// SomeStruct uses enumerations
type SomeStruct struct {
ThisOneWorks StringEnum
ThisOneDoesnt UintEnum
}
// ExtractEnums demonstrates how to use an enum handler
func ExtractEnums() {
handler, err := bel.NewParsedSourceEnumHandler(".")
if err != nil {
panic(err)
}
ts, err := bel.Extract(SomeStruct{}, bel.WithEnumerations(handler))
if err != nil {
panic(err)
}
err = bel.Render(ts)
if err != nil {
panic(err)
}
err = bel.Render(ts, bel.GenerateEnumAsSumType)
if err != nil {
panic(err)
}
}
func init() {
examples["enums"] = ExtractEnums
}