| 1 | // Copyright 2018 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 types |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "math" |
| 21 | "reflect" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | "time" |
| 25 | |
| 26 | "google.golang.org/protobuf/encoding/protojson" |
| 27 | "google.golang.org/protobuf/proto" |
| 28 | |
| 29 | "github.com/google/cel-go/common/types/pb" |
| 30 | "github.com/google/cel-go/common/types/ref" |
| 31 | "github.com/google/cel-go/common/types/traits" |
| 32 | |
| 33 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 34 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 35 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 36 | tpb "google.golang.org/protobuf/types/known/timestamppb" |
| 37 | wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 38 | ) |
| 39 | |
| 40 | type testStruct struct { |
| 41 | M string |
| 42 | Details []string |
| 43 | ExtraDetails string |
| 44 | } |
| 45 | |
| 46 | func TestMapContains(t *testing.T) { |
| 47 | reg := newTestRegistry(t, &proto3pb.TestAllTypes{}) |
| 48 | reflectMap := reg.NativeToValue(map[any]any{ |
| 49 | int64(1): "hello", |
| 50 | uint64(2): "world", |
| 51 | }).(traits.Mapper) |
| 52 | |
| 53 | refValMap := reg.NativeToValue(map[ref.Val]ref.Val{ |
| 54 | Int(1): String("hello"), |
| 55 | Uint(2): String("world"), |
| 56 | }).(traits.Mapper) |
| 57 | |
| 58 | msg := &proto3pb.TestAllTypes{ |
| 59 | MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{ |
| 60 | 1: {}, |
| 61 | 2: {}, |
| 62 | }, |
| 63 | } |
| 64 | pbMsg := reg.NativeToValue(msg).(traits.Indexer) |
| 65 | protoMap := pbMsg.Get(String("map_int64_nested_type")).(traits.Mapper) |
| 66 | |
| 67 | tests := []struct { |
| 68 | value any |
| 69 | out Bool |
| 70 | }{ |
| 71 | {value: 1, out: True}, |
| 72 | {value: 1.0, out: True}, |
| 73 | {value: uint(1), out: True}, |
| 74 | {value: 2, out: True}, |
| 75 | {value: 2.0, out: True}, |
| 76 | {value: uint(2), out: True}, |
| 77 | |
| 78 | {value: 3, out: False}, |
| 79 | {value: 1.1, out: False}, |
| 80 | {value: 1.1 + math.MaxInt64, out: False}, |
| 81 | {value: 1.1 + math.MaxUint64, out: False}, |
| 82 | {value: "3", out: False}, |
| 83 | } |
| 84 | |
| 85 | for i, tst := range tests { |
| 86 | tc := tst |
| 87 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 88 | v := reg.NativeToValue(tc.value) |
| 89 | if reflectMap.Contains(v).Equal(tc.out) != True { |
| 90 | t.Errorf("reflectMap.Contains(%v) got %v, wanted %v", v, tc.out.Negate(), tc.out) |
| 91 | } |
| 92 | if refValMap.Contains(v).Equal(tc.out) != True { |
| 93 | t.Errorf("refValMap.Contains(%v) got %v, wanted %v", v, tc.out.Negate(), tc.out) |
| 94 | } |
| 95 | if protoMap.Contains(v).Equal(tc.out) != True { |
| 96 | t.Errorf("protoMap.Contains(%v) got %v, wanted %v", v, tc.out.Negate(), tc.out) |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestStringMapContains(t *testing.T) { |
| 103 | reg := newTestRegistry(t) |
| 104 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 105 | "first": "hello", |
| 106 | "second": "world"}) |
| 107 | if mapVal.Contains(String("first")) != True { |
| 108 | t.Error("mapVal.Contains('first') did not return true") |
| 109 | } |
| 110 | if mapVal.Contains(String("third")) != False { |
| 111 | t.Error("mapVal.Contains('third') did not return false") |
| 112 | } |
| 113 | if IsError(mapVal.Contains(Int(123))) { |
| 114 | t.Error("mapVal.Contains(123) errored, wanted false'.") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestDynamicMapConvertToNative_Any(t *testing.T) { |
| 119 | reg := newTestRegistry(t) |
| 120 | mapVal := NewDynamicMap(reg, map[string]map[string]float32{ |
| 121 | "nested": {"1": -1.0}}) |
| 122 | val, err := mapVal.ConvertToNative(anyValueType) |
| 123 | if err != nil { |
| 124 | t.Error(err) |
| 125 | } |
| 126 | jsonMap := &structpb.Struct{} |
| 127 | err = protojson.Unmarshal([]byte(`{"nested":{"1":-1}}`), jsonMap) |
| 128 | if err != nil { |
| 129 | t.Fatalf("protojson.Unmarshal() failed: %v", err) |
| 130 | } |
| 131 | want, err := anypb.New(jsonMap) |
| 132 | if err != nil { |
| 133 | t.Error(err) |
| 134 | } |
| 135 | if !proto.Equal(val.(proto.Message), want) { |
| 136 | t.Errorf("Got %v, wanted %v", val, want) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestDynamicMapConvertToNative_Error(t *testing.T) { |
| 141 | reg := newTestRegistry(t) |
| 142 | mapVal := NewDynamicMap(reg, map[string]map[string]float32{ |
| 143 | "nested": {"1": -1.0}}) |
| 144 | val, err := mapVal.ConvertToNative(reflect.TypeOf("")) |
| 145 | if err == nil { |
| 146 | t.Errorf("mapVal.ConvertToNative(string) got '%v', expected error", val) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestDynamicMapConvertToNative_Json(t *testing.T) { |
| 151 | reg := newTestRegistry(t) |
| 152 | mapVal := NewDynamicMap(reg, map[string]map[string]float32{ |
| 153 | "nested": {"1": -1.0}}) |
| 154 | json, err := mapVal.ConvertToNative(JSONValueType) |
| 155 | if err != nil { |
| 156 | t.Error(err) |
| 157 | } |
| 158 | jsonBytes, err := protojson.Marshal(json.(proto.Message)) |
| 159 | if err != nil { |
| 160 | t.Fatalf("protojson.Marshal(%v) failed: %v", json, err) |
| 161 | } |
| 162 | jsonTxt := string(jsonBytes) |
| 163 | if jsonTxt != `{"nested":{"1":-1}}` { |
| 164 | t.Error(jsonTxt) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestDynamicMapConvertToNative_Struct(t *testing.T) { |
| 169 | reg := newTestRegistry(t) |
| 170 | want := testStruct{M: "hello", Details: []string{"world", "universe"}, ExtraDetails: "extra, extra!"} |
| 171 | tests := []map[string]any{ |
| 172 | { |
| 173 | "m": "hello", |
| 174 | "details": []string{"world", "universe"}, |
| 175 | "extra_Details": "extra, extra!", |
| 176 | }, |
| 177 | { |
| 178 | "M": "hello", |
| 179 | "Details": []string{"world", "universe"}, |
| 180 | "extra_details": "extra, extra!", |
| 181 | }, |
| 182 | { |
| 183 | "M": "hello", |
| 184 | "Details": []string{"world", "universe"}, |
| 185 | " extra___details": "extra, extra!", |
| 186 | }, |
| 187 | { |
| 188 | "M": "hello", |
| 189 | "Details_ ": []string{"world", "universe"}, |
| 190 | " extra___details": "extra, extra!", |
| 191 | }, |
| 192 | { |
| 193 | "_m": "hello", |
| 194 | "Details_ ": []string{"world", "universe"}, |
| 195 | " extra___details": "extra, extra!", |
| 196 | }, |
| 197 | { |
| 198 | "_M": "hello", |
| 199 | "Details_ ": []string{"world", "universe"}, |
| 200 | " extra___details": "extra, extra!", |
| 201 | }, |
| 202 | } |
| 203 | for i, tst := range tests { |
| 204 | tc := tst |
| 205 | t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 206 | mapVal := NewDynamicMap(reg, tc) |
| 207 | ts, err := mapVal.ConvertToNative(reflect.TypeFor[testStruct]()) |
| 208 | if err != nil { |
| 209 | t.Error(err) |
| 210 | } |
| 211 | if !reflect.DeepEqual(ts, want) { |
| 212 | t.Errorf("mapVal.ConvertToNative(struct) got %v, wanted %v", ts, want) |
| 213 | } |
| 214 | }) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestDynamicMapConvertToNative_StructPtr(t *testing.T) { |
| 219 | reg := newTestRegistry(t) |
| 220 | mapVal := NewDynamicMap(reg, map[string]any{ |
| 221 | "m": "hello", |
| 222 | "details": []string{"world", "universe"}, |
| 223 | }) |
| 224 | ts, err := mapVal.ConvertToNative(reflect.TypeOf(&testStruct{})) |
| 225 | if err != nil { |
| 226 | t.Error(err) |
| 227 | } |
| 228 | want := &testStruct{M: "hello", Details: []string{"world", "universe"}} |
| 229 | if !reflect.DeepEqual(ts, want) { |
| 230 | t.Errorf("mapVal.ConvertToNative(struct) got %v, wanted %v", ts, want) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | func TestDynamicMapConvertToNative_StructPtrPtr(t *testing.T) { |
| 235 | reg := newTestRegistry(t) |
| 236 | mapVal := NewDynamicMap(reg, map[string]any{ |
| 237 | "m": "hello", |
| 238 | "details": []string{"world", "universe"}, |
| 239 | }) |
| 240 | ptr := &testStruct{} |
| 241 | ts, err := mapVal.ConvertToNative(reflect.TypeOf(&ptr)) |
| 242 | if err == nil { |
| 243 | t.Errorf("Got %v, wanted error", ts) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func TestDynamicMapConvertToNative_Struct_InvalidFieldError(t *testing.T) { |
| 248 | reg := newTestRegistry(t) |
| 249 | mapVal := NewDynamicMap(reg, map[string]any{ |
| 250 | "m": "hello", |
| 251 | "details": []string{"world", "universe"}, |
| 252 | "invalid": "invalid field", |
| 253 | }) |
| 254 | ts, err := mapVal.ConvertToNative(reflect.TypeOf(&testStruct{})) |
| 255 | if err == nil { |
| 256 | t.Errorf("mapVal.ConvertToNative(struct) got %v, wanted error", ts) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestDynamicMapConvertToNative_Struct_EmptyFieldError(t *testing.T) { |
| 261 | reg := newTestRegistry(t) |
| 262 | mapVal := NewDynamicMap(reg, map[string]any{ |
| 263 | "m": "hello", |
| 264 | "details": []string{"world", "universe"}, |
| 265 | "": "empty field", |
| 266 | }) |
| 267 | ts, err := mapVal.ConvertToNative(reflect.TypeOf(&testStruct{})) |
| 268 | if err == nil { |
| 269 | t.Errorf("mapVal.ConvertToNative(struct) got %v, wanted error", ts) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | func TestDynamicMapConvertToNative_Struct_PrivateFieldError(t *testing.T) { |
| 274 | reg := newTestRegistry(t) |
| 275 | mapVal := NewDynamicMap(reg, map[string]any{ |
| 276 | "message": "hello", |
| 277 | "details": []string{"world", "universe"}, |
| 278 | "private": "private field", |
| 279 | }) |
| 280 | ts, err := mapVal.ConvertToNative(reflect.TypeOf(&testStruct{})) |
| 281 | if err == nil { |
| 282 | t.Errorf("mapVal.ConvertToNative(struct) got %v, wanted error", ts) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | func TestStringMapConvertToNative(t *testing.T) { |
| 287 | reg := newTestRegistry(t) |
| 288 | strMap := map[string]string{ |
| 289 | "first": "hello", |
| 290 | "second": "world", |
| 291 | } |
| 292 | mapVal := NewStringStringMap(reg, strMap) |
| 293 | val, err := mapVal.ConvertToNative(reflect.TypeOf(strMap)) |
| 294 | if err != nil { |
| 295 | t.Fatalf("mapVal.ConvertToNative(map[string]string) failed: %v", err) |
| 296 | } |
| 297 | if !reflect.DeepEqual(val.(map[string]string), strMap) { |
| 298 | t.Errorf("got not-equal, wanted equal for %v == %v", val, strMap) |
| 299 | } |
| 300 | val, err = mapVal.ConvertToNative(reflect.TypeOf(mapVal)) |
| 301 | if err != nil { |
| 302 | t.Fatalf("mapVal.ConvertToNative(baseMap) failed: %v", err) |
| 303 | } |
| 304 | if !reflect.DeepEqual(val, mapVal) { |
| 305 | t.Errorf("got not-equal, wanted equal for %v == %v", val, mapVal) |
| 306 | } |
| 307 | jsonVal, err := mapVal.ConvertToNative(JSONStructType) |
| 308 | if err != nil { |
| 309 | t.Fatalf("mapVal.ConvertToNative(jsonStructType) failed: %v", err) |
| 310 | } |
| 311 | jsonBytes, err := protojson.Marshal(jsonVal.(proto.Message)) |
| 312 | if err != nil { |
| 313 | t.Fatalf("protojson.Marshal() failed: %v", err) |
| 314 | } |
| 315 | jsonTxt := string(jsonBytes) |
| 316 | outMap := map[string]any{} |
| 317 | err = json.Unmarshal(jsonBytes, &outMap) |
| 318 | if err != nil { |
| 319 | t.Fatalf("json.Unmarshal(%q) failed: %v", jsonTxt, err) |
| 320 | } |
| 321 | if !reflect.DeepEqual(outMap, map[string]any{ |
| 322 | "first": "hello", |
| 323 | "second": "world", |
| 324 | }) { |
| 325 | t.Errorf("got json '%v', expected %v", jsonTxt, outMap) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | func TestDynamicMapConvertToType(t *testing.T) { |
| 330 | reg := newTestRegistry(t) |
| 331 | mapVal := NewDynamicMap(reg, map[string]string{"key": "value"}) |
| 332 | if mapVal.ConvertToType(MapType) != mapVal { |
| 333 | t.Error("mapVal.ConvertToType(MapType) could not be converted to a map.") |
| 334 | } |
| 335 | if mapVal.ConvertToType(TypeType) != MapType { |
| 336 | t.Error("mapVal.ConvertToType(TypeType) did not return a map type.") |
| 337 | } |
| 338 | if !IsError(mapVal.ConvertToType(ListType)) { |
| 339 | t.Error("mapVal.ConvertToType(ListType) returned a non-error.") |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | func TestStringMapConvertToType(t *testing.T) { |
| 344 | reg := newTestRegistry(t) |
| 345 | mapVal := reg.NativeToValue(map[string]string{"key": "value"}) |
| 346 | if mapVal.ConvertToType(MapType) != mapVal { |
| 347 | t.Error("mapVal.ConvertToType(MapType) could not be converted to a map.") |
| 348 | } |
| 349 | if mapVal.ConvertToType(TypeType) != MapType { |
| 350 | t.Error("mapVal.ConvertToType(TypeType) did not return the map type.") |
| 351 | } |
| 352 | if !IsError(mapVal.ConvertToType(ListType)) { |
| 353 | t.Error("mapVal.ConvertToType(ListType) did not error.") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func TestDynamicMapEqual_True(t *testing.T) { |
| 358 | reg := newTestRegistry(t) |
| 359 | mapVal := NewDynamicMap(reg, map[string]map[int32]float32{ |
| 360 | "nested": {1: -1.0, 2: 2.0}, |
| 361 | "empty": {}}) |
| 362 | if mapVal.Equal(mapVal) != True { |
| 363 | t.Error("mapVal.Equal(mapVal) did not return true") |
| 364 | } |
| 365 | |
| 366 | if nestedVal := mapVal.Get(String("nested")); IsError(nestedVal) { |
| 367 | t.Error(nestedVal) |
| 368 | } else if mapVal.Equal(nestedVal) == True || |
| 369 | nestedVal.Equal(mapVal) == True { |
| 370 | t.Error("Same length, but different key names did not result in error") |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | func TestStringMapEqual_True(t *testing.T) { |
| 375 | reg := newTestRegistry(t) |
| 376 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 377 | "first": "hello", |
| 378 | "second": "world"}) |
| 379 | if mapVal.Equal(mapVal) != True { |
| 380 | t.Error("mapVal.Equal(mapVal) did not return true") |
| 381 | } |
| 382 | equivDyn := NewDynamicMap(reg, map[string]string{ |
| 383 | "second": "world", |
| 384 | "first": "hello"}) |
| 385 | if mapVal.Equal(equivDyn) != True { |
| 386 | t.Error("mapVal.Equal(equivDyn) did not return true, and was key-order dependent") |
| 387 | } |
| 388 | equivJSON := NewJSONStruct(reg, &structpb.Struct{ |
| 389 | Fields: map[string]*structpb.Value{ |
| 390 | "first": structpb.NewStringValue("hello"), |
| 391 | "second": structpb.NewStringValue("world"), |
| 392 | }}) |
| 393 | if mapVal.Equal(equivJSON) != True && equivJSON.Equal(mapVal) != True { |
| 394 | t.Error("mapVal.Equal(equivJSON) did not return true") |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | func TestDynamicMapEqual_NotTrue(t *testing.T) { |
| 399 | reg := newTestRegistry(t) |
| 400 | mapVal := NewDynamicMap(reg, map[string]map[int32]float32{ |
| 401 | "nested": {1: -1.0, 2: 2.0}, |
| 402 | "empty": {}}) |
| 403 | other := NewDynamicMap(reg, map[string]map[int64]float64{ |
| 404 | "nested": {1: -1.0, 2: 2.0, 3: 3.14}, |
| 405 | "empty": {}}) |
| 406 | if mapVal.Equal(other) != False { |
| 407 | t.Error("mapVal.Equal(other) did not return false.") |
| 408 | } |
| 409 | other = NewDynamicMap(reg, map[string]map[int64]float64{ |
| 410 | "nested": {1: -1.0, 2: 2.0, 3: 3.14}, |
| 411 | "absent": {}}) |
| 412 | if mapVal.Equal(other) != False { |
| 413 | t.Error("mapVal.Equal(other) did not return false.") |
| 414 | } |
| 415 | if mapVal.Equal(NullValue) != False { |
| 416 | t.Errorf("mapVal.Equal(NullValue) returned %v, wanted false", mapVal.Equal(NullValue)) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | func TestStringMapEqual_NotTrue(t *testing.T) { |
| 421 | reg := newTestRegistry(t) |
| 422 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 423 | "first": "hello", |
| 424 | "second": "world"}) |
| 425 | if mapVal.Equal(mapVal) != True { |
| 426 | t.Error("mapVal.Equal(mapVal) did not return true") |
| 427 | } |
| 428 | other := NewStringStringMap(reg, map[string]string{ |
| 429 | "second": "world", |
| 430 | "first": "goodbye"}) |
| 431 | if mapVal.Equal(other) != False { |
| 432 | t.Error("mapVal.Equal(other) with same keys and different values did not return false") |
| 433 | } |
| 434 | other = NewStringStringMap(reg, map[string]string{ |
| 435 | "first": "hello"}) |
| 436 | if mapVal.Equal(other) != False { |
| 437 | t.Error("mapVal.Equal(other) between maps of different size did not return false") |
| 438 | } |
| 439 | other = NewStringStringMap(reg, map[string]string{ |
| 440 | "first": "hello", |
| 441 | "third": "goodbye"}) |
| 442 | if mapVal.Equal(other) != False { |
| 443 | t.Error("mapVal.Equal(other) between maps with different keys did not return false") |
| 444 | } |
| 445 | other = NewDynamicMap(reg, map[string]any{ |
| 446 | "first": "hello", |
| 447 | "second": 1}) |
| 448 | if IsError(mapVal.Equal(other)) { |
| 449 | t.Error("mapVal.Equal(other) between maps with same keys and different value types errored, wanted 'false'") |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | func TestDynamicMapGet(t *testing.T) { |
| 454 | reg := newTestRegistry(t) |
| 455 | mapVal := NewDynamicMap(reg, map[string]map[int32]float32{ |
| 456 | "nested": {1: -1.0, 2: 2.0}, |
| 457 | "empty": {}}) |
| 458 | nestedVal, ok := mapVal.Get(String("nested")).(traits.Mapper) |
| 459 | if !ok { |
| 460 | t.Fatalf("mapVal.Get('nested') got %v, wanted map value", mapVal.Get(String("nested"))) |
| 461 | } |
| 462 | floatVal := nestedVal.(traits.Indexer).Get(Int(1)) |
| 463 | if floatVal.Equal(Double(-1.0)) != True { |
| 464 | t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal) |
| 465 | } |
| 466 | err := mapVal.Get(String("absent")) |
| 467 | if !IsError(err) || err.(*Err).Error() != "no such key: absent" { |
| 468 | t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err) |
| 469 | } |
| 470 | err = nestedVal.Get(String("bad_key")) |
| 471 | if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" { |
| 472 | t.Errorf("nestedVal.Get('bad_key') errored %v, wanted no such key: bad_key.", err) |
| 473 | } |
| 474 | empty, ok := mapVal.Get(String("empty")).(traits.Mapper) |
| 475 | if !ok { |
| 476 | t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty"))) |
| 477 | } |
| 478 | err = empty.Get(String("hello")) |
| 479 | if !IsError(err) || err.(*Err).Error() != "no such key: hello" { |
| 480 | t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err) |
| 481 | } |
| 482 | err = empty.Get(Double(-1.0)) |
| 483 | if !IsError(err) || err.(*Err).Error() != "no such key: -1" { |
| 484 | t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | func TestStringIfaceMapGet(t *testing.T) { |
| 489 | reg := newTestRegistry(t) |
| 490 | mapVal := NewStringInterfaceMap(reg, map[string]any{ |
| 491 | "nested": map[int32]float64{1: -1.0, 2: 2.0}, |
| 492 | "empty": map[string]any{}, |
| 493 | }) |
| 494 | nestedVal, ok := mapVal.Get(String("nested")).(traits.Mapper) |
| 495 | if !ok { |
| 496 | t.Fatalf("mapVal.Get('nested') got %v, wanted map value", mapVal.Get(String("nested"))) |
| 497 | } |
| 498 | floatVal := nestedVal.(traits.Indexer).Get(Int(1)) |
| 499 | if floatVal.Equal(Double(-1.0)) != True { |
| 500 | t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal) |
| 501 | } |
| 502 | err := mapVal.Get(String("absent")) |
| 503 | if !IsError(err) || err.(*Err).Error() != "no such key: absent" { |
| 504 | t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err) |
| 505 | } |
| 506 | err = nestedVal.Get(String("bad_key")) |
| 507 | if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" { |
| 508 | t.Errorf("nestedVal.Get('bad_key') got %v, no such key: bad_key", err) |
| 509 | } |
| 510 | empty, ok := mapVal.Get(String("empty")).(traits.Mapper) |
| 511 | if !ok { |
| 512 | t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty"))) |
| 513 | } |
| 514 | err = empty.Get(String("hello")) |
| 515 | if !IsError(err) || err.(*Err).Error() != "no such key: hello" { |
| 516 | t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err) |
| 517 | } |
| 518 | err = empty.Get(Double(-1.0)) |
| 519 | if !IsError(err) || err.(*Err).Error() != "no such key: -1" { |
| 520 | t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err) |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | func TestStringMapGet(t *testing.T) { |
| 525 | reg := newTestRegistry(t) |
| 526 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 527 | "first": "hello", |
| 528 | "second": "world"}) |
| 529 | val := mapVal.Get(String("first")) |
| 530 | if val.Equal(String("hello")) != True { |
| 531 | t.Errorf("mapVal.Get('first') '%v', wanted 'hello'", val) |
| 532 | } |
| 533 | if !IsError(mapVal.Get(Int(1))) { |
| 534 | t.Error("mapVal.Get(1) got real value, wanted error") |
| 535 | } |
| 536 | if !IsError(mapVal.Get(String("third"))) { |
| 537 | t.Error("mapVal.Get('third') got real value, wanted error") |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | func TestRefValMapGet(t *testing.T) { |
| 542 | reg := newTestRegistry(t) |
| 543 | mapVal := NewRefValMap(reg, map[ref.Val]ref.Val{ |
| 544 | String("nested"): NewRefValMap(reg, map[ref.Val]ref.Val{ |
| 545 | Int(1): Double(-1.0), Int(2): Double(2.0), |
| 546 | }), |
| 547 | String("empty"): NewRefValMap(reg, map[ref.Val]ref.Val{}), |
| 548 | }) |
| 549 | nestedVal, ok := mapVal.Get(String("nested")).(traits.Mapper) |
| 550 | if !ok { |
| 551 | t.Fatalf("mapVal.Get('nested') got %v, wanted map value", mapVal.Get(String("nested"))) |
| 552 | } |
| 553 | floatVal := nestedVal.(traits.Indexer).Get(Int(1)) |
| 554 | if floatVal.Equal(Double(-1.0)) != True { |
| 555 | t.Errorf("nestedVal.Get(1) got %v, wanted -1.0", floatVal) |
| 556 | } |
| 557 | err := mapVal.Get(String("absent")) |
| 558 | if !IsError(err) || err.(*Err).Error() != "no such key: absent" { |
| 559 | t.Errorf("mapVal.Get('absent') got %v, wanted no such key: absent.", err) |
| 560 | } |
| 561 | err = nestedVal.Get(String("bad_key")) |
| 562 | if !IsError(err) || err.(*Err).Error() != "no such key: bad_key" { |
| 563 | t.Errorf("nestedVal.Get('bad_key') got %v, wanted no such key: bad_key.", err) |
| 564 | } |
| 565 | empty, ok := mapVal.Get(String("empty")).(traits.Mapper) |
| 566 | if !ok { |
| 567 | t.Fatalf("mapVal.Get('empty') got %v, wanted empty map", mapVal.Get(String("empty"))) |
| 568 | } |
| 569 | err = empty.Get(String("hello")) |
| 570 | if !IsError(err) || err.(*Err).Error() != "no such key: hello" { |
| 571 | t.Errorf("empty.Get('hello') got %v, wanted no such key: hello", err) |
| 572 | } |
| 573 | err = empty.Get(Double(-1.0)) |
| 574 | if !IsError(err) || err.(*Err).Error() != "no such key: -1" { |
| 575 | t.Errorf("empty.Get(-1.0) got %v, wanted no such key: -1", err) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | func TestMapIsZeroValue(t *testing.T) { |
| 580 | msg := &proto3pb.TestAllTypes{ |
| 581 | MapStringString: map[string]string{ |
| 582 | "hello": "world", |
| 583 | }, |
| 584 | } |
| 585 | reg := newTestRegistry(t, msg) |
| 586 | obj := reg.NativeToValue(msg).(traits.Indexer) |
| 587 | |
| 588 | tests := []struct { |
| 589 | val any |
| 590 | isZeroValue bool |
| 591 | }{ |
| 592 | { |
| 593 | val: map[int]int{}, |
| 594 | isZeroValue: true, |
| 595 | }, |
| 596 | { |
| 597 | val: map[string]any{}, |
| 598 | isZeroValue: true, |
| 599 | }, |
| 600 | { |
| 601 | val: map[string]string{}, |
| 602 | isZeroValue: true, |
| 603 | }, |
| 604 | { |
| 605 | val: map[ref.Val]ref.Val{}, |
| 606 | isZeroValue: true, |
| 607 | }, |
| 608 | { |
| 609 | val: &structpb.Struct{}, |
| 610 | isZeroValue: true, |
| 611 | }, |
| 612 | { |
| 613 | val: obj.Get(String("map_int64_nested_type")), |
| 614 | isZeroValue: true, |
| 615 | }, |
| 616 | { |
| 617 | val: map[int]int{1: 1}, |
| 618 | isZeroValue: false, |
| 619 | }, |
| 620 | { |
| 621 | val: map[string]any{"hello": []any{}}, |
| 622 | isZeroValue: false, |
| 623 | }, |
| 624 | { |
| 625 | val: map[string]string{"": ""}, |
| 626 | isZeroValue: false, |
| 627 | }, |
| 628 | { |
| 629 | val: map[ref.Val]ref.Val{False: True}, |
| 630 | isZeroValue: false, |
| 631 | }, |
| 632 | { |
| 633 | val: &structpb.Struct{ |
| 634 | Fields: map[string]*structpb.Value{ |
| 635 | "": structpb.NewNullValue(), |
| 636 | }, |
| 637 | }, |
| 638 | isZeroValue: false, |
| 639 | }, |
| 640 | { |
| 641 | val: obj.Get(String("map_string_string")), |
| 642 | isZeroValue: false, |
| 643 | }, |
| 644 | } |
| 645 | for i, tst := range tests { |
| 646 | tc := tst |
| 647 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 648 | v := DefaultTypeAdapter.NativeToValue(tc.val) |
| 649 | zv, ok := v.(traits.Zeroer) |
| 650 | if !ok { |
| 651 | t.Fatalf("%v could not be converted to a zero-valuer type", tc.val) |
| 652 | } |
| 653 | if zv.IsZeroValue() != tc.isZeroValue { |
| 654 | t.Errorf("%v.IsZeroValue() got %t, wanted %t", v, zv.IsZeroValue(), tc.isZeroValue) |
| 655 | } |
| 656 | }) |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | func TestDynamicMapIterator(t *testing.T) { |
| 661 | reg := newTestRegistry(t) |
| 662 | mapVal := NewDynamicMap(reg, map[string]map[int32]float32{ |
| 663 | "nested": {1: -1.0, 2: 2.0}, |
| 664 | "empty": {}}) |
| 665 | it := mapVal.Iterator() |
| 666 | var i = 0 |
| 667 | var fieldNames []any |
| 668 | for ; it.HasNext() == True; i++ { |
| 669 | fieldName := it.Next() |
| 670 | if value := mapVal.Get(fieldName); IsError(value) { |
| 671 | t.Error(value) |
| 672 | } else { |
| 673 | fieldNames = append(fieldNames, fieldName) |
| 674 | } |
| 675 | } |
| 676 | if len(fieldNames) != 2 { |
| 677 | t.Errorf("Did not find the correct number of fields: %v", fieldNames) |
| 678 | } |
| 679 | if it.Next() != nil { |
| 680 | t.Error("Iterator ran off the end of the field names") |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | func TestStringMapIterator(t *testing.T) { |
| 685 | reg := newTestRegistry(t) |
| 686 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 687 | "first": "hello", |
| 688 | "second": "world"}) |
| 689 | it := mapVal.Iterator() |
| 690 | var i = 0 |
| 691 | var fieldNames []any |
| 692 | for ; it.HasNext() == True; i++ { |
| 693 | fieldName := it.Next() |
| 694 | if value := mapVal.Get(fieldName); IsError(value) { |
| 695 | t.Error(value) |
| 696 | } else { |
| 697 | fieldNames = append(fieldNames, fieldName) |
| 698 | } |
| 699 | } |
| 700 | if len(fieldNames) != 2 { |
| 701 | t.Errorf("Did not find the correct number of fields: %v", fieldNames) |
| 702 | } |
| 703 | fieldsMap := map[string]bool{ |
| 704 | "first": false, |
| 705 | "second": false, |
| 706 | } |
| 707 | expectedMap := map[string]bool{ |
| 708 | "first": true, |
| 709 | "second": true, |
| 710 | } |
| 711 | for _, fieldName := range fieldNames { |
| 712 | key := string(fieldName.(String)) |
| 713 | if _, found := fieldsMap[key]; found { |
| 714 | fieldsMap[key] = true |
| 715 | } |
| 716 | } |
| 717 | if !reflect.DeepEqual(fieldsMap, expectedMap) { |
| 718 | t.Errorf("Got '%v', wanted '%v'", fieldsMap, expectedMap) |
| 719 | } |
| 720 | if it.Next() != nil { |
| 721 | t.Error("Iterator ran off the end of the field names") |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | func TestDynamicMapSize(t *testing.T) { |
| 726 | reg := newTestRegistry(t) |
| 727 | mapVal := NewDynamicMap(reg, map[string]int{ |
| 728 | "first": 1, |
| 729 | "second": 2}) |
| 730 | if mapVal.Size() != Int(2) { |
| 731 | t.Errorf("mapVal.Size() got '%v', expected 2", mapVal.Size()) |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | func TestStringMapSize(t *testing.T) { |
| 736 | reg := newTestRegistry(t) |
| 737 | mapVal := NewStringStringMap(reg, map[string]string{ |
| 738 | "first": "hello", |
| 739 | "second": "world"}) |
| 740 | if mapVal.Size() != Int(2) { |
| 741 | t.Errorf("mapVal.Size() got '%v', expected 2", mapVal.Size()) |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | func TestProtoMap(t *testing.T) { |
| 746 | strMap := map[string]string{ |
| 747 | "hello": "world", |
| 748 | "goodbye": "for now", |
| 749 | "welcome": "back", |
| 750 | } |
| 751 | msg := &proto3pb.TestAllTypes{MapStringString: strMap} |
| 752 | reg := newTestRegistry(t, msg) |
| 753 | obj := reg.NativeToValue(msg).(traits.Indexer) |
| 754 | |
| 755 | // Test a simple proto map of string string. |
| 756 | field := obj.Get(String("map_string_string")) |
| 757 | mapVal, ok := field.(traits.Mapper) |
| 758 | if !ok { |
| 759 | t.Fatalf("obj.Get('map_string_string') did not return map: (%T)%v", field, field) |
| 760 | } |
| 761 | // CEL type conversion tests. |
| 762 | if mapVal.ConvertToType(MapType) != mapVal { |
| 763 | t.Errorf("mapVal.ConvertToType(MapType) got %v, wanted map type", mapVal.ConvertToType(MapType)) |
| 764 | } |
| 765 | if mapVal.ConvertToType(TypeType) != MapType { |
| 766 | t.Errorf("mapVal.ConvertToType(TypeType) got %v, wanted type type", mapVal.ConvertToType(TypeType)) |
| 767 | } |
| 768 | conv := mapVal.ConvertToType(ListType) |
| 769 | if !IsError(conv) { |
| 770 | t.Errorf("mapVal.ConvertToType(ListType) got %v, wanted error", conv) |
| 771 | } |
| 772 | // Size test |
| 773 | if mapVal.Size() != Int(len(strMap)) { |
| 774 | t.Errorf("mapVal.Size() got %d, wanted %d", mapVal.Size(), len(strMap)) |
| 775 | } |
| 776 | // Contains, Find, and Get tests. |
| 777 | for k, v := range strMap { |
| 778 | if mapVal.Contains(reg.NativeToValue(k)) != True { |
| 779 | t.Errorf("mapVal.Contains() missing key: %v", k) |
| 780 | } |
| 781 | kv := mapVal.Get(reg.NativeToValue(k)) |
| 782 | if kv.Equal(reg.NativeToValue(v)) != True { |
| 783 | t.Errorf("mapVal.Get(%v) got value %v wanted %v", k, kv, v) |
| 784 | } |
| 785 | } |
| 786 | // Equality test |
| 787 | refStrMap := reg.NativeToValue(strMap) |
| 788 | if refStrMap.Equal(mapVal) != True || mapVal.Equal(refStrMap) != True { |
| 789 | t.Errorf("mapVal.Equal(refStrMap) not equal to itself: ref.Val %v != ref.Val %v", refStrMap, mapVal) |
| 790 | } |
| 791 | // Iterator test |
| 792 | it := mapVal.Iterator() |
| 793 | mapValCopy := map[ref.Val]ref.Val{} |
| 794 | for it.HasNext() == True { |
| 795 | key := it.Next() |
| 796 | mapValCopy[key] = mapVal.Get(key) |
| 797 | } |
| 798 | mapVal2 := reg.NativeToValue(mapValCopy) |
| 799 | if mapVal2.Equal(mapVal) != True || mapVal.Equal(mapVal2) != True { |
| 800 | t.Errorf("mapVal.Equal(copy) not equal to original: cel ref.Val %v != ref.Val %v", mapVal2, mapVal) |
| 801 | } |
| 802 | if mapVal.Equal(NullValue) != False { |
| 803 | t.Errorf("mapVal.Equal(NullValue) got %v, wanted false", mapVal.Equal(NullValue)) |
| 804 | } |
| 805 | convMap, err := mapVal.ConvertToNative(reflect.TypeOf(strMap)) |
| 806 | if err != nil { |
| 807 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 808 | } |
| 809 | if !reflect.DeepEqual(strMap, convMap) { |
| 810 | t.Errorf("mapVal.ConvertToNative() got map %v, wanted %v", convMap, strMap) |
| 811 | } |
| 812 | // Inequality tests. |
| 813 | strNeMap := map[string]string{ |
| 814 | "hello": "world", |
| 815 | "goodbye": "forever", |
| 816 | "welcome": "back", |
| 817 | } |
| 818 | mapNeVal := reg.NativeToValue(strNeMap) |
| 819 | if mapNeVal.Equal(mapVal) != False || mapVal.Equal(mapNeVal) != False { |
| 820 | t.Error("mapNeVal.Equal(mapVal) returned true, wanted false") |
| 821 | } |
| 822 | strNeMap = map[string]string{ |
| 823 | "hello": "world", |
| 824 | "goodbe": "for now", |
| 825 | "welcome": "back", |
| 826 | } |
| 827 | mapNeVal = reg.NativeToValue(strNeMap) |
| 828 | if mapNeVal.Equal(mapVal) != False || mapVal.Equal(mapNeVal) != False { |
| 829 | t.Error("mapNeVal.Equal(mapVal) returned true, wanted false") |
| 830 | } |
| 831 | mapNeVal = reg.NativeToValue(map[string]string{}) |
| 832 | if mapNeVal.Equal(mapVal) != False || mapVal.Equal(mapNeVal) != False { |
| 833 | t.Error("mapNeVal.Equal(mapVal) returned true, wanted false") |
| 834 | } |
| 835 | mapNeMap := map[int64]int64{ |
| 836 | 1: 9, |
| 837 | 2: 1, |
| 838 | 3: 1, |
| 839 | } |
| 840 | mapNeVal = reg.NativeToValue(mapNeMap) |
| 841 | if IsError(mapNeVal.Equal(mapVal)) || IsError(mapVal.Equal(mapNeVal)) { |
| 842 | t.Error("mapNeVal.Equal(mapVal) returned error, wanted false") |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | func TestProtoMapGet(t *testing.T) { |
| 847 | strMap := map[string]string{ |
| 848 | "hello": "world", |
| 849 | "goodbye": "for now", |
| 850 | "welcome": "back", |
| 851 | } |
| 852 | msg := &proto3pb.TestAllTypes{MapStringString: strMap} |
| 853 | reg := newTestRegistry(t, msg) |
| 854 | obj := reg.NativeToValue(msg).(traits.Indexer) |
| 855 | field := obj.Get(String("map_string_string")) |
| 856 | mapVal, ok := field.(traits.Mapper) |
| 857 | if !ok { |
| 858 | t.Fatalf("obj.Get(map_string_string) failed: %v", field) |
| 859 | } |
| 860 | v := mapVal.Get(String("hello")) |
| 861 | if v.Equal(String("world")) == False { |
| 862 | t.Errorf("mapVal.Get('hello') got %v, wanted 'world'", v) |
| 863 | } |
| 864 | notFound := mapVal.Get(String("not_found")) |
| 865 | if !IsError(notFound) || !strings.Contains(notFound.(*Err).Error(), "no such key") { |
| 866 | t.Errorf("mapVal.Get('not_found') got %v, wanted no such key error", notFound) |
| 867 | } |
| 868 | badKey := mapVal.Get(Int(42)) |
| 869 | if !IsError(badKey) || !strings.Contains(badKey.(*Err).Error(), "no such key: 42") { |
| 870 | t.Errorf("mapVal.Get(42) got %v, wanted no such key: 42", badKey) |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | func TestProtoMapString(t *testing.T) { |
| 875 | strMap := map[string]string{ |
| 876 | "hello": "world", |
| 877 | } |
| 878 | reg := newTestRegistry(t) |
| 879 | m := reg.NativeToValue(strMap) |
| 880 | want := `{hello: world}` |
| 881 | if fmt.Sprintf("%v", m) != want { |
| 882 | t.Errorf("map.String() got %v, wanted %v", m, want) |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | func TestProtoMapConvertToNative(t *testing.T) { |
| 887 | strMap := map[string]string{ |
| 888 | "hello": "world", |
| 889 | "goodbye": "for now", |
| 890 | "welcome": "back", |
| 891 | } |
| 892 | msg := &proto3pb.TestAllTypes{MapStringString: strMap} |
| 893 | reg := newTestRegistry(t, msg) |
| 894 | obj := reg.NativeToValue(msg).(traits.Indexer) |
| 895 | // Test a simple proto map of string string. |
| 896 | field := obj.Get(String("map_string_string")) |
| 897 | mapVal, ok := field.(traits.Mapper) |
| 898 | if !ok { |
| 899 | t.Fatalf("obj.Get('map_string_string') did not return map: (%T)%v", field, field) |
| 900 | } |
| 901 | convMap, err := mapVal.ConvertToNative(reflect.TypeOf(map[string]any{})) |
| 902 | if err != nil { |
| 903 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 904 | } |
| 905 | for k, v := range convMap.(map[string]any) { |
| 906 | if strMap[k] != v { |
| 907 | t.Errorf("got differing values for key %q: got %v, wanted: %v", k, strMap[k], v) |
| 908 | } |
| 909 | } |
| 910 | mapVal2 := reg.NativeToValue(convMap) |
| 911 | if mapVal2.Equal(mapVal) != True || mapVal.Equal(mapVal2) != True { |
| 912 | t.Errorf("mapVal2.Equal(mapVal) returned false, wanted true") |
| 913 | } |
| 914 | convMap, err = mapVal.ConvertToNative(anyValueType) |
| 915 | if err != nil { |
| 916 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 917 | } |
| 918 | mapVal3 := reg.NativeToValue(convMap) |
| 919 | if mapVal3.Equal(mapVal) != True || mapVal.Equal(mapVal3) != True { |
| 920 | t.Errorf("mapVal3.Equal(mapVal) returned false, wanted true") |
| 921 | } |
| 922 | convMap, err = mapVal.ConvertToNative(JSONValueType) |
| 923 | if err != nil { |
| 924 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 925 | } |
| 926 | mapVal4 := reg.NativeToValue(convMap) |
| 927 | if mapVal4.Equal(mapVal) != True || mapVal.Equal(mapVal4) != True { |
| 928 | t.Errorf("mapVal4.Equal(mapVal) returned false, wanted true") |
| 929 | } |
| 930 | convMap, err = mapVal.ConvertToNative(reflect.TypeOf(&pb.Map{})) |
| 931 | if err != nil { |
| 932 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 933 | } |
| 934 | mapVal5 := reg.NativeToValue(convMap) |
| 935 | if mapVal5.Equal(mapVal) != True || mapVal.Equal(mapVal5) != True { |
| 936 | t.Errorf("mapVal5.Equal(mapVal) returned false, wanted true") |
| 937 | } |
| 938 | var mapper traits.Mapper = mapVal |
| 939 | convMap, err = mapVal.ConvertToNative(reflect.TypeOf(mapper)) |
| 940 | if err != nil { |
| 941 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 942 | } |
| 943 | mapVal6 := reg.NativeToValue(convMap) |
| 944 | if mapVal6.Equal(mapVal) != True || mapVal.Equal(mapVal6) != True { |
| 945 | t.Errorf("mapVal6.Equal(mapVal) returned false, wanted true") |
| 946 | } |
| 947 | _, err = mapVal.ConvertToNative(JSONListType) |
| 948 | if err == nil { |
| 949 | t.Fatalf("mapVal.ConvertToNative() succeeded for invalid type") |
| 950 | } |
| 951 | _, err = mapVal.ConvertToNative(reflect.TypeOf(map[int32]string{})) |
| 952 | if err == nil { |
| 953 | t.Fatalf("mapVal.ConvertToNative() succeeded for invalid type") |
| 954 | } |
| 955 | _, err = mapVal.ConvertToNative(reflect.TypeOf(map[string]int64{})) |
| 956 | if err == nil { |
| 957 | t.Fatalf("mapVal.ConvertToNative() succeeded for invalid type") |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | func TestProtoMapConvertToNative_NestedProto(t *testing.T) { |
| 962 | nestedTypeMap := map[int64]*proto3pb.NestedTestAllTypes{ |
| 963 | 1: { |
| 964 | Payload: &proto3pb.TestAllTypes{ |
| 965 | SingleBoolWrapper: wrapperspb.Bool(true), |
| 966 | }, |
| 967 | }, |
| 968 | 2: { |
| 969 | Child: &proto3pb.NestedTestAllTypes{ |
| 970 | Payload: &proto3pb.TestAllTypes{ |
| 971 | SingleTimestamp: tpb.New(time.Now()), |
| 972 | }, |
| 973 | }, |
| 974 | }, |
| 975 | } |
| 976 | msg := &proto3pb.TestAllTypes{MapInt64NestedType: nestedTypeMap} |
| 977 | reg := newTestRegistry(t, msg) |
| 978 | obj := reg.NativeToValue(msg).(traits.Indexer) |
| 979 | // Test a simple proto map of string string. |
| 980 | field := obj.Get(String("map_int64_nested_type")) |
| 981 | mapVal, ok := field.(traits.Mapper) |
| 982 | if !ok { |
| 983 | t.Fatalf("obj.Get('map_int64_nested_type') did not return map: (%T)%v", field, field) |
| 984 | } |
| 985 | convMap, err := mapVal.ConvertToNative(reflect.TypeOf(map[int32]any{})) |
| 986 | if err != nil { |
| 987 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 988 | } |
| 989 | for k, v := range convMap.(map[int32]any) { |
| 990 | if !proto.Equal(nestedTypeMap[int64(k)], v.(proto.Message)) { |
| 991 | t.Errorf("got differing values for key %q: got %v, wanted: %v", k, nestedTypeMap[int64(k)], v) |
| 992 | } |
| 993 | } |
| 994 | convMap, err = mapVal.ConvertToNative(reflect.TypeOf(map[int32]proto.Message{})) |
| 995 | if err != nil { |
| 996 | t.Fatalf("mapVal.ConvertToNative() failed: %v", err) |
| 997 | } |
| 998 | for k, v := range convMap.(map[int32]proto.Message) { |
| 999 | if !proto.Equal(nestedTypeMap[int64(k)], v) { |
| 1000 | t.Errorf("got differing values for key %q: got %v, wanted: %v", k, nestedTypeMap[int64(k)], v) |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | func TestMutableMap(t *testing.T) { |
| 1006 | m := NewMutableMap( |
| 1007 | DefaultTypeAdapter, |
| 1008 | map[ref.Val]ref.Val{String("hello"): String("world")}) |
| 1009 | m.Insert(String("goodbye"), String("cruel world")) |
| 1010 | im := m.ToImmutableMap() |
| 1011 | if im.Size() != Int(2) { |
| 1012 | t.Errorf("m.ToImmutableMap() had size %d, wanted 2", im.Size()) |
| 1013 | } |
| 1014 | if !IsError(m.Insert(String("goodbye"), String("happy world"))) { |
| 1015 | t.Error("m.Insert('goodbye', 'happy world') suceeded, wanted error") |
| 1016 | } |
| 1017 | m.Insert(String("well"), String("well")) |
| 1018 | if im.Size() != Int(2) { |
| 1019 | t.Errorf("m.Insert() mutated storage for immutable map: had size %d, wanted 2", im.Size()) |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | func TestMapFold(t *testing.T) { |
| 1024 | pbDB := pb.NewDb() |
| 1025 | fd, err := pbDB.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 1026 | if err != nil { |
| 1027 | t.Fatalf("pbdb.RegisterMessage(TestAllTypes) failed: %v", err) |
| 1028 | } |
| 1029 | td, found := fd.GetTypeDescription(string((&proto3pb.TestAllTypes{}).ProtoReflect().Descriptor().FullName())) |
| 1030 | if !found { |
| 1031 | t.Fatal("fd.GetTypeDescription() failed") |
| 1032 | } |
| 1033 | mapStrStrFD, found := td.FieldByName("map_string_string") |
| 1034 | if !found { |
| 1035 | t.Fatal("Could not find map_string_string field") |
| 1036 | } |
| 1037 | |
| 1038 | mapStrDesc := (&proto3pb.TestAllTypes{}).ProtoReflect().Descriptor().Fields().ByName("map_string_string") |
| 1039 | tests := []struct { |
| 1040 | m any |
| 1041 | folds int |
| 1042 | foldLimit int |
| 1043 | }{ |
| 1044 | { |
| 1045 | m: map[string]any{"a": 1, "b": 2}, |
| 1046 | folds: 2, |
| 1047 | foldLimit: 2, |
| 1048 | }, |
| 1049 | { |
| 1050 | m: map[string]string{"hello": "world"}, |
| 1051 | folds: 1, |
| 1052 | foldLimit: 2, |
| 1053 | }, |
| 1054 | { |
| 1055 | m: map[string]string{"hello": "world", "goodbye": "cruel world"}, |
| 1056 | folds: 1, |
| 1057 | foldLimit: 1, |
| 1058 | }, |
| 1059 | { |
| 1060 | m: map[ref.Val]ref.Val{}, |
| 1061 | folds: 0, |
| 1062 | foldLimit: 20, |
| 1063 | }, |
| 1064 | { |
| 1065 | m: map[ref.Val]ref.Val{ |
| 1066 | (String("hello")): String("world"), |
| 1067 | (String("goodbye")): String("cruel world"), |
| 1068 | }, |
| 1069 | folds: 1, |
| 1070 | foldLimit: 1, |
| 1071 | }, |
| 1072 | { |
| 1073 | m: testCreateStruct(t, map[string]any{ |
| 1074 | "hello": []any{}, |
| 1075 | "world": map[string]any{}, |
| 1076 | }), |
| 1077 | folds: 2, |
| 1078 | foldLimit: 2, |
| 1079 | }, |
| 1080 | { |
| 1081 | m: testCreateStruct(t, map[string]any{ |
| 1082 | "hello": []any{}, |
| 1083 | "world": map[string]any{}, |
| 1084 | }), |
| 1085 | folds: 1, |
| 1086 | foldLimit: 1, |
| 1087 | }, |
| 1088 | { |
| 1089 | m: (&proto3pb.TestAllTypes{ |
| 1090 | MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{ |
| 1091 | 1: {}, |
| 1092 | 2: {}, |
| 1093 | 3: {}, |
| 1094 | }, |
| 1095 | }).GetMapInt64NestedType(), |
| 1096 | folds: 3, |
| 1097 | foldLimit: 3, |
| 1098 | }, |
| 1099 | { |
| 1100 | m: (&proto3pb.TestAllTypes{ |
| 1101 | MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{ |
| 1102 | 1: {}, |
| 1103 | 2: {}, |
| 1104 | 3: {}, |
| 1105 | }, |
| 1106 | }).GetMapInt64NestedType(), |
| 1107 | folds: 2, |
| 1108 | foldLimit: 2, |
| 1109 | }, |
| 1110 | { |
| 1111 | m: &pb.Map{ |
| 1112 | Map: (&proto3pb.TestAllTypes{ |
| 1113 | MapStringString: map[string]string{ |
| 1114 | "1": "one", |
| 1115 | "2": "two", |
| 1116 | }, |
| 1117 | }).ProtoReflect().Get(mapStrDesc).Map(), |
| 1118 | KeyType: mapStrStrFD.KeyType, |
| 1119 | ValueType: mapStrStrFD.ValueType, |
| 1120 | }, |
| 1121 | folds: 1, |
| 1122 | foldLimit: 1, |
| 1123 | }, |
| 1124 | } |
| 1125 | reg := NewEmptyRegistry() |
| 1126 | for i, tst := range tests { |
| 1127 | tc := tst |
| 1128 | m := reg.NativeToValue(tc.m).(traits.Mapper) |
| 1129 | foldKinds := map[string]traits.Foldable{ |
| 1130 | "modern": ToFoldableMap(m), |
| 1131 | "legacy": ToFoldableMap(proxyLegacyMap{proxy: m}), |
| 1132 | } |
| 1133 | for foldKind, foldable := range foldKinds { |
| 1134 | t.Run(fmt.Sprintf("[%d]%s", i, foldKind), func(t *testing.T) { |
| 1135 | f := &testMapFolder{foldLimit: tc.foldLimit} |
| 1136 | foldable.Fold(f) |
| 1137 | if f.folds != tc.folds { |
| 1138 | t.Errorf("m.Fold(f) got %d, wanted %d folds", f.folds, tc.folds) |
| 1139 | } |
| 1140 | }) |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | func TestInsertMapKeyValue_MutableMapper(t *testing.T) { |
| 1146 | m := NewMutableMap(DefaultTypeAdapter, map[ref.Val]ref.Val{String("first"): Int(1)}) |
| 1147 | modified := InsertMapKeyValue(m, String("second"), Int(2)) |
| 1148 | if IsError(modified) { |
| 1149 | t.Fatalf("InsertMapKeyValue() got error: %v, wanted insertion", modified) |
| 1150 | } |
| 1151 | if modified != m { |
| 1152 | t.Fatalf("InsertMapKeyValue() created a new map for a mutable input: %v", modified) |
| 1153 | } |
| 1154 | im := m.ToImmutableMap() |
| 1155 | if _, found := im.Find(String("first")); !found { |
| 1156 | t.Errorf("InsertMapKeyValue() did not preserve entry 'first': %v", im) |
| 1157 | } |
| 1158 | if _, found := im.Find(String("second")); !found { |
| 1159 | t.Errorf("InsertMapKeyValue() did not insert entry 'second': %v", im) |
| 1160 | } |
| 1161 | if !IsError(InsertMapKeyValue(m, String("second"), Int(3))) { |
| 1162 | t.Errorf("InsertMapKeyValue('second', 3) modified the map instead of erroring: %v", m) |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | func TestInsertMapKeyValue_Mapper(t *testing.T) { |
| 1167 | m := NewRefValMap(DefaultTypeAdapter, map[ref.Val]ref.Val{String("first"): Int(1)}) |
| 1168 | modified := InsertMapKeyValue(m, String("second"), Int(2)) |
| 1169 | if IsError(modified) { |
| 1170 | t.Fatalf("InsertMapKeyValue() got error: %v, wanted insertion", modified) |
| 1171 | } |
| 1172 | if modified == m { |
| 1173 | t.Fatalf("InsertMapKeyValue() modified an immutable input: %v", modified) |
| 1174 | } |
| 1175 | im := modified.(traits.Mapper) |
| 1176 | if _, found := im.Find(String("first")); !found { |
| 1177 | t.Errorf("InsertMapKeyValue() did not preserve entry 'first': %v", im) |
| 1178 | } |
| 1179 | if _, found := im.Find(String("second")); !found { |
| 1180 | t.Errorf("InsertMapKeyValue() did not insert entry 'second': %v", im) |
| 1181 | } |
| 1182 | if !IsError(InsertMapKeyValue(im, String("second"), Int(3))) { |
| 1183 | t.Errorf("InsertMapKeyValue('second', 3) modified the map instead of erroring: %v", m) |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | type testMapFolder struct { |
| 1188 | foldLimit int |
| 1189 | folds int |
| 1190 | } |
| 1191 | |
| 1192 | func (f *testMapFolder) FoldEntry(k, v any) bool { |
| 1193 | if f.foldLimit != 0 { |
| 1194 | if f.folds >= f.foldLimit { |
| 1195 | return false |
| 1196 | } |
| 1197 | } |
| 1198 | f.folds++ |
| 1199 | return true |
| 1200 | } |
| 1201 | |
| 1202 | func testCreateStruct(t *testing.T, m map[string]any) *structpb.Struct { |
| 1203 | t.Helper() |
| 1204 | v, err := structpb.NewStruct(m) |
| 1205 | if err != nil { |
| 1206 | t.Fatalf("structpb.NewStruct(m) failed: %v", err) |
| 1207 | } |
| 1208 | return v |
| 1209 | } |
| 1210 | |
| 1211 | // proxyLegacyMap omits the foldable interfaces associated with all core Mapper implementations |
| 1212 | type proxyLegacyMap struct { |
| 1213 | proxy traits.Mapper |
| 1214 | } |
| 1215 | |
| 1216 | func (m proxyLegacyMap) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 1217 | return m.proxy.ConvertToNative(typeDesc) |
| 1218 | } |
| 1219 | |
| 1220 | func (m proxyLegacyMap) ConvertToType(typeValue ref.Type) ref.Val { |
| 1221 | return m.proxy.ConvertToType(typeValue) |
| 1222 | } |
| 1223 | |
| 1224 | func (m proxyLegacyMap) Equal(other ref.Val) ref.Val { |
| 1225 | return m.proxy.Equal(other) |
| 1226 | } |
| 1227 | |
| 1228 | func (m proxyLegacyMap) Type() ref.Type { |
| 1229 | return m.proxy.Type() |
| 1230 | } |
| 1231 | |
| 1232 | func (m proxyLegacyMap) Value() any { |
| 1233 | return m.proxy.Value() |
| 1234 | } |
| 1235 | |
| 1236 | func (m proxyLegacyMap) Contains(value ref.Val) ref.Val { |
| 1237 | return m.proxy.Contains(value) |
| 1238 | } |
| 1239 | |
| 1240 | func (m proxyLegacyMap) Find(key ref.Val) (ref.Val, bool) { |
| 1241 | return m.proxy.Find(key) |
| 1242 | } |
| 1243 | |
| 1244 | func (m proxyLegacyMap) Get(index ref.Val) ref.Val { |
| 1245 | return m.proxy.Get(index) |
| 1246 | } |
| 1247 | |
| 1248 | func (m proxyLegacyMap) Iterator() traits.Iterator { |
| 1249 | return m.proxy.Iterator() |
| 1250 | } |
| 1251 | |
| 1252 | func (m proxyLegacyMap) Size() ref.Val { |
| 1253 | return m.proxy.Size() |
| 1254 | } |