|
| 1 | +// Copyright 2025 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 | +// https://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 rust |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/googleapis/librarian/internal/sidekick/internal/api" |
| 22 | +) |
| 23 | + |
| 24 | +func TestMapKeyAnnotations(t *testing.T) { |
| 25 | + for _, test := range []struct { |
| 26 | + wantSerdeAs string |
| 27 | + typez api.Typez |
| 28 | + }{ |
| 29 | + {"wkt::internal::I32", api.INT32_TYPE}, |
| 30 | + {"wkt::internal::I32", api.SFIXED32_TYPE}, |
| 31 | + {"wkt::internal::I32", api.SINT32_TYPE}, |
| 32 | + {"wkt::internal::I64", api.INT64_TYPE}, |
| 33 | + {"wkt::internal::I64", api.SFIXED64_TYPE}, |
| 34 | + {"wkt::internal::I64", api.SINT64_TYPE}, |
| 35 | + {"wkt::internal::U32", api.UINT32_TYPE}, |
| 36 | + {"wkt::internal::U32", api.FIXED32_TYPE}, |
| 37 | + {"wkt::internal::U64", api.UINT64_TYPE}, |
| 38 | + {"wkt::internal::U64", api.FIXED64_TYPE}, |
| 39 | + {"serde_with::DisplayFromStr", api.BOOL_TYPE}, |
| 40 | + } { |
| 41 | + mapMessage := &api.Message{ |
| 42 | + Name: "$map<unused, unused>", |
| 43 | + ID: "$map<unused, unused>", |
| 44 | + Package: "$", |
| 45 | + IsMap: true, |
| 46 | + Fields: []*api.Field{ |
| 47 | + { |
| 48 | + Name: "key", |
| 49 | + ID: "$map<unused, unused>.key", |
| 50 | + Typez: test.typez, |
| 51 | + TypezID: "unused", |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "value", |
| 55 | + ID: "$map<unused, unused>.value", |
| 56 | + Typez: api.STRING_TYPE, |
| 57 | + TypezID: "unused", |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + field := &api.Field{ |
| 62 | + Name: "field", |
| 63 | + JSONName: "field", |
| 64 | + ID: ".test.Message.field", |
| 65 | + Typez: api.MESSAGE_TYPE, |
| 66 | + TypezID: "$map<unused, unused>", |
| 67 | + } |
| 68 | + message := &api.Message{ |
| 69 | + Name: "TestMessage", |
| 70 | + Package: "test", |
| 71 | + ID: ".test.TestMessage", |
| 72 | + Documentation: "A test message.", |
| 73 | + Fields: []*api.Field{field}, |
| 74 | + } |
| 75 | + model := api.NewTestAPI([]*api.Message{message, mapMessage}, []*api.Enum{}, []*api.Service{}) |
| 76 | + api.CrossReference(model) |
| 77 | + api.LabelRecursiveFields(model) |
| 78 | + codec, err := newCodec("protobuf", map[string]string{}) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + annotateModel(model, codec) |
| 83 | + |
| 84 | + got := field.Codec.(*fieldAnnotations).SerdeAs |
| 85 | + want := fmt.Sprintf("std::collections::HashMap<%s, serde_with::Same>", test.wantSerdeAs) |
| 86 | + if got != want { |
| 87 | + t.Errorf("mismatch for %s, want=%q, got=%q", test.wantSerdeAs, want, got) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestMapValueAnnotations(t *testing.T) { |
| 93 | + for _, test := range []struct { |
| 94 | + spec string |
| 95 | + typez api.Typez |
| 96 | + typezID string |
| 97 | + wantSerdeAs string |
| 98 | + }{ |
| 99 | + {"protobuf", api.STRING_TYPE, "unused", "serde_with::Same"}, |
| 100 | + {"disco", api.STRING_TYPE, "unused", "serde_with::Same"}, |
| 101 | + {"protobuf", api.BYTES_TYPE, "unused", "serde_with::base64::Base64"}, |
| 102 | + {"disco", api.BYTES_TYPE, "unused", "serde_with::base64::Base64<serde_with::base64::UrlSafe>"}, |
| 103 | + {"protobuf", api.MESSAGE_TYPE, ".google.protobuf.BytesValue", "serde_with::base64::Base64"}, |
| 104 | + {"disco", api.MESSAGE_TYPE, ".google.protobuf.BytesValue", "serde_with::base64::Base64<serde_with::base64::UrlSafe>"}, |
| 105 | + |
| 106 | + {"protobuf", api.BOOL_TYPE, "unused", "serde_with::Same"}, |
| 107 | + {"protobuf", api.INT32_TYPE, "unused", "wkt::internal::I32"}, |
| 108 | + {"protobuf", api.SFIXED32_TYPE, "unused", "wkt::internal::I32"}, |
| 109 | + {"protobuf", api.SINT32_TYPE, "unused", "wkt::internal::I32"}, |
| 110 | + {"protobuf", api.INT64_TYPE, "unused", "wkt::internal::I64"}, |
| 111 | + {"protobuf", api.SFIXED64_TYPE, "unused", "wkt::internal::I64"}, |
| 112 | + {"protobuf", api.SINT64_TYPE, "unused", "wkt::internal::I64"}, |
| 113 | + {"protobuf", api.UINT32_TYPE, "unused", "wkt::internal::U32"}, |
| 114 | + {"protobuf", api.FIXED32_TYPE, "unused", "wkt::internal::U32"}, |
| 115 | + {"protobuf", api.UINT64_TYPE, "unused", "wkt::internal::U64"}, |
| 116 | + {"protobuf", api.FIXED64_TYPE, "unused", "wkt::internal::U64"}, |
| 117 | + |
| 118 | + {"protobuf", api.MESSAGE_TYPE, ".google.protobuf.UInt64Value", "wkt::internal::U64"}, |
| 119 | + {"protobuf", api.MESSAGE_TYPE, ".test.Message", "serde_with::Same"}, |
| 120 | + } { |
| 121 | + mapMessage := &api.Message{ |
| 122 | + Name: "$map<unused, unused>", |
| 123 | + ID: "$map<unused, unused>", |
| 124 | + Package: "$", |
| 125 | + IsMap: true, |
| 126 | + Fields: []*api.Field{ |
| 127 | + { |
| 128 | + Name: "key", |
| 129 | + ID: "$map<unused, unused>.key", |
| 130 | + Typez: api.INT32_TYPE, |
| 131 | + TypezID: "unused", |
| 132 | + }, |
| 133 | + { |
| 134 | + Name: "value", |
| 135 | + ID: "$map<unused, unused>.value", |
| 136 | + Typez: test.typez, |
| 137 | + TypezID: test.typezID, |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | + field := &api.Field{ |
| 142 | + Name: "field", |
| 143 | + JSONName: "field", |
| 144 | + ID: ".test.Message.field", |
| 145 | + Typez: api.MESSAGE_TYPE, |
| 146 | + TypezID: "$map<unused, unused>", |
| 147 | + } |
| 148 | + message := &api.Message{ |
| 149 | + Name: "Message", |
| 150 | + Package: "test", |
| 151 | + ID: ".test.Message", |
| 152 | + Documentation: "A test message.", |
| 153 | + Fields: []*api.Field{field}, |
| 154 | + } |
| 155 | + model := api.NewTestAPI([]*api.Message{message, mapMessage}, []*api.Enum{}, []*api.Service{}) |
| 156 | + api.CrossReference(model) |
| 157 | + api.LabelRecursiveFields(model) |
| 158 | + codec, err := newCodec(test.spec, map[string]string{}) |
| 159 | + if err != nil { |
| 160 | + t.Fatal(err) |
| 161 | + } |
| 162 | + annotateModel(model, codec) |
| 163 | + |
| 164 | + got := field.Codec.(*fieldAnnotations).SerdeAs |
| 165 | + want := fmt.Sprintf("std::collections::HashMap<wkt::internal::I32, %s>", test.wantSerdeAs) |
| 166 | + if got != want { |
| 167 | + t.Errorf("mismatch for %v, want=%q, got=%q", test, want, got) |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// A map without any SerdeAs mapping receives a special annotation. |
| 173 | +func TestMapAnnotationsSameSame(t *testing.T) { |
| 174 | + mapMessage := &api.Message{ |
| 175 | + Name: "$map<string, string>", |
| 176 | + ID: "$map<string, string>", |
| 177 | + Package: "$", |
| 178 | + IsMap: true, |
| 179 | + Fields: []*api.Field{ |
| 180 | + { |
| 181 | + Name: "key", |
| 182 | + ID: "$map<string, string>.key", |
| 183 | + Typez: api.STRING_TYPE, |
| 184 | + TypezID: "unused", |
| 185 | + }, |
| 186 | + { |
| 187 | + Name: "value", |
| 188 | + ID: "$map<string, string>.value", |
| 189 | + Typez: api.STRING_TYPE, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + field := &api.Field{ |
| 194 | + Name: "field", |
| 195 | + JSONName: "field", |
| 196 | + ID: ".test.Message.field", |
| 197 | + Typez: api.MESSAGE_TYPE, |
| 198 | + TypezID: "$map<unused, unused>", |
| 199 | + } |
| 200 | + message := &api.Message{ |
| 201 | + Name: "Message", |
| 202 | + Package: "test", |
| 203 | + ID: ".test.Message", |
| 204 | + Documentation: "A test message.", |
| 205 | + Fields: []*api.Field{field}, |
| 206 | + } |
| 207 | + model := api.NewTestAPI([]*api.Message{message, mapMessage}, []*api.Enum{}, []*api.Service{}) |
| 208 | + api.CrossReference(model) |
| 209 | + api.LabelRecursiveFields(model) |
| 210 | + codec, err := newCodec("protobuf", map[string]string{}) |
| 211 | + if err != nil { |
| 212 | + t.Fatal(err) |
| 213 | + } |
| 214 | + annotateModel(model, codec) |
| 215 | + |
| 216 | + got := field.Codec.(*fieldAnnotations).SerdeAs |
| 217 | + if got != "" { |
| 218 | + t.Errorf("mismatch for %v, got=%q", mapMessage, got) |
| 219 | + } |
| 220 | +} |
0 commit comments