-
Notifications
You must be signed in to change notification settings - Fork 19
/
map.go
132 lines (119 loc) · 4.52 KB
/
map.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
// Copyright 2023-2024 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.FieldConstraints{}).ProtoReflect().Descriptor().Fields().ByName("map")
mapKeysRuleDescriptor = (&validate.MapRules{}).ProtoReflect().Descriptor().Fields().ByName("keys")
mapKeysRulePath = &validate.FieldPath{
Elements: []*validate.FieldPathElement{
fieldPathElement(mapRuleDescriptor),
fieldPathElement(mapKeysRuleDescriptor),
},
}
mapValuesDescriptor = (&validate.MapRules{}).ProtoReflect().Descriptor().Fields().ByName("values")
mapValuesRulePath = &validate.FieldPath{
Elements: []*validate.FieldPathElement{
fieldPathElement(mapRuleDescriptor),
fieldPathElement(mapValuesDescriptor),
},
}
)
// kvPairs performs validation on a map field's KV Pairs.
type kvPairs struct {
base
// KeyConstraints are checked on the map keys
KeyConstraints value
// ValueConstraints are checked on the map values
ValueConstraints value
}
func newKVPairs(valEval *value) kvPairs {
return kvPairs{
base: newBase(valEval),
KeyConstraints: value{NestedRule: mapKeysRulePath},
ValueConstraints: value{NestedRule: mapValuesRulePath},
}
}
func (m kvPairs) Evaluate(val protoreflect.Value, failFast bool) (err error) {
var ok bool
val.Map().Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
evalErr := m.evalPairs(key, value, failFast)
if evalErr != nil {
element := &validate.FieldPathElement{
FieldNumber: proto.Int32(m.base.FieldPathElement.GetFieldNumber()),
FieldType: m.base.FieldPathElement.GetFieldType().Enum(),
FieldName: proto.String(m.base.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.Subscript = &validate.FieldPathElement_BoolKey{BoolKey: key.Bool()}
case protoreflect.Int32Kind, protoreflect.Int64Kind,
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind,
protoreflect.Sint32Kind, protoreflect.Sint64Kind:
element.Subscript = &validate.FieldPathElement_IntKey{IntKey: key.Int()}
case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
element.Subscript = &validate.FieldPathElement_UintKey{UintKey: key.Uint()}
case protoreflect.StringKind:
element.Subscript = &validate.FieldPathElement_StringKey{StringKey: 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, m.base.RulePrefix.GetElements())
}
ok, err = mergeViolations(err, evalErr, failFast)
return ok
})
return err
}
func (m kvPairs) evalPairs(key protoreflect.MapKey, value protoreflect.Value, failFast bool) (err error) {
evalErr := m.KeyConstraints.Evaluate(key.Value(), failFast)
markViolationForKey(evalErr)
ok, err := mergeViolations(err, evalErr, failFast)
if !ok {
return err
}
evalErr = m.ValueConstraints.Evaluate(value, failFast)
_, err = mergeViolations(err, evalErr, failFast)
return err
}
func (m kvPairs) Tautology() bool {
return m.KeyConstraints.Tautology() &&
m.ValueConstraints.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{}