/
tag.go
146 lines (122 loc) · 2.89 KB
/
tag.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2023 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cuegen
import (
"reflect"
"strings"
)
type tagOptions struct {
// basic
Name string
Inline bool
Optional bool
// extended
Default *string // nil means no default value
Enum []string
}
// TODO(iyear): be customizable
const (
basicTag = "json" // same as json tag
extTag = "cue" // format: cue:"key1:value1;key2:value2;boolValue1;boolValue2"
)
func (g *Generator) parseTag(tag string) *tagOptions {
if tag == "" {
return &tagOptions{}
}
name, opts := parseTag(reflect.StructTag(tag).Get(basicTag))
ext := parseExtTag(reflect.StructTag(tag).Get(extTag))
return &tagOptions{
Name: name,
Inline: opts.Has("inline"),
Optional: opts.Has("omitempty"),
Default: ext.GetX("default"),
Enum: unescapeSplit(ext.Get("enum"), ","),
}
}
type basicTagOptions string
func parseTag(tag string) (string, basicTagOptions) {
tag, opt, _ := strings.Cut(tag, ",")
return tag, basicTagOptions(opt)
}
func (o basicTagOptions) Has(opt string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var name string
name, s, _ = strings.Cut(s, ",")
if name == opt {
return true
}
}
return false
}
func parseExtTag(str string) extTagOptions {
settings := map[string]string{}
if str == "" {
return settings
}
pairs := unescapeSplit(str, ";")
for _, pair := range pairs {
switch kv := unescapeSplit(pair, ":"); len(kv) {
case 1:
settings[kv[0]] = ""
case 2:
settings[kv[0]] = kv[1]
default:
// ignore invalid pair
}
}
return settings
}
func unescapeSplit(str string, sep string) []string {
if str == "" {
return []string{}
}
ss := strings.Split(str, sep)
for i := 0; i < len(ss); i++ {
j := i
if len(ss[j]) > 0 {
for {
if ss[j][len(ss[j])-1] == '\\' && i+1 < len(ss) {
i++
ss[j] = ss[j][0:len(ss[j])-1] + sep + ss[i]
ss[i] = ""
} else {
break
}
}
}
}
// filter empty strings
res := make([]string, 0, len(ss))
for _, s := range ss {
if s != "" {
res = append(res, s)
}
}
return res
}
type extTagOptions map[string]string
// GetX returns the value of the key if it exists, otherwise nil.
func (e extTagOptions) GetX(key string) *string {
if v, ok := e[key]; ok {
return &v
}
return nil
}
// Get returns the value of the key if it exists, otherwise "".
func (e extTagOptions) Get(key string) string {
return e[key]
}