-
Notifications
You must be signed in to change notification settings - Fork 6
/
ctx.go
213 lines (193 loc) · 4.19 KB
/
ctx.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package ctx
import (
"fmt"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/ml444/gkit/cmd/protoc-gen-go-validate/funcs"
"github.com/ml444/gkit/cmd/protoc-gen-go-validate/v"
)
type ValidateCtx struct {
Imports []*ImportCtx
Messages []*MessageCtx
NeedWellKnow *NeedWellKnown
NeedCommon bool
ErrCodeBegin int32
}
type NeedWellKnown struct {
Email bool
Hostname bool
UUID bool
}
type ImportCtx struct {
Alias string
Path string
}
type MessageCtx struct {
Desc protoreflect.MessageDescriptor
TypeName string
Fields []*FieldCtx
Required bool
Disabled bool
Ignored bool
NonOneOfFields []*FieldCtx
OptionalFields []*FieldCtx
RealOneOfs map[string]*OneOfField
SubMessageCtxs []*MessageCtx
}
type OneOfField struct {
Field *protogen.Field
Fields []*FieldCtx
Name string
Type string
//Index int
Required bool
Skip bool
//TmplName string
//Err error
}
type FieldCtx struct {
//MessageDesc protoreflect.MessageDescriptor
Desc protoreflect.FieldDescriptor
Field *protogen.Field
Rules proto.Message
Pkg string
Name string
Type string
Index int
OnKey string
Required bool
Skip bool
Wrap string
TmplName string
Err error
Accessor string
ErrCode int32
}
func (fc *FieldCtx) FullName() string {
isEnum := fc.Desc.Kind() == protoreflect.EnumKind
if isEnum {
return funcs.EnumName(fc.Desc.Enum())
}
if fc.Pkg != "" {
return fc.Pkg + "." + fc.Name
}
return fc.Name
}
// ImpCtx returns the import package name of the field
func (fc *FieldCtx) ImpCtx() *ImportCtx {
if len(funcs.ExtraPkg) == 0 {
funcs.GetImports(funcs.FileDescriptor(fc.Desc.Parent()))
}
var parent protoreflect.Descriptor
switch fc.Desc.Kind() {
case protoreflect.MessageKind:
parent = fc.Desc.Message().Parent()
case protoreflect.EnumKind:
parent = fc.Desc.Enum().Parent()
default:
return nil
}
for {
message, ok := parent.(protoreflect.MessageDescriptor)
if ok {
parent = message.Parent()
} else {
if pkgName, ok := funcs.ExtraPkg[string(parent.FullName())]; ok {
fc.Pkg = pkgName
return &ImportCtx{
Alias: pkgName,
Path: funcs.ExtraPkgPath[pkgName],
}
}
break
}
}
return nil
}
func (fc *FieldCtx) GetTmplName() (string, error) {
if fc.Err != nil {
return "", fc.Err
}
return fc.TmplName, nil
}
func (fc *FieldCtx) Elem(def string) (elem FieldCtx, err error) {
elem = *fc
elem.Accessor = def
var rules *v.FieldRules
switch r := fc.Rules.(type) {
case *v.MapRules:
rules = r.GetValues()
case *v.RepeatedRules:
rules = r.GetItems()
default:
err = fmt.Errorf("cannot get Elem from %s", fc.Field.GoName)
return
}
rType, rIns, mRule, wrap := ResolveRules(fc.Desc, rules)
elem.Rules = rIns
if wrap {
elem.Wrap = rType
elem.TmplName = "wrapper"
} else {
elem.TmplName = rType
}
if mRule != nil {
if mRule.Required != nil {
elem.Required = *mRule.Required
}
if mRule.Skip != nil {
elem.Skip = *mRule.Skip
}
}
return
}
func (fc *FieldCtx) Unwrap(name string) (out FieldCtx, err error) {
if fc.TmplName != "wrapper" {
err = fmt.Errorf("cannot unwrap non-wrapper type %q", fc.TmplName)
return
}
return FieldCtx{
Field: fc.Field,
Rules: fc.Rules,
TmplName: fc.Wrap,
Accessor: name,
}, nil
}
func (fc FieldCtx) GetAccessor() string {
if fc.Accessor != "" {
if fc.Accessor == "wrapper" {
return "wrapper.GetValue()"
}
return fc.Accessor
}
return fmt.Sprintf("m.Get%s()", fc.Field.GoName)
}
func (fc *FieldCtx) SetAccessor(def string) *FieldCtx {
fc.Accessor = def
return fc
}
func (fc *FieldCtx) MapTypeName(field *protogen.Field, desc protoreflect.FieldDescriptor, rule *v.FieldRules) *FieldCtx {
rType, rIns, mRule, wrap := ResolveRules(desc, rule)
elem := FieldCtx{
Desc: desc,
Field: field,
Rules: rIns,
TmplName: rType,
}
if wrap {
elem.Wrap = rType
elem.TmplName = "wrapper"
} else {
elem.TmplName = rType
}
if mRule != nil {
if mRule.Required != nil {
elem.Required = *mRule.Required
}
if mRule.Skip != nil {
elem.Skip = *mRule.Skip
}
}
return &elem
}