-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuilder_test.go
140 lines (123 loc) · 4.31 KB
/
builder_test.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
// 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 (
"sync"
"testing"
pvcel "github.com/bufbuild/protovalidate-go/cel"
pb "github.com/bufbuild/protovalidate-go/internal/gen/tests/example/v1"
"github.com/google/cel-go/cel"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func TestBuildCache(t *testing.T) {
t.Parallel()
env, err := cel.NewEnv(cel.Lib(pvcel.NewLibrary()))
require.NoError(t, err, "failed to construct CEL environment")
bldr := newBuilder(
env, false, protoregistry.GlobalTypes, false,
)
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
dynamicMsg := dynamicProto{&pb.Person{
Id: 1234,
Email: "[email protected]",
Name: "Protocol Buffer",
}, int32(i)}
desc := dynamicMsg.ProtoReflect().Descriptor()
go func() {
defer wg.Done()
eval := bldr.Load(desc)
assert.NotNil(t, eval)
}()
}
wg.Wait()
}
type dynamicProto struct {
proto.Message
salt int32
}
func (d dynamicProto) ProtoReflect() protoreflect.Message {
return dynamicMessage{d.Message.ProtoReflect(), d.salt}
}
type dynamicMessage struct {
protoreflect.Message
salt int32
}
func (d dynamicMessage) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
d.Message.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
return f(dynamicFieldDescriptor{fd, d.salt}, v)
})
}
func (d dynamicMessage) Has(fd protoreflect.FieldDescriptor) bool {
return d.Message.Has(unwrapFieldDescriptor(fd))
}
func (d dynamicMessage) Clear(fd protoreflect.FieldDescriptor) {
d.Message.Clear(unwrapFieldDescriptor(fd))
}
func (d dynamicMessage) Get(fd protoreflect.FieldDescriptor) protoreflect.Value {
return d.Message.Get(unwrapFieldDescriptor(fd))
}
func (d dynamicMessage) Set(fd protoreflect.FieldDescriptor, v protoreflect.Value) {
d.Message.Set(unwrapFieldDescriptor(fd), v)
}
func (d dynamicMessage) Mutable(fd protoreflect.FieldDescriptor) protoreflect.Value {
return d.Message.Mutable(unwrapFieldDescriptor(fd))
}
func (d dynamicMessage) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value {
return d.Message.NewField(unwrapFieldDescriptor(fd))
}
func (d dynamicMessage) Descriptor() protoreflect.MessageDescriptor {
return dynamicMessageDescriptor{d.Message.Descriptor(), d.salt}
}
type dynamicMessageDescriptor struct {
protoreflect.MessageDescriptor
salt int32
}
func (d dynamicMessageDescriptor) Fields() protoreflect.FieldDescriptors {
return dynamicFieldDescriptors{d.MessageDescriptor.Fields(), d.salt}
}
type dynamicFieldDescriptors struct {
protoreflect.FieldDescriptors
salt int32
}
func (d dynamicFieldDescriptors) Get(i int) protoreflect.FieldDescriptor {
return dynamicFieldDescriptor{d.FieldDescriptors.Get(i), d.salt}
}
func (d dynamicFieldDescriptors) ByName(s protoreflect.Name) protoreflect.FieldDescriptor {
return dynamicFieldDescriptor{d.FieldDescriptors.ByName(s), d.salt}
}
func (d dynamicFieldDescriptors) ByJSONName(s string) protoreflect.FieldDescriptor {
return dynamicFieldDescriptor{d.FieldDescriptors.ByJSONName(s), d.salt}
}
func (d dynamicFieldDescriptors) ByTextName(s string) protoreflect.FieldDescriptor {
return dynamicFieldDescriptor{d.FieldDescriptors.ByTextName(s), d.salt}
}
func (d dynamicFieldDescriptors) ByNumber(n protoreflect.FieldNumber) protoreflect.FieldDescriptor {
return dynamicFieldDescriptor{d.FieldDescriptors.ByNumber(n), d.salt}
}
type dynamicFieldDescriptor struct {
protoreflect.FieldDescriptor
salt int32
}
func unwrapFieldDescriptor(fd protoreflect.FieldDescriptor) protoreflect.FieldDescriptor {
if d, ok := fd.(dynamicFieldDescriptor); ok {
return d.FieldDescriptor
}
return fd
}