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