@@ -23,6 +23,21 @@ import (
2323 "github.com/googleapis/librarian/internal/sidekick/api"
2424)
2525
26+ func newTestCodec (t * testing.T , specificationFormat , packageName string , options map [string ]string ) * codec {
27+ t .Helper ()
28+ codec , err := newCodec (specificationFormat , options )
29+ if err != nil {
30+ t .Fatal (err )
31+ }
32+ codec .packageMapping = map [string ]* packagez {
33+ "google.protobuf" : & packagez {name : "wkt" },
34+ }
35+ if packageName != "" {
36+ codec .packageMapping [packageName ] = & packagez {name : "external-rust-pkg" }
37+ }
38+ return codec
39+ }
40+
2641func TestFieldAnnotations (t * testing.T ) {
2742 key_field := & api.Field {Name : "key" , Typez : api .INT32_TYPE }
2843 value_field := & api.Field {Name : "value" , Typez : api .INT64_TYPE }
@@ -74,10 +89,7 @@ func TestFieldAnnotations(t *testing.T) {
7489 model .State .MessageByID [map_message .ID ] = map_message
7590 api .CrossReference (model )
7691 api .LabelRecursiveFields (model )
77- codec , err := newCodec ("protobuf" , map [string ]string {})
78- if err != nil {
79- t .Fatal (err )
80- }
92+ codec := newTestCodec (t , "protobuf" , "test" , map [string ]string {})
8193 annotateModel (model , codec )
8294 wantMessage := & messageAnnotation {
8395 Name : "TestMessage" ,
@@ -247,10 +259,7 @@ func TestRecursiveFieldAnnotations(t *testing.T) {
247259 model .State .MessageByID [map_message .ID ] = map_message
248260 api .CrossReference (model )
249261 api .LabelRecursiveFields (model )
250- codec , err := newCodec ("protobuf" , map [string ]string {})
251- if err != nil {
252- t .Fatal (err )
253- }
262+ codec := newTestCodec (t , "protobuf" , "test" , map [string ]string {})
254263 annotateModel (model , codec )
255264 wantMessage := & messageAnnotation {
256265 Name : "TestMessage" ,
@@ -440,10 +449,8 @@ func TestSameTypeNameFieldAnnotations(t *testing.T) {
440449 model .State .MessageByID [inner_message .ID ] = inner_message
441450 api .CrossReference (model )
442451 api .LabelRecursiveFields (model )
443- codec , err := newCodec ("protobuf" , map [string ]string {})
444- if err != nil {
445- t .Fatal (err )
446- }
452+ codec := newTestCodec (t , "protobuf" , "test" , map [string ]string {})
453+ codec .packageMapping ["test.v1.inner" ] = & packagez {name : "rusty-test-inner-v1" }
447454 annotateModel (model , codec )
448455 wantMessage := & messageAnnotation {
449456 Name : "TestMessage" ,
@@ -469,12 +476,12 @@ func TestSameTypeNameFieldAnnotations(t *testing.T) {
469476 SetterName : "map_field" ,
470477 BranchName : "MapField" ,
471478 FQMessageName : "crate::model::TestMessage" ,
472- FieldType : "std::collections::HashMap<i32,test.v1.inner ::TestMessage>" ,
473- PrimitiveFieldType : "std::collections::HashMap<i32,test.v1.inner ::TestMessage>" ,
479+ FieldType : "std::collections::HashMap<i32,rusty_test_inner_v1::model ::TestMessage>" ,
480+ PrimitiveFieldType : "std::collections::HashMap<i32,rusty_test_inner_v1::model ::TestMessage>" ,
474481 AddQueryParameter : `let builder = { use gaxi::query_parameter::QueryParameter; serde_json::to_value(&req.map_field).map_err(Error::ser)?.add(builder, "mapField") };` ,
475482 KeyType : "i32" ,
476483 KeyField : key_field ,
477- ValueType : "test.v1.inner ::TestMessage" ,
484+ ValueType : "rusty_test_inner_v1::model ::TestMessage" ,
478485 ValueField : value_field ,
479486 SerdeAs : "std::collections::HashMap<wkt::internal::I32, serde_with::Same>" ,
480487 SkipIfIsDefault : true ,
@@ -495,9 +502,9 @@ func TestSameTypeNameFieldAnnotations(t *testing.T) {
495502 SetterName : "oneof_field" ,
496503 BranchName : "OneofField" ,
497504 FQMessageName : "crate::model::TestMessage" ,
498- FieldType : "std::boxed::Box<test.v1.inner ::TestMessage>" ,
505+ FieldType : "std::boxed::Box<rusty_test_inner_v1::model ::TestMessage>" ,
499506 MessageType : inner_message ,
500- PrimitiveFieldType : "test.v1.inner ::TestMessage" ,
507+ PrimitiveFieldType : "rusty_test_inner_v1::model ::TestMessage" ,
501508 AddQueryParameter : `let builder = req.oneof_field().map(|p| serde_json::to_value(p).map_err(Error::ser) ).transpose()?.into_iter().fold(builder, |builder, p| { use gaxi::query_parameter::QueryParameter; p.add(builder, "oneofField") });` ,
502509 IsBoxed : true ,
503510 SkipIfIsDefault : true ,
@@ -519,9 +526,9 @@ func TestSameTypeNameFieldAnnotations(t *testing.T) {
519526 SetterName : "repeated_field" ,
520527 BranchName : "RepeatedField" ,
521528 FQMessageName : "crate::model::TestMessage" ,
522- FieldType : "std::vec::Vec<test.v1.inner ::TestMessage>" ,
529+ FieldType : "std::vec::Vec<rusty_test_inner_v1::model ::TestMessage>" ,
523530 MessageType : inner_message ,
524- PrimitiveFieldType : "test.v1.inner ::TestMessage" ,
531+ PrimitiveFieldType : "rusty_test_inner_v1::model ::TestMessage" ,
525532 AddQueryParameter : `let builder = req.repeated_field.as_ref().map(|p| serde_json::to_value(p).map_err(Error::ser) ).transpose()?.into_iter().fold(builder, |builder, v| { use gaxi::query_parameter::QueryParameter; v.add(builder, "repeatedField") });` ,
526533 SkipIfIsDefault : true ,
527534 AliasInExamples : "RepeatedField" ,
@@ -541,9 +548,9 @@ func TestSameTypeNameFieldAnnotations(t *testing.T) {
541548 SetterName : "message_field" ,
542549 BranchName : "MessageField" ,
543550 FQMessageName : "crate::model::TestMessage" ,
544- FieldType : "test.v1.inner ::TestMessage" ,
551+ FieldType : "rusty_test_inner_v1::model ::TestMessage" ,
545552 MessageType : inner_message ,
546- PrimitiveFieldType : "test.v1.inner ::TestMessage" ,
553+ PrimitiveFieldType : "rusty_test_inner_v1::model ::TestMessage" ,
547554 AddQueryParameter : `let builder = { use gaxi::query_parameter::QueryParameter; serde_json::to_value(&req.message_field).map_err(Error::ser)?.add(builder, "messageField") };` ,
548555 SkipIfIsDefault : true ,
549556 AliasInExamples : "MessageField" ,
@@ -594,10 +601,7 @@ func TestPrimitiveFieldAnnotations(t *testing.T) {
594601 model := api .NewTestAPI ([]* api.Message {message }, []* api.Enum {}, []* api.Service {})
595602 api .CrossReference (model )
596603 api .LabelRecursiveFields (model )
597- codec , err := newCodec ("protobuf" , map [string ]string {})
598- if err != nil {
599- t .Fatal (err )
600- }
604+ codec := newTestCodec (t , "protobuf" , "test" , map [string ]string {})
601605 annotateModel (model , codec )
602606
603607 wantField := & fieldAnnotations {
@@ -645,10 +649,7 @@ func TestBytesAnnotations(t *testing.T) {
645649 model := api .NewTestAPI ([]* api.Message {message }, []* api.Enum {}, []* api.Service {})
646650 api .CrossReference (model )
647651 api .LabelRecursiveFields (model )
648- codec , err := newCodec (test .sourceSpecification , map [string ]string {})
649- if err != nil {
650- t .Fatal (err )
651- }
652+ codec := newTestCodec (t , test .sourceSpecification , "test" , map [string ]string {})
652653 annotateModel (model , codec )
653654
654655 wantField := & fieldAnnotations {
0 commit comments