| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package generators |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"regexp" |
| 22 |
"sort" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"k8s.io/gengo/v2" |
| 26 |
"k8s.io/gengo/v2/generator" |
| 27 |
"k8s.io/gengo/v2/types" |
| 28 |
) |
| 29 |
|
| 30 |
const tagEnumType = "enum" |
| 31 |
const enumTypeDescriptionHeader = "Possible enum values:" |
| 32 |
|
| 33 |
type enumValue struct { |
| 34 |
Name string |
| 35 |
Value string |
| 36 |
Comment string |
| 37 |
} |
| 38 |
|
| 39 |
type enumType struct { |
| 40 |
Name types.Name |
| 41 |
Values []*enumValue |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
type enumMap map[types.Name]*enumType |
| 46 |
|
| 47 |
type enumContext struct { |
| 48 |
enumTypes enumMap |
| 49 |
} |
| 50 |
|
| 51 |
func newEnumContext(c *generator.Context) *enumContext { |
| 52 |
return &enumContext{enumTypes: parseEnums(c)} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func (ec *enumContext) EnumType(t *types.Type) (enum *enumType, isEnum bool) { |
| 59 |
|
| 60 |
if t.Kind == types.Pointer { |
| 61 |
t = t.Elem |
| 62 |
} |
| 63 |
enum, ok := ec.enumTypes[t.Name] |
| 64 |
return enum, ok |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
func (et *enumType) ValueStrings() []string { |
| 70 |
var values []string |
| 71 |
for _, value := range et.Values { |
| 72 |
|
| 73 |
values = append(values, fmt.Sprintf("%q", value.Value)) |
| 74 |
} |
| 75 |
sort.Strings(values) |
| 76 |
return values |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func (et *enumType) DescriptionLines() []string { |
| 85 |
if len(et.Values) == 0 { |
| 86 |
return nil |
| 87 |
} |
| 88 |
var lines []string |
| 89 |
for _, value := range et.Values { |
| 90 |
lines = append(lines, value.Description()) |
| 91 |
} |
| 92 |
sort.Strings(lines) |
| 93 |
|
| 94 |
return append([]string{"", enumTypeDescriptionHeader}, lines...) |
| 95 |
} |
| 96 |
|
| 97 |
func parseEnums(c *generator.Context) enumMap { |
| 98 |
|
| 99 |
stringType := c.Universe.Type(types.Name{Name: "string"}) |
| 100 |
|
| 101 |
|
| 102 |
enumTypes := make(enumMap) |
| 103 |
for _, p := range c.Universe { |
| 104 |
for _, t := range p.Types { |
| 105 |
if isEnumType(stringType, t) { |
| 106 |
if _, ok := enumTypes[t.Name]; !ok { |
| 107 |
enumTypes[t.Name] = &enumType{ |
| 108 |
Name: t.Name, |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
for _, p := range c.Universe { |
| 117 |
for _, c := range p.Constants { |
| 118 |
enumType := c.Underlying |
| 119 |
if _, ok := enumTypes[enumType.Name]; ok { |
| 120 |
value := &enumValue{ |
| 121 |
Name: c.Name.Name, |
| 122 |
Value: *c.ConstValue, |
| 123 |
Comment: strings.Join(c.CommentLines, " "), |
| 124 |
} |
| 125 |
enumTypes[enumType.Name].addIfNotPresent(value) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return enumTypes |
| 131 |
} |
| 132 |
|
| 133 |
func (et *enumType) addIfNotPresent(value *enumValue) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
for i, existing := range et.Values { |
| 138 |
if existing.Value == value.Value { |
| 139 |
|
| 140 |
|
| 141 |
if len(existing.Comment) < len(value.Comment) || (len(existing.Comment) == len(value.Comment) && existing.Comment > value.Comment) { |
| 142 |
et.Values[i] = value |
| 143 |
} |
| 144 |
|
| 145 |
return |
| 146 |
} |
| 147 |
} |
| 148 |
et.Values = append(et.Values, value) |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func (ev *enumValue) Description() string { |
| 155 |
comment := strings.TrimSpace(ev.Comment) |
| 156 |
|
| 157 |
comment = strings.TrimPrefix(comment, ev.Name) |
| 158 |
|
| 159 |
comment = strings.TrimSpace(comment) |
| 160 |
|
| 161 |
comment = whitespaceRegex.ReplaceAllString(comment, " ") |
| 162 |
return fmt.Sprintf(" - `%q` %s", ev.Value, comment) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func isEnumType(stringType *types.Type, t *types.Type) bool { |
| 169 |
return t.Kind == types.Alias && t.Underlying == stringType && hasEnumTag(t) |
| 170 |
} |
| 171 |
|
| 172 |
func hasEnumTag(t *types.Type) bool { |
| 173 |
return gengo.ExtractCommentTags("+", t.CommentLines)[tagEnumType] != nil |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
var whitespaceRegex = regexp.MustCompile(`\s+`) |