| 1 |
// Copyright 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package pb |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/google/cel-go/checker/decls" |
| 23 |
"google.golang.org/protobuf/proto" |
| 24 |
|
| 25 |
proto2pb "github.com/google/cel-go/test/proto2pb" |
| 26 |
proto3pb "github.com/google/cel-go/test/proto3pb" |
| 27 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 28 |
dynamicpb "google.golang.org/protobuf/types/dynamicpb" |
| 29 |
anypb "google.golang.org/protobuf/types/known/anypb" |
| 30 |
dpb "google.golang.org/protobuf/types/known/durationpb" |
| 31 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 32 |
tpb "google.golang.org/protobuf/types/known/timestamppb" |
| 33 |
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 34 |
) |
| 35 |
|
| 36 |
func TestTypeDescription(t *testing.T) { |
| 37 |
pbdb := NewDb() |
| 38 |
types := []string{ |
| 39 |
".google.protobuf.Any", |
| 40 |
".google.protobuf.BoolValue", |
| 41 |
".google.protobuf.BytesValue", |
| 42 |
".google.protobuf.DoubleValue", |
| 43 |
".google.protobuf.FloatValue", |
| 44 |
".google.protobuf.Int32Value", |
| 45 |
".google.protobuf.Int64Value", |
| 46 |
".google.protobuf.ListValue", |
| 47 |
".google.protobuf.Struct", |
| 48 |
".google.protobuf.Value", |
| 49 |
} |
| 50 |
for _, typeName := range types { |
| 51 |
if _, found := pbdb.DescribeType(typeName); !found { |
| 52 |
t.Errorf("pbdb.DescribeType(%v) not found", typeName) |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestTypeDescriptionGroupFields(t *testing.T) { |
| 58 |
pbdb := NewDb() |
| 59 |
msg := &proto2pb.TestAllTypes{} |
| 60 |
pbdb.RegisterMessage(msg) |
| 61 |
td, found := pbdb.DescribeType(string(msg.ProtoReflect().Descriptor().FullName())) |
| 62 |
if !found { |
| 63 |
t.Fatalf("pbdb.DescribeType(%v) not found", msg) |
| 64 |
} |
| 65 |
field, found := td.FieldByName("nestedgroup") |
| 66 |
if !found { |
| 67 |
t.Fatal("TypeDescription.FieldByName('nestedgroup') could not be found") |
| 68 |
} |
| 69 |
if !field.IsMessage() { |
| 70 |
t.Errorf("Group field 'nestedgroup' is type %v, wanted message type", field.Descriptor()) |
| 71 |
} |
| 72 |
if !proto.Equal(field.CheckedType(), decls.NewObjectType("google.expr.proto2.test.TestAllTypes.NestedGroup")) { |
| 73 |
t.Errorf("field.CheckedType() got %v, wanted 'google.expr.proto2.test.TestAllTypes.NestedGroup'", field.CheckedType()) |
| 74 |
} |
| 75 |
ng, found := pbdb.DescribeType(string(field.Descriptor().Message().FullName())) |
| 76 |
if !found { |
| 77 |
t.Fatalf("pbdb.DescribeType(%v) not found", field.Descriptor().Message().FullName()) |
| 78 |
} |
| 79 |
groupFields := ng.FieldMap() |
| 80 |
if groupFields["nested_name"].ReflectType() != reflect.TypeOf("") { |
| 81 |
t.Errorf("groupFields['nested_name'] is type %v, not string", groupFields["nested_name"].ReflectType()) |
| 82 |
} |
| 83 |
if groupFields["nested_id"].ReflectType() != reflect.TypeOf(int32(1)) { |
| 84 |
t.Errorf("groupFields['nested_id'] is type %v, not int32", groupFields["nested_id"].ReflectType()) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestTypeDescriptionFieldMap(t *testing.T) { |
| 89 |
pbdb := NewDb() |
| 90 |
msg := &proto3pb.NestedTestAllTypes{} |
| 91 |
pbdb.RegisterMessage(msg) |
| 92 |
td, found := pbdb.DescribeType(string(msg.ProtoReflect().Descriptor().FullName())) |
| 93 |
if !found { |
| 94 |
t.Fatalf("pbdb.DescribeType(%v) not found", msg) |
| 95 |
} |
| 96 |
if len(td.FieldMap()) != 2 { |
| 97 |
t.Errorf("Unexpected field count. got '%d', wanted '%d'", len(td.FieldMap()), 2) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestFieldDescription(t *testing.T) { |
| 102 |
pbdb := NewDb() |
| 103 |
msg := proto3pb.NestedTestAllTypes{} |
| 104 |
_, err := pbdb.RegisterMessage(&msg) |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("pbdb.RegisterMessage(%v) failed: %v", &msg, err) |
| 107 |
} |
| 108 |
td, found := pbdb.DescribeType(string(msg.ProtoReflect().Descriptor().FullName())) |
| 109 |
if !found { |
| 110 |
t.Fatalf("pbdb.DescribeType(%v) not found", &msg) |
| 111 |
} |
| 112 |
fd, found := td.FieldByName("payload") |
| 113 |
if !found { |
| 114 |
t.Error("Field 'payload' not found") |
| 115 |
} |
| 116 |
if fd.Name() != "payload" { |
| 117 |
t.Error("Unexpected struct name for field 'payload'", fd.Name()) |
| 118 |
} |
| 119 |
if fd.IsOneof() { |
| 120 |
t.Error("Field payload is listed as a oneof and it is not.") |
| 121 |
} |
| 122 |
if fd.IsMap() { |
| 123 |
t.Error("Field 'payload' is listed as a map and it is not.") |
| 124 |
} |
| 125 |
if !fd.IsMessage() { |
| 126 |
t.Error("Field 'payload' is not marked as a message.") |
| 127 |
} |
| 128 |
if fd.IsEnum() { |
| 129 |
t.Error("Field 'payload' is marked as an enum.") |
| 130 |
} |
| 131 |
if fd.IsList() { |
| 132 |
t.Error("Field 'payload' is marked as repeated.") |
| 133 |
} |
| 134 |
// Access the field by its Go struct name and check to see that it's index |
| 135 |
// matches the one determined by the TypeDescription utils. |
| 136 |
got := fd.CheckedType() |
| 137 |
wanted := &exprpb.Type{ |
| 138 |
TypeKind: &exprpb.Type_MessageType{ |
| 139 |
MessageType: "google.expr.proto3.test.TestAllTypes", |
| 140 |
}, |
| 141 |
} |
| 142 |
if !proto.Equal(got, wanted) { |
| 143 |
t.Error("Field 'payload' had an unexpected checked type.") |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestFieldDescriptionGetFrom(t *testing.T) { |
| 148 |
pbdb := NewDb() |
| 149 |
msg := &proto3pb.TestAllTypes{ |
| 150 |
SingleUint64: 12, |
| 151 |
SingleDuration: dpb.New(time.Duration(1234)), |
| 152 |
SingleTimestamp: tpb.New(time.Unix(12345, 0).UTC()), |
| 153 |
SingleBoolWrapper: wrapperspb.Bool(false), |
| 154 |
SingleInt32Wrapper: wrapperspb.Int32(42), |
| 155 |
StandaloneEnum: proto3pb.TestAllTypes_BAR, |
| 156 |
NestedType: &proto3pb.TestAllTypes_SingleNestedMessage{ |
| 157 |
SingleNestedMessage: &proto3pb.TestAllTypes_NestedMessage{ |
| 158 |
Bb: 123, |
| 159 |
}, |
| 160 |
}, |
| 161 |
SingleValue: structpb.NewStringValue("hello world"), |
| 162 |
SingleStruct: jsonStruct(t, map[string]any{ |
| 163 |
"null": nil, |
| 164 |
}), |
| 165 |
} |
| 166 |
msgName := string(msg.ProtoReflect().Descriptor().FullName()) |
| 167 |
_, err := pbdb.RegisterMessage(msg) |
| 168 |
if err != nil { |
| 169 |
t.Fatalf("pbdb.RegisterMessage(%q) failed: %v", msgName, err) |
| 170 |
} |
| 171 |
td, found := pbdb.DescribeType(msgName) |
| 172 |
if !found { |
| 173 |
t.Fatalf("pbdb.DescribeType(%q) not found", msgName) |
| 174 |
} |
| 175 |
expected := map[string]any{ |
| 176 |
"single_uint64": uint64(12), |
| 177 |
"single_duration": time.Duration(1234), |
| 178 |
"single_timestamp": time.Unix(12345, 0).UTC(), |
| 179 |
"single_bool_wrapper": false, |
| 180 |
"single_int32_wrapper": int32(42), |
| 181 |
"single_int64_wrapper": structpb.NullValue_NULL_VALUE, |
| 182 |
"single_nested_message": &proto3pb.TestAllTypes_NestedMessage{ |
| 183 |
Bb: 123, |
| 184 |
}, |
| 185 |
"standalone_enum": int64(1), |
| 186 |
"single_value": "hello world", |
| 187 |
"single_struct": jsonStruct(t, map[string]any{ |
| 188 |
"null": nil, |
| 189 |
}), |
| 190 |
} |
| 191 |
for field, want := range expected { |
| 192 |
f, found := td.FieldByName(field) |
| 193 |
if !found { |
| 194 |
t.Fatalf("td.FieldByName(%q) not found", field) |
| 195 |
} |
| 196 |
got, err := f.GetFrom(msg) |
| 197 |
if err != nil { |
| 198 |
t.Fatalf("field.GetFrom() failed: %v", err) |
| 199 |
} |
| 200 |
switch g := got.(type) { |
| 201 |
case proto.Message: |
| 202 |
if !proto.Equal(g, want.(proto.Message)) { |
| 203 |
t.Errorf("got field %s value %v, wanted %v", field, g, want) |
| 204 |
} |
| 205 |
default: |
| 206 |
if !reflect.DeepEqual(g, want) { |
| 207 |
t.Errorf("got field %s value %v, wanted %v", field, g, want) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestFieldDescriptionIsSet(t *testing.T) { |
| 214 |
pbdb := NewDb() |
| 215 |
msg := &proto3pb.TestAllTypes{} |
| 216 |
msgName := string(msg.ProtoReflect().Descriptor().FullName()) |
| 217 |
_, err := pbdb.RegisterMessage(msg) |
| 218 |
if err != nil { |
| 219 |
t.Fatalf("pbdb.RegisterMessage(%q) failed: %v", msgName, err) |
| 220 |
} |
| 221 |
td, found := pbdb.DescribeType(msgName) |
| 222 |
if !found { |
| 223 |
t.Fatalf("pbdb.DescribeType(%q) not found", msgName) |
| 224 |
} |
| 225 |
|
| 226 |
tests := []struct { |
| 227 |
msg any |
| 228 |
field string |
| 229 |
isSet bool |
| 230 |
}{ |
| 231 |
{ |
| 232 |
msg: &proto3pb.TestAllTypes{SingleBool: true}, |
| 233 |
field: "single_bool", |
| 234 |
isSet: true, |
| 235 |
}, |
| 236 |
{ |
| 237 |
msg: &proto3pb.TestAllTypes{}, |
| 238 |
field: "single_bool", |
| 239 |
isSet: false, |
| 240 |
}, |
| 241 |
{ |
| 242 |
msg: (&proto3pb.TestAllTypes{SingleBool: false}), |
| 243 |
field: "single_bool", |
| 244 |
isSet: false, |
| 245 |
}, |
| 246 |
{ |
| 247 |
msg: (&proto3pb.TestAllTypes{}), |
| 248 |
field: "single_bool", |
| 249 |
isSet: false, |
| 250 |
}, |
| 251 |
{ |
| 252 |
msg: reflect.ValueOf(&proto3pb.TestAllTypes{}), |
| 253 |
field: "single_bool", |
| 254 |
isSet: false, |
| 255 |
}, |
| 256 |
{ |
| 257 |
msg: nil, |
| 258 |
field: "single_any", |
| 259 |
isSet: false, |
| 260 |
}, |
| 261 |
} |
| 262 |
for _, tc := range tests { |
| 263 |
f, found := td.FieldByName(tc.field) |
| 264 |
if !found { |
| 265 |
t.Fatalf("td.FieldByName(%q) not found", tc.field) |
| 266 |
} |
| 267 |
if f.IsSet(tc.msg) != tc.isSet { |
| 268 |
t.Errorf("got field %s set: %v, wanted %v", tc.field, f.IsSet(tc.msg), tc.isSet) |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestTypeDescriptionMaybeUnwrap(t *testing.T) { |
| 274 |
pbdb := NewDb() |
| 275 |
_, err := pbdb.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 276 |
if err != nil { |
| 277 |
t.Fatalf("pbdb.RegisterMessage() failed: %v", err) |
| 278 |
} |
| 279 |
msgType := "google.protobuf.Value" |
| 280 |
msgDesc, found := pbdb.DescribeType(msgType) |
| 281 |
if !found { |
| 282 |
t.Fatalf("pbdb.DescribeType(%q) not found", msgType) |
| 283 |
} |
| 284 |
|
| 285 |
tests := []struct { |
| 286 |
in proto.Message |
| 287 |
out any |
| 288 |
}{ |
| 289 |
{ |
| 290 |
in: msgDesc.Zero(), |
| 291 |
out: structpb.NullValue_NULL_VALUE, |
| 292 |
}, |
| 293 |
{ |
| 294 |
in: msgDesc.New().Interface(), |
| 295 |
out: structpb.NullValue_NULL_VALUE, |
| 296 |
}, |
| 297 |
{ |
| 298 |
in: anyMsg(t, wrapperspb.Bool(true)), |
| 299 |
out: true, |
| 300 |
}, |
| 301 |
{ |
| 302 |
in: anyMsg(t, structpb.NewNumberValue(4.5)), |
| 303 |
out: float64(4.5), |
| 304 |
}, |
| 305 |
{ |
| 306 |
in: dynMsg(t, anyMsg(t, structpb.NewNumberValue(4.5))), |
| 307 |
out: float64(4.5), |
| 308 |
}, |
| 309 |
{ |
| 310 |
in: dynMsg(t, anyMsg(t, &proto3pb.TestAllTypes{SingleFloat: 123.0})), |
| 311 |
out: &proto3pb.TestAllTypes{SingleFloat: 123.0}, |
| 312 |
}, |
| 313 |
{ |
| 314 |
in: dynMsg(t, &structpb.ListValue{}), |
| 315 |
out: jsonList(t, []any{}), |
| 316 |
}, |
| 317 |
{ |
| 318 |
in: structpb.NewBoolValue(true), |
| 319 |
out: true, |
| 320 |
}, |
| 321 |
{ |
| 322 |
in: structpb.NewBoolValue(false), |
| 323 |
out: false, |
| 324 |
}, |
| 325 |
{ |
| 326 |
in: structpb.NewNullValue(), |
| 327 |
out: structpb.NullValue_NULL_VALUE, |
| 328 |
}, |
| 329 |
{ |
| 330 |
in: &structpb.Value{}, |
| 331 |
out: structpb.NullValue_NULL_VALUE, |
| 332 |
}, |
| 333 |
{ |
| 334 |
in: structpb.NewNumberValue(1.5), |
| 335 |
out: float64(1.5), |
| 336 |
}, |
| 337 |
{ |
| 338 |
in: structpb.NewStringValue("hello world"), |
| 339 |
out: "hello world", |
| 340 |
}, |
| 341 |
{ |
| 342 |
in: structpb.NewListValue(jsonList(t, []any{true, 1.0})), |
| 343 |
out: jsonList(t, []any{true, 1.0}), |
| 344 |
}, |
| 345 |
{ |
| 346 |
in: structpb.NewStructValue(jsonStruct(t, map[string]any{"hello": "world"})), |
| 347 |
out: jsonStruct(t, map[string]any{"hello": "world"}), |
| 348 |
}, |
| 349 |
{ |
| 350 |
in: wrapperspb.Bool(false), |
| 351 |
out: false, |
| 352 |
}, |
| 353 |
{ |
| 354 |
in: wrapperspb.Bool(true), |
| 355 |
out: true, |
| 356 |
}, |
| 357 |
{ |
| 358 |
in: (*wrapperspb.BoolValue)(nil), |
| 359 |
out: nil, |
| 360 |
}, |
| 361 |
{ |
| 362 |
in: wrapperspb.Bytes([]byte("hello")), |
| 363 |
out: []byte("hello"), |
| 364 |
}, |
| 365 |
{ |
| 366 |
in: (*wrapperspb.BytesValue)(nil), |
| 367 |
out: nil, |
| 368 |
}, |
| 369 |
{ |
| 370 |
in: wrapperspb.Double(-4.2), |
| 371 |
out: -4.2, |
| 372 |
}, |
| 373 |
{ |
| 374 |
in: (*wrapperspb.DoubleValue)(nil), |
| 375 |
out: nil, |
| 376 |
}, |
| 377 |
{ |
| 378 |
in: wrapperspb.Float(4.5), |
| 379 |
out: 4.5, |
| 380 |
}, |
| 381 |
{ |
| 382 |
in: (*wrapperspb.FloatValue)(nil), |
| 383 |
out: nil, |
| 384 |
}, |
| 385 |
{ |
| 386 |
in: wrapperspb.Int32(123), |
| 387 |
out: int64(123), |
| 388 |
}, |
| 389 |
{ |
| 390 |
in: (*wrapperspb.Int32Value)(nil), |
| 391 |
out: nil, |
| 392 |
}, |
| 393 |
{ |
| 394 |
in: wrapperspb.Int64(456), |
| 395 |
out: int64(456), |
| 396 |
}, |
| 397 |
{ |
| 398 |
in: (*wrapperspb.Int64Value)(nil), |
| 399 |
out: nil, |
| 400 |
}, |
| 401 |
{ |
| 402 |
in: wrapperspb.String("goodbye"), |
| 403 |
out: "goodbye", |
| 404 |
}, |
| 405 |
{ |
| 406 |
in: (*wrapperspb.StringValue)(nil), |
| 407 |
out: nil, |
| 408 |
}, |
| 409 |
{ |
| 410 |
in: wrapperspb.UInt32(1234), |
| 411 |
out: uint64(1234), |
| 412 |
}, |
| 413 |
{ |
| 414 |
in: (*wrapperspb.UInt32Value)(nil), |
| 415 |
out: nil, |
| 416 |
}, |
| 417 |
{ |
| 418 |
in: wrapperspb.UInt64(5678), |
| 419 |
out: uint64(5678), |
| 420 |
}, |
| 421 |
{ |
| 422 |
in: (*wrapperspb.UInt64Value)(nil), |
| 423 |
out: nil, |
| 424 |
}, |
| 425 |
{ |
| 426 |
in: tpb.New(time.Unix(12345, 0).UTC()), |
| 427 |
out: time.Unix(12345, 0).UTC(), |
| 428 |
}, |
| 429 |
{ |
| 430 |
in: dpb.New(time.Duration(345)), |
| 431 |
out: time.Duration(345), |
| 432 |
}, |
| 433 |
{ |
| 434 |
in: struct{ proto.Message }{wrapperspb.Int32(1234)}, |
| 435 |
out: int32(1234), |
| 436 |
}, |
| 437 |
} |
| 438 |
for _, tc := range tests { |
| 439 |
typeName := string(tc.in.ProtoReflect().Descriptor().FullName()) |
| 440 |
td, found := pbdb.DescribeType(typeName) |
| 441 |
if !found { |
| 442 |
t.Fatalf("pbdb.DescribeType(%q) not found", typeName) |
| 443 |
} |
| 444 |
msg, unwrapped, err := td.MaybeUnwrap(tc.in) |
| 445 |
if err != nil { |
| 446 |
t.Fatalf("MaybeUnwrap(%v) failed: %v", tc.in, err) |
| 447 |
} |
| 448 |
if !unwrapped { |
| 449 |
t.Errorf("value %v not unwrapped", tc.in) |
| 450 |
} |
| 451 |
switch val := msg.(type) { |
| 452 |
case proto.Message: |
| 453 |
if !proto.Equal(val, tc.out.(proto.Message)) { |
| 454 |
t.Errorf("got value %v, wanted %v", val, tc.out) |
| 455 |
} |
| 456 |
default: |
| 457 |
if !reflect.DeepEqual(val, tc.out) { |
| 458 |
t.Errorf("got value %v, wanted %v", val, tc.out) |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
func BenchmarkTypeDescriptionMaybeUnwrap(b *testing.B) { |
| 465 |
pbdb := NewDb() |
| 466 |
pbdb.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 467 |
msgType := "google.protobuf.Value" |
| 468 |
msgDesc, found := pbdb.DescribeType(msgType) |
| 469 |
if !found { |
| 470 |
b.Fatalf("pbdb.DescribeType(%q) not found", msgType) |
| 471 |
} |
| 472 |
tests := []struct { |
| 473 |
in proto.Message |
| 474 |
}{ |
| 475 |
{in: msgDesc.Zero()}, |
| 476 |
{in: msgDesc.New().Interface()}, |
| 477 |
{in: dynamicpb.NewMessage((&structpb.ListValue{}).ProtoReflect().Descriptor())}, |
| 478 |
{in: structpb.NewBoolValue(true)}, |
| 479 |
{in: structpb.NewBoolValue(false)}, |
| 480 |
{in: structpb.NewNullValue()}, |
| 481 |
{in: &structpb.Value{}}, |
| 482 |
{in: structpb.NewNumberValue(1.5)}, |
| 483 |
{in: structpb.NewStringValue("hello world")}, |
| 484 |
{in: wrapperspb.Bool(false)}, |
| 485 |
{in: wrapperspb.Bool(true)}, |
| 486 |
{in: wrapperspb.Bytes([]byte("hello"))}, |
| 487 |
{in: wrapperspb.Double(-4.2)}, |
| 488 |
{in: wrapperspb.Float(4.5)}, |
| 489 |
{in: wrapperspb.Int32(123)}, |
| 490 |
{in: wrapperspb.Int64(456)}, |
| 491 |
{in: wrapperspb.String("goodbye")}, |
| 492 |
{in: wrapperspb.UInt32(1234)}, |
| 493 |
{in: wrapperspb.UInt64(5678)}, |
| 494 |
{in: tpb.New(time.Unix(12345, 0).UTC())}, |
| 495 |
{in: dpb.New(time.Duration(345))}, |
| 496 |
{in: &proto3pb.TestAllTypes{}}, |
| 497 |
} |
| 498 |
for _, tc := range tests { |
| 499 |
typeName := string(tc.in.ProtoReflect().Descriptor().FullName()) |
| 500 |
td, found := pbdb.DescribeType(typeName) |
| 501 |
if !found { |
| 502 |
b.Fatalf("pbdb.DescribeType(%q) not found", typeName) |
| 503 |
} |
| 504 |
in := tc.in |
| 505 |
b.Run(typeName, func(b *testing.B) { |
| 506 |
for i := 0; i < b.N; i++ { |
| 507 |
td.MaybeUnwrap(in) |
| 508 |
} |
| 509 |
}) |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
func TestTypeDescriptionCheckedType(t *testing.T) { |
| 514 |
pbdb := NewDb() |
| 515 |
msg := &proto3pb.TestAllTypes{} |
| 516 |
msgName := string(msg.ProtoReflect().Descriptor().FullName()) |
| 517 |
_, err := pbdb.RegisterMessage(msg) |
| 518 |
if err != nil { |
| 519 |
t.Fatalf("pbdb.RegisterMessage(%q) failed: %v", msgName, err) |
| 520 |
} |
| 521 |
td, found := pbdb.DescribeType(msgName) |
| 522 |
if !found { |
| 523 |
t.Fatalf("pbdb.DescribeType(%q) not found", msgName) |
| 524 |
} |
| 525 |
field, found := td.FieldByName("map_string_string") |
| 526 |
if !found { |
| 527 |
t.Fatal("td.FieldByName('map_string_string') not found") |
| 528 |
} |
| 529 |
mapType := decls.NewMapType(decls.String, decls.String) |
| 530 |
if !proto.Equal(field.CheckedType(), mapType) { |
| 531 |
t.Errorf("got checked type %v, wanted %v", field.CheckedType(), mapType) |
| 532 |
} |
| 533 |
field, found = td.FieldByName("repeated_nested_message") |
| 534 |
if !found { |
| 535 |
t.Fatal("td.FieldByName('repeated_nested_message') not found") |
| 536 |
} |
| 537 |
listType := decls.NewListType(decls.NewObjectType("google.expr.proto3.test.TestAllTypes.NestedMessage")) |
| 538 |
if !proto.Equal(field.CheckedType(), listType) { |
| 539 |
t.Errorf("got checked type %v, wanted %v", field.CheckedType(), listType) |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
func dynMsg(t *testing.T, msg proto.Message) *dynamicpb.Message { |
| 544 |
t.Helper() |
| 545 |
dynPB := dynamicpb.NewMessage(msg.ProtoReflect().Descriptor()) |
| 546 |
proto.Merge(dynPB, msg) |
| 547 |
return dynPB |
| 548 |
} |
| 549 |
|
| 550 |
func anyMsg(t *testing.T, msg proto.Message) *anypb.Any { |
| 551 |
t.Helper() |
| 552 |
pb, err := anypb.New(msg) |
| 553 |
if err != nil { |
| 554 |
t.Fatalf("anypb.New(%v) failed: %v", msg, err) |
| 555 |
} |
| 556 |
return pb |
| 557 |
} |
| 558 |
|
| 559 |
func jsonList(t *testing.T, elems []any) *structpb.ListValue { |
| 560 |
t.Helper() |
| 561 |
l, err := structpb.NewList(elems) |
| 562 |
if err != nil { |
| 563 |
t.Fatalf("structpb.NewList() failed: %v", err) |
| 564 |
} |
| 565 |
return l |
| 566 |
} |
| 567 |
|
| 568 |
func jsonStruct(t *testing.T, entries map[string]any) *structpb.Struct { |
| 569 |
t.Helper() |
| 570 |
s, err := structpb.NewStruct(entries) |
| 571 |
if err != nil { |
| 572 |
t.Fatalf("structpb.NewStruct() failed: %v", err) |
| 573 |
} |
| 574 |
return s |
| 575 |
} |