forked from bufbuild/protoc-gen-validate
/
context.go
186 lines (160 loc) · 4.75 KB
/
context.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
package shared
import (
"bytes"
"fmt"
"text/template"
"github.com/golang/protobuf/proto"
"github.com/lyft/protoc-gen-star"
"github.com/lyft/protoc-gen-validate/gogoproto"
"github.com/lyft/protoc-gen-validate/validate"
)
type RuleContext struct {
Field pgs.Field
Rules proto.Message
Gogo Gogo
Typ string
WrapperTyp string
OnKey bool
Index string
AccessorOverride string
}
type Gogo struct {
Nullable bool
StdDuration bool
StdTime bool
}
func rulesContext(f pgs.Field) (out RuleContext, err error) {
out.Field = f
out.Gogo.Nullable = true
f.Extension(gogoproto.E_Nullable, &out.Gogo.Nullable)
f.Extension(gogoproto.E_Stdduration, &out.Gogo.StdDuration)
f.Extension(gogoproto.E_Stdtime, &out.Gogo.StdTime)
var rules validate.FieldRules
if _, err = f.Extension(validate.E_Rules, &rules); err != nil {
return
}
var wrapped bool
if out.Typ, out.Rules, wrapped = resolveRules(f.Type(), &rules); wrapped {
out.WrapperTyp = out.Typ
out.Typ = "wrapper"
}
if out.Typ == "error" {
err = fmt.Errorf("unknown rule type (%T)", rules.Type)
}
return
}
func (ctx RuleContext) Key(name, idx string) (out RuleContext, err error) {
rules, ok := ctx.Rules.(*validate.MapRules)
if !ok {
err = fmt.Errorf("cannot get Key RuleContext from %T", ctx.Field)
return
}
out.Field = ctx.Field
out.AccessorOverride = name
out.Index = idx
out.Typ, out.Rules, _ = resolveRules(ctx.Field.Type().Key(), rules.GetKeys())
if out.Typ == "error" {
err = fmt.Errorf("unknown rule type (%T)", rules)
}
return
}
func (ctx RuleContext) Elem(name, idx string) (out RuleContext, err error) {
out.Field = ctx.Field
out.AccessorOverride = name
out.Index = idx
var rules *validate.FieldRules
switch r := ctx.Rules.(type) {
case *validate.MapRules:
rules = r.GetValues()
case *validate.RepeatedRules:
rules = r.GetItems()
default:
err = fmt.Errorf("cannot get Elem RuleContext from %T", ctx.Field)
return
}
var wrapped bool
if out.Typ, out.Rules, wrapped = resolveRules(ctx.Field.Type().Element(), rules); wrapped {
out.WrapperTyp = out.Typ
out.Typ = "wrapper"
}
if out.Typ == "error" {
err = fmt.Errorf("unknown rule type (%T)", rules)
}
return
}
func (ctx RuleContext) Unwrap(name string) (out RuleContext, err error) {
if ctx.Typ != "wrapper" {
err = fmt.Errorf("cannot unwrap non-wrapper type %q", ctx.Typ)
return
}
return RuleContext{
Field: ctx.Field,
Rules: ctx.Rules,
Typ: ctx.WrapperTyp,
AccessorOverride: name,
}, nil
}
func Render(tpl *template.Template) func(ctx RuleContext) (string, error) {
return func(ctx RuleContext) (string, error) {
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, ctx.Typ, ctx)
return b.String(), err
}
}
func resolveRules(typ interface{ IsEmbed() bool }, rules *validate.FieldRules) (ruleType string, rule proto.Message, wrapped bool) {
switch r := rules.GetType().(type) {
case *validate.FieldRules_Float:
return "float", r.Float, typ.IsEmbed()
case *validate.FieldRules_Double:
return "double", r.Double, typ.IsEmbed()
case *validate.FieldRules_Int32:
return "int32", r.Int32, typ.IsEmbed()
case *validate.FieldRules_Int64:
return "int64", r.Int64, typ.IsEmbed()
case *validate.FieldRules_Uint32:
return "uint32", r.Uint32, typ.IsEmbed()
case *validate.FieldRules_Uint64:
return "uint64", r.Uint64, typ.IsEmbed()
case *validate.FieldRules_Sint32:
return "sint32", r.Sint32, false
case *validate.FieldRules_Sint64:
return "sint64", r.Sint64, false
case *validate.FieldRules_Fixed32:
return "fixed32", r.Fixed32, false
case *validate.FieldRules_Fixed64:
return "fixed64", r.Fixed64, false
case *validate.FieldRules_Sfixed32:
return "sfixed32", r.Sfixed32, false
case *validate.FieldRules_Sfixed64:
return "sfixed64", r.Sfixed64, false
case *validate.FieldRules_Bool:
return "bool", r.Bool, typ.IsEmbed()
case *validate.FieldRules_String_:
return "string", r.String_, typ.IsEmbed()
case *validate.FieldRules_Bytes:
return "bytes", r.Bytes, typ.IsEmbed()
case *validate.FieldRules_Enum:
return "enum", r.Enum, false
case *validate.FieldRules_Message:
return "message", r.Message, false
case *validate.FieldRules_Repeated:
return "repeated", r.Repeated, false
case *validate.FieldRules_Map:
return "map", r.Map, false
case *validate.FieldRules_Any:
return "any", r.Any, false
case *validate.FieldRules_Duration:
return "duration", r.Duration, false
case *validate.FieldRules_Timestamp:
return "timestamp", r.Timestamp, false
case nil:
if ft, ok := typ.(pgs.FieldType); ok && ft.IsRepeated() {
return "repeated", &validate.RepeatedRules{}, false
} else if typ.IsEmbed() {
return "message", &validate.MessageRules{}, false
}
return "none", nil, false
default:
return "error", nil, false
}
}