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/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", |
Damien Neil | c1507ac | 2019-10-08 13:24:16 -0700 | [diff] [blame] | 152 | resolver: new(preg.Types), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 153 | message: func() proto.Message { |
| 154 | m := &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 155 | OptString: proto.String("embedded inside Any"), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 156 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 157 | OptString: proto.String("inception"), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 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 | }, { |
Damien Neil | c1507ac | 2019-10-08 13:24:16 -0700 | [diff] [blame] | 172 | desc: "Any field with registered type", |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 173 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 174 | m := &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 175 | OptString: proto.String("embedded inside Any"), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 176 | OptNested: &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 177 | OptString: proto.String("inception"), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 178 | }, |
| 179 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 180 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 181 | if err != nil { |
| 182 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 183 | } |
| 184 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 185 | OptAny: &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 186 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 187 | Value: b, |
| 188 | }, |
| 189 | } |
| 190 | }(), |
| 191 | }, { |
| 192 | desc: "Any field containing Any message", |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 193 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 194 | m1 := &pb2.Nested{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 195 | OptString: proto.String("message inside Any of another Any field"), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 196 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 197 | b1, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 198 | if err != nil { |
| 199 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 200 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 201 | m2 := &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 202 | TypeUrl: "pb2.Nested", |
| 203 | Value: b1, |
| 204 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 205 | b2, err := proto.MarshalOptions{Deterministic: true}.Marshal(m2) |
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 | } |
| 209 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 210 | OptAny: &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 211 | TypeUrl: "google.protobuf.Any", |
| 212 | Value: b2, |
| 213 | }, |
| 214 | } |
| 215 | }(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 216 | }} |
| 217 | |
| 218 | for _, tt := range tests { |
| 219 | tt := tt |
| 220 | t.Run(tt.desc, func(t *testing.T) { |
| 221 | t.Parallel() |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 222 | b, err := prototext.MarshalOptions{Resolver: tt.resolver}.Marshal(tt.message) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 223 | if err != nil { |
| 224 | t.Errorf("Marshal() returned error: %v\n\n", err) |
| 225 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 226 | |
| 227 | gotMessage := new(pb2.KnownTypes) |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 228 | err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(b, gotMessage) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 229 | if err != nil { |
| 230 | t.Errorf("Unmarshal() returned error: %v\n\n", err) |
| 231 | } |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 232 | |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 233 | if !proto.Equal(gotMessage, tt.message) { |
Herbie Ong | 7065195 | 2018-12-13 14:19:50 -0800 | [diff] [blame] | 234 | t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message) |
| 235 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 236 | }) |
| 237 | } |
| 238 | } |