-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmap.go
More file actions
137 lines (124 loc) · 4.71 KB
/
map.go
File metadata and controls
137 lines (124 loc) · 4.71 KB
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
// Copyright 2023-2026 Buf Technologies, Inc.
//
// 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 protovalidate
import (
"fmt"
"strconv"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
//nolint:gochecknoglobals
var (
mapRuleDescriptor = (&validate.FieldRules{}).ProtoReflect().Descriptor().Fields().ByName("map")
mapKeysRuleDescriptor = (&validate.MapRules{}).ProtoReflect().Descriptor().Fields().ByName("keys")
mapKeysRulePath = validate.FieldPath_builder{
Elements: []*validate.FieldPathElement{
fieldPathElement(mapRuleDescriptor),
fieldPathElement(mapKeysRuleDescriptor),
},
}.Build()
mapValuesDescriptor = (&validate.MapRules{}).ProtoReflect().Descriptor().Fields().ByName("values")
mapValuesRulePath = validate.FieldPath_builder{
Elements: []*validate.FieldPathElement{
fieldPathElement(mapRuleDescriptor),
fieldPathElement(mapValuesDescriptor),
},
}.Build()
)
// kvPairs performs validation on a map field's KV Pairs.
//
// This type uses pointer receivers to avoid a heap allocation on every
// Evaluate call. The closure passed to Map().Range() captures the receiver,
// and with a value receiver the entire struct would escape to the heap.
// With a pointer receiver, only the pointer is captured.
type kvPairs struct {
base
// KeyRules are checked on the map keys
KeyRules value
// ValueRules are checked on the map values
ValueRules value
}
func newKVPairs(valEval *value) *kvPairs {
return &kvPairs{
base: newBase(valEval),
KeyRules: value{NestedRule: mapKeysRulePath},
ValueRules: value{NestedRule: mapValuesRulePath},
}
}
func (m *kvPairs) Evaluate(msg protoreflect.Message, val protoreflect.Value, cfg *validationConfig) (err error) {
var ok bool
val.Map().Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
evalErr := m.evalPairs(msg, key, value, cfg)
if evalErr != nil {
element := validate.FieldPathElement_builder{
FieldNumber: proto.Int32(m.FieldPathElement.GetFieldNumber()),
FieldType: m.base.FieldPathElement.GetFieldType().Enum(),
FieldName: proto.String(m.FieldPathElement.GetFieldName()),
}
element.KeyType = descriptorpb.FieldDescriptorProto_Type(m.base.Descriptor.MapKey().Kind()).Enum()
element.ValueType = descriptorpb.FieldDescriptorProto_Type(m.base.Descriptor.MapValue().Kind()).Enum()
switch m.base.Descriptor.MapKey().Kind() {
case protoreflect.BoolKind:
element.BoolKey = proto.Bool(key.Bool())
case protoreflect.Int32Kind, protoreflect.Int64Kind,
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind,
protoreflect.Sint32Kind, protoreflect.Sint64Kind:
element.IntKey = proto.Int64(key.Int())
case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
element.UintKey = proto.Uint64(key.Uint())
case protoreflect.StringKind:
element.StringKey = proto.String(key.String())
case protoreflect.EnumKind, protoreflect.FloatKind, protoreflect.DoubleKind,
protoreflect.BytesKind, protoreflect.MessageKind, protoreflect.GroupKind:
fallthrough
default:
err = &CompilationError{cause: fmt.Errorf(
"unexpected map key type %s",
m.base.Descriptor.MapKey().Kind())}
return false
}
updateViolationPaths(evalErr, element.Build(), m.RulePrefix.GetElements())
}
ok, err = mergeViolations(err, evalErr, cfg)
return ok
})
return err
}
func (m *kvPairs) evalPairs(msg protoreflect.Message, key protoreflect.MapKey, value protoreflect.Value, cfg *validationConfig) (err error) {
evalErr := m.KeyRules.EvaluateField(msg, key.Value(), cfg, true)
markViolationForKey(evalErr)
ok, err := mergeViolations(err, evalErr, cfg)
if !ok {
return err
}
evalErr = m.ValueRules.EvaluateField(msg, value, cfg, true)
_, err = mergeViolations(err, evalErr, cfg)
return err
}
func (m *kvPairs) Tautology() bool {
return m.KeyRules.Tautology() &&
m.ValueRules.Tautology()
}
func (m *kvPairs) formatKey(key any) string {
switch k := key.(type) {
case string:
return strconv.Quote(k)
default:
return fmt.Sprintf("%v", key)
}
}
var _ evaluator = (*kvPairs)(nil)