@@ -565,14 +565,27 @@ func TestBuildQueryLinesEnums(t *testing.T) {
565565 r := sample .Replication ()
566566 a := sample .Automatic ()
567567 enum := sample .EnumState ()
568+ foreignEnumState := & api.Enum {
569+ Name : "ForeignEnum" ,
570+ Package : "google.cloud.foo" ,
571+ ID : "google.cloud.foo.ForeignEnum" ,
572+ Values : []* api.EnumValue {
573+ {
574+ Name : "Enabled" ,
575+ Number : 1 ,
576+ },
577+ },
578+ }
579+
568580 model := api .NewTestAPI (
569581 []* api.Message {r , a , sample .CustomerManagedEncryption ()},
570- []* api.Enum {enum },
582+ []* api.Enum {enum , foreignEnumState },
571583 []* api.Service {})
572584 model .PackageName = "test"
573585 annotate := newAnnotateModel (model )
574- annotate .annotateModel (map [string ]string {})
575-
586+ annotate .annotateModel (map [string ]string {
587+ "prefix:google.cloud.foo" : "foo" ,
588+ })
576589 for _ , test := range []struct {
577590 enumField * api.Field
578591 want []string
@@ -594,6 +607,15 @@ func TestBuildQueryLinesEnums(t *testing.T) {
594607 Optional : true },
595608 []string {"if (result.optionalEnum != null) 'optionalJsonEnum': result.optionalEnum!.value" },
596609 },
610+ {
611+ & api.Field {
612+ Name : "enumName" ,
613+ JSONName : "jsonEnumName" ,
614+ Typez : api .ENUM_TYPE ,
615+ TypezID : foreignEnumState .ID ,
616+ Optional : false },
617+ []string {"if (result.enumName.isNotDefault) 'jsonEnumName': result.enumName.value" },
618+ },
597619 } {
598620 t .Run (test .enumField .Name , func (t * testing.T ) {
599621 got := annotate .buildQueryLines ([]string {}, "result." , "" , test .enumField , model .State )
@@ -715,6 +737,41 @@ func TestBuildQueryLinesMessages(t *testing.T) {
715737
716738func TestCreateFromJsonLine (t * testing.T ) {
717739 secret := sample .Secret ()
740+ enumState := sample .EnumState ()
741+
742+ foreignMessage := & api.Message {
743+ Name : "Foo" ,
744+ Package : "google.cloud.foo" ,
745+ ID : "google.cloud.foo.Foo" ,
746+ Enums : []* api.Enum {},
747+ Fields : []* api.Field {},
748+ }
749+ foreignEnumState := & api.Enum {
750+ Name : "ForeignEnum" ,
751+ Package : "google.cloud.foo" ,
752+ ID : "google.cloud.foo.ForeignEnum" ,
753+ Values : []* api.EnumValue {
754+ {
755+ Name : "Enabled" ,
756+ Number : 1 ,
757+ },
758+ },
759+ }
760+ mapStringToBytes := & api.Message {
761+ Name : "$StringToBytes" ,
762+ ID : "..$StringToBytes" ,
763+ IsMap : true ,
764+ Fields : []* api.Field {
765+ {
766+ Name : "key" ,
767+ Typez : api .STRING_TYPE ,
768+ },
769+ {
770+ Name : "value" ,
771+ Typez : api .BYTES_TYPE ,
772+ },
773+ },
774+ }
718775
719776 for _ , test := range []struct {
720777 field * api.Field
@@ -780,11 +837,35 @@ func TestCreateFromJsonLine(t *testing.T) {
780837 "decodeListBytes(json['bytesList'])" ,
781838 },
782839
840+ // enums
841+ {
842+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .ENUM_TYPE , TypezID : enumState .ID },
843+ "decodeEnum(json['message'], State.fromJson) ?? State.$default" ,
844+ },
845+ {
846+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .ENUM_TYPE , TypezID : foreignEnumState .ID },
847+ "decodeEnum(json['message'], foo.ForeignEnum.fromJson) ?? foo.ForeignEnum.$default" ,
848+ },
849+
783850 // messages
784851 {
785852 & api.Field {Name : "message" , JSONName : "message" , Typez : api .MESSAGE_TYPE , TypezID : secret .ID },
786853 "decode(json['message'], Secret.fromJson)" ,
787854 },
855+ {
856+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .MESSAGE_TYPE , TypezID : foreignMessage .ID },
857+ "decode(json['message'], foo.Foo.fromJson)" ,
858+ },
859+ {
860+ // Custom encoding.
861+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .MESSAGE_TYPE , TypezID : ".google.protobuf.Duration" },
862+ "decodeCustom(json['message'], Duration.fromJson)" ,
863+ },
864+ {
865+ // Map of bytes.
866+ & api.Field {Name : "message" , JSONName : "message" , Map : true , Typez : api .MESSAGE_TYPE , TypezID : mapStringToBytes .ID },
867+ "decodeMapBytes(json['message']) ?? {}" ,
868+ },
788869 } {
789870 t .Run (test .field .Name , func (t * testing.T ) {
790871 message := & api.Message {
@@ -793,9 +874,11 @@ func TestCreateFromJsonLine(t *testing.T) {
793874 Package : sample .Package ,
794875 Fields : []* api.Field {test .field },
795876 }
796- model := api .NewTestAPI ([]* api.Message {message , secret }, []* api.Enum {}, []* api.Service {})
877+ model := api .NewTestAPI ([]* api.Message {message , secret , foreignMessage , mapStringToBytes }, []* api.Enum {enumState , foreignEnumState }, []* api.Service {})
797878 annotate := newAnnotateModel (model )
798- annotate .annotateModel (map [string ]string {})
879+ annotate .annotateModel (map [string ]string {
880+ "prefix:google.cloud.foo" : "foo" ,
881+ })
799882 codec := test .field .Codec .(* fieldAnnotation )
800883
801884 got := annotate .createFromJsonLine (test .field , model .State , codec .Required )
@@ -810,6 +893,25 @@ func TestCreateToJsonLine(t *testing.T) {
810893 secret := sample .Secret ()
811894 enum := sample .EnumState ()
812895
896+ foreignMessage := & api.Message {
897+ Name : "Foo" ,
898+ Package : "google.cloud.foo" ,
899+ ID : "google.cloud.foo.Foo" ,
900+ Enums : []* api.Enum {},
901+ Fields : []* api.Field {},
902+ }
903+ foreignEnumState := & api.Enum {
904+ Name : "ForeignEnum" ,
905+ Package : "google.cloud.foo" ,
906+ ID : "google.cloud.foo.ForeignEnum" ,
907+ Values : []* api.EnumValue {
908+ {
909+ Name : "Enabled" ,
910+ Number : 1 ,
911+ },
912+ },
913+ }
914+
813915 for _ , test := range []struct {
814916 field * api.Field
815917 want string
@@ -874,11 +976,25 @@ func TestCreateToJsonLine(t *testing.T) {
874976 "encodeListBytes(bytesList)" ,
875977 },
876978
979+ // enums
980+ {
981+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .ENUM_TYPE , TypezID : enum .ID },
982+ "message.toJson()" ,
983+ },
984+ {
985+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .ENUM_TYPE , TypezID : foreignEnumState .ID },
986+ "message.toJson()" ,
987+ },
988+
877989 // messages
878990 {
879991 & api.Field {Name : "message" , JSONName : "message" , Typez : api .MESSAGE_TYPE , TypezID : secret .ID },
880992 "message!.toJson()" ,
881993 },
994+ {
995+ & api.Field {Name : "message" , JSONName : "message" , Typez : api .MESSAGE_TYPE , TypezID : foreignMessage .ID },
996+ "message!.toJson()" ,
997+ },
882998 } {
883999 t .Run (test .field .Name , func (t * testing.T ) {
8841000 message := & api.Message {
@@ -887,9 +1003,11 @@ func TestCreateToJsonLine(t *testing.T) {
8871003 Package : sample .Package ,
8881004 Fields : []* api.Field {test .field },
8891005 }
890- model := api .NewTestAPI ([]* api.Message {message , secret }, []* api.Enum {enum }, []* api.Service {})
1006+ model := api .NewTestAPI ([]* api.Message {message , secret , foreignMessage }, []* api.Enum {enum , foreignEnumState }, []* api.Service {})
8911007 annotate := newAnnotateModel (model )
892- annotate .annotateModel (map [string ]string {})
1008+ annotate .annotateModel (map [string ]string {
1009+ "prefix:google.cloud.foo" : "foo" ,
1010+ })
8931011 codec := test .field .Codec .(* fieldAnnotation )
8941012
8951013 got := createToJsonLine (test .field , model .State , codec .Required )
0 commit comments