-
Notifications
You must be signed in to change notification settings - Fork 4
/
enum.go
90 lines (77 loc) · 1.95 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
package generator
import (
"strings"
v3 "github.com/google/gnostic/openapiv3"
"google.golang.org/protobuf/reflect/protoreflect"
)
func enumKindSchema(field protoreflect.FieldDescriptor) *v3.SchemaOrReference {
list := enumsToV3Any(field)
s := &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{
Format: "enum",
Type: "string",
Enum: list,
},
},
}
return s
}
func enumsToV3Any(field protoreflect.FieldDescriptor, enumValues ...int32) []*v3.Any {
stringList := enumToStringSlice(field, enumValues...)
list := []*v3.Any{}
for _, v := range stringList {
n := &v3.Any{
Yaml: string(v),
}
list = append(list, n)
}
return list
}
func enumToStringSlice(field protoreflect.FieldDescriptor, enumValues ...int32) []string {
removeUnspecified := len(enumValues) == 0
list := []string{}
values := field.Enum().Values()
for i := 0; i < values.Len(); i++ {
if len(enumValues) == 0 || has(enumValues, int32(values.Get(i).Index())) {
v := values.Get(i)
// skip default unspecified values
if removeUnspecified && strings.HasSuffix(string(v.Name()), "_UNSPECIFIED") {
continue
}
list = append(list, string(v.Name()))
}
}
return list
}
// enumValues returns of list of enum ids for a given field
func enumValues(field protoreflect.FieldDescriptor, removeUnspecified bool) []int32 {
values := field.Enum().Values()
var list []int32
for i := 0; i < values.Len(); i++ {
v := values.Get(i)
// skip default unspecified values
if removeUnspecified && strings.HasSuffix(string(v.Name()), "_UNSPECIFIED") {
continue
}
list = append(list, int32(values.Get(i).Index()))
}
return list
}
func remove(list []int32, idxs ...int32) []int32 {
var filtered []int32
for _, v := range idxs {
if !has(list, v) {
filtered = append(filtered, int32(v))
}
}
return filtered
}
func has(list []int32, idx int32) bool {
for _, v := range list {
if v == idx {
return true
}
}
return false
}