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