Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 1 | package prototext_test |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | protoV1 "github.com/golang/protobuf/proto" |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 7 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 8 | "google.golang.org/protobuf/internal/impl" |
| 9 | pimpl "google.golang.org/protobuf/internal/impl" |
| 10 | "google.golang.org/protobuf/internal/scalar" |
| 11 | "google.golang.org/protobuf/proto" |
| 12 | preg "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 13 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/encoding/testprotos/pb2" |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 15 | "google.golang.org/protobuf/types/known/anypb" |
| 16 | "google.golang.org/protobuf/types/known/durationpb" |
| 17 | "google.golang.org/protobuf/types/known/emptypb" |
| 18 | "google.golang.org/protobuf/types/known/structpb" |
| 19 | "google.golang.org/protobuf/types/known/timestamppb" |
| 20 | "google.golang.org/protobuf/types/known/wrapperspb" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func TestRoundTrip(t *testing.T) { |
| 24 | tests := []struct { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 25 | desc string |
| 26 | resolver *preg.Types |
| 27 | message proto.Message |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 28 | }{{ |
| 29 | desc: "well-known type fields set to empty messages", |
| 30 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 31 | OptBool: &wrapperspb.BoolValue{}, |
| 32 | OptInt32: &wrapperspb.Int32Value{}, |
| 33 | OptInt64: &wrapperspb.Int64Value{}, |
| 34 | OptUint32: &wrapperspb.UInt32Value{}, |
| 35 | OptUint64: &wrapperspb.UInt64Value{}, |
| 36 | OptFloat: &wrapperspb.FloatValue{}, |
| 37 | OptDouble: &wrapperspb.DoubleValue{}, |
| 38 | OptString: &wrapperspb.StringValue{}, |
| 39 | OptBytes: &wrapperspb.BytesValue{}, |
| 40 | OptDuration: &durationpb.Duration{}, |
| 41 | OptTimestamp: ×tamppb.Timestamp{}, |
| 42 | OptStruct: &structpb.Struct{}, |
| 43 | OptList: &structpb.ListValue{}, |
| 44 | OptValue: &structpb.Value{}, |
| 45 | OptEmpty: &emptypb.Empty{}, |
| 46 | OptAny: &anypb.Any{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 47 | }, |
| 48 | }, { |
| 49 | desc: "well-known type scalar fields", |
| 50 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 51 | OptBool: &wrapperspb.BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 52 | Value: true, |
| 53 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 54 | OptInt32: &wrapperspb.Int32Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 55 | Value: -42, |
| 56 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 57 | OptInt64: &wrapperspb.Int64Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 58 | Value: -42, |
| 59 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 60 | OptUint32: &wrapperspb.UInt32Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 61 | Value: 0xff, |
| 62 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 63 | OptUint64: &wrapperspb.UInt64Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 64 | Value: 0xffff, |
| 65 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 66 | OptFloat: &wrapperspb.FloatValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 67 | Value: 1.234, |
| 68 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 69 | OptDouble: &wrapperspb.DoubleValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 70 | Value: 1.23e308, |
| 71 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 72 | OptString: &wrapperspb.StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 73 | Value: "谷歌", |
| 74 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 75 | OptBytes: &wrapperspb.BytesValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 76 | Value: []byte("\xe8\xb0\xb7\xe6\xad\x8c"), |
| 77 | }, |
| 78 | }, |
| 79 | }, { |
| 80 | desc: "well-known type time-related fields", |
| 81 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 82 | OptDuration: &durationpb.Duration{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 83 | Seconds: -3600, |
| 84 | Nanos: -123, |
| 85 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 86 | OptTimestamp: ×tamppb.Timestamp{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 87 | Seconds: 1257894000, |
| 88 | Nanos: 123, |
| 89 | }, |
| 90 | }, |
| 91 | }, { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 92 | desc: "Struct field and different Value types", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 93 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 94 | OptStruct: &structpb.Struct{ |
| 95 | Fields: map[string]*structpb.Value{ |
| 96 | "bool": &structpb.Value{ |
| 97 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 98 | BoolValue: true, |
| 99 | }, |
| 100 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 101 | "double": &structpb.Value{ |
| 102 | Kind: &structpb.Value_NumberValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 103 | NumberValue: 3.1415, |
| 104 | }, |
| 105 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 106 | "null": &structpb.Value{ |
| 107 | Kind: &structpb.Value_NullValue{ |
| 108 | NullValue: structpb.NullValue_NULL_VALUE, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 109 | }, |
| 110 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 111 | "string": &structpb.Value{ |
| 112 | Kind: &structpb.Value_StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 113 | StringValue: "string", |
| 114 | }, |
| 115 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 116 | "struct": &structpb.Value{ |
| 117 | Kind: &structpb.Value_StructValue{ |
| 118 | StructValue: &structpb.Struct{ |
| 119 | Fields: map[string]*structpb.Value{ |
| 120 | "bool": &structpb.Value{ |
| 121 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 122 | BoolValue: false, |
| 123 | }, |
| 124 | }, |
| 125 | }, |
| 126 | }, |
| 127 | }, |
| 128 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 129 | "list": &structpb.Value{ |
| 130 | Kind: &structpb.Value_ListValue{ |
| 131 | ListValue: &structpb.ListValue{ |
| 132 | Values: []*structpb.Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 133 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 134 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 135 | BoolValue: false, |
| 136 | }, |
| 137 | }, |
| 138 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 139 | Kind: &structpb.Value_StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 140 | StringValue: "hello", |
| 141 | }, |
| 142 | }, |
| 143 | }, |
| 144 | }, |
| 145 | }, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | }, |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 150 | }, { |
| 151 | desc: "Any field without registered type", |
| 152 | resolver: preg.NewTypes(), |
| 153 | message: func() proto.Message { |
| 154 | m := &pb2.Nested{ |
| 155 | OptString: scalar.String("embedded inside Any"), |
| 156 | OptNested: &pb2.Nested{ |
| 157 | OptString: scalar.String("inception"), |
| 158 | }, |
| 159 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 160 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 161 | if err != nil { |
| 162 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 163 | } |
| 164 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 165 | OptAny: &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 166 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 167 | Value: b, |
| 168 | }, |
| 169 | } |
| 170 | }(), |
| 171 | }, { |
| 172 | desc: "Any field with registered type", |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 173 | resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
| 174 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 175 | m := &pb2.Nested{ |
| 176 | OptString: scalar.String("embedded inside Any"), |
| 177 | OptNested: &pb2.Nested{ |
| 178 | OptString: scalar.String("inception"), |
| 179 | }, |
| 180 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 181 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 182 | if err != nil { |
| 183 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 184 | } |
| 185 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 186 | OptAny: &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 187 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 188 | Value: b, |
| 189 | }, |
| 190 | } |
| 191 | }(), |
| 192 | }, { |
| 193 | desc: "Any field containing Any message", |
| 194 | resolver: func() *preg.Types { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 195 | mt1 := impl.Export{}.MessageTypeOf(&pb2.Nested{}) |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 196 | mt2 := impl.Export{}.MessageTypeOf(&anypb.Any{}) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 197 | return preg.NewTypes(mt1, mt2) |
| 198 | }(), |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 199 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 200 | m1 := &pb2.Nested{ |
| 201 | OptString: scalar.String("message inside Any of another Any field"), |
| 202 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 203 | b1, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 204 | if err != nil { |
| 205 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 206 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 207 | m2 := &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 208 | TypeUrl: "pb2.Nested", |
| 209 | Value: b1, |
| 210 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 211 | b2, err := proto.MarshalOptions{Deterministic: true}.Marshal(m2) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 212 | if err != nil { |
| 213 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 214 | } |
| 215 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame^] | 216 | OptAny: &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 217 | TypeUrl: "google.protobuf.Any", |
| 218 | Value: b2, |
| 219 | }, |
| 220 | } |
| 221 | }(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 222 | }} |
| 223 | |
| 224 | for _, tt := range tests { |
| 225 | tt := tt |
| 226 | t.Run(tt.desc, func(t *testing.T) { |
| 227 | t.Parallel() |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 228 | b, err := prototext.MarshalOptions{Resolver: tt.resolver}.Marshal(tt.message) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 229 | if err != nil { |
| 230 | t.Errorf("Marshal() returned error: %v\n\n", err) |
| 231 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 232 | |
| 233 | gotMessage := new(pb2.KnownTypes) |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 234 | err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(b, gotMessage) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 235 | if err != nil { |
| 236 | t.Errorf("Unmarshal() returned error: %v\n\n", err) |
| 237 | } |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 238 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 239 | if !protoV1.Equal(gotMessage, tt.message.(protoV1.Message)) { |
Herbie Ong | 7065195 | 2018-12-13 14:19:50 -0800 | [diff] [blame] | 240 | t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message) |
| 241 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 242 | }) |
| 243 | } |
| 244 | } |