1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
package encoding
import (
"bytes"
"encoding/json"
"errors"
"image"
"reflect"
"testing"
)
type T struct {
X string
Y int
Z int `rethinkdb:"-"`
}
type U struct {
Alphabet string `rethinkdb:"alpha"`
}
type V struct {
F1 interface{}
F2 int32
F3 string
}
type tx struct {
x int
}
var txType = reflect.TypeOf((*tx)(nil)).Elem()
// Test data structures for anonymous fields.
type Point struct {
Z int
}
type Top struct {
Level0 int
Embed0
*Embed0a
*Embed0b `rethinkdb:"e,omitempty"` // treated as named
Embed0c `rethinkdb:"-"` // ignored
Loop
Embed0p // has Point with X, Y, used
Embed0q // has Point with Z, used
}
type Embed0 struct {
Level1a int // overridden by Embed0a's Level1a with tag
Level1b int // used because Embed0a's Level1b is renamed
Level1c int // used because Embed0a's Level1c is ignored
Level1d int // annihilated by Embed0a's Level1d
Level1e int `rethinkdb:"x"` // annihilated by Embed0a.Level1e
}
type Embed0a struct {
Level1a int `rethinkdb:"Level1a,omitempty"`
Level1b int `rethinkdb:"LEVEL1B,omitempty"`
Level1c int `rethinkdb:"-"`
Level1d int // annihilated by Embed0's Level1d
Level1f int `rethinkdb:"x"` // annihilated by Embed0's Level1e
}
type Embed0b Embed0
type Embed0c Embed0
type Embed0p struct {
image.Point
}
type Embed0q struct {
Point
}
type Loop struct {
Loop1 int `rethinkdb:",omitempty"`
Loop2 int `rethinkdb:",omitempty"`
*Loop
}
// From reflect test:
// The X in S6 and S7 annihilate, but they also block the X in S8.S9.
type S5 struct {
S6
S7
S8
}
type S6 struct {
X int
}
type S7 S6
type S8 struct {
S9
}
type S9 struct {
X int
Y int
}
// From reflect test:
// The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
type S10 struct {
S11
S12
S13
}
type S11 struct {
S6
}
type S12 struct {
S6
}
type S13 struct {
S8
}
type PointerBasic struct {
X int
Y *int
}
type Pointer struct {
PPoint *Point
Point Point
}
type decodeTest struct {
in interface{}
ptr interface{}
out interface{}
err error
}
type Ambig struct {
// Given "hello", the first match should win.
First int `rethinkdb:"HELLO"`
Second int `rethinkdb:"Hello"`
}
type SliceStruct struct {
X []string
}
// Decode test helper vars
var (
sampleInt = 2
)
var decodeTests = []decodeTest{
// basic types
{in: true, ptr: new(bool), out: true},
{in: 1, ptr: new(int), out: 1},
{in: 1.2, ptr: new(float64), out: 1.2},
{in: -5, ptr: new(int16), out: int16(-5)},
{in: 2, ptr: new(string), out: string("2")},
{in: float64(2.0), ptr: new(interface{}), out: float64(2.0)},
{in: string("2"), ptr: new(interface{}), out: string("2")},
{in: "a\u1234", ptr: new(string), out: "a\u1234"},
{in: []interface{}{}, ptr: new([]string), out: []string{}},
{in: map[string]interface{}{"X": []interface{}{1, 2, 3}, "Y": 4}, ptr: new(T), out: T{}, err: &DecodeTypeError{reflect.TypeOf(""), reflect.TypeOf([]interface{}{}), ""}},
{in: map[string]interface{}{"x": 1}, ptr: new(tx), out: tx{}},
{in: map[string]interface{}{"F1": float64(1), "F2": 2, "F3": 3}, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: string("3")}},
{in: map[string]interface{}{"F1": string("1"), "F2": 2, "F3": 3}, ptr: new(V), out: V{F1: string("1"), F2: int32(2), F3: string("3")}},
{
in: map[string]interface{}{"k1": int64(1), "k2": "s", "k3": []interface{}{int64(1), 2.0, 3e-3}, "k4": map[string]interface{}{"kk1": "s", "kk2": int64(2)}},
out: map[string]interface{}{"k1": int64(1), "k2": "s", "k3": []interface{}{int64(1), 2.0, 3e-3}, "k4": map[string]interface{}{"kk1": "s", "kk2": int64(2)}},
ptr: new(interface{}),
},
// Z has a "-" tag.
{in: map[string]interface{}{"Y": 1, "Z": 2}, ptr: new(T), out: T{Y: 1}},
{in: map[string]interface{}{"alpha": "abc", "alphabet": "xyz"}, ptr: new(U), out: U{Alphabet: "abc"}},
{in: map[string]interface{}{"alpha": "abc"}, ptr: new(U), out: U{Alphabet: "abc"}},
{in: map[string]interface{}{"alphabet": "xyz"}, ptr: new(U), out: U{}},
// array tests
{in: []interface{}{1, 2, 3}, ptr: new([3]int), out: [3]int{1, 2, 3}},
{in: []interface{}{1, 2, 3}, ptr: new([1]int), out: [1]int{1}},
{in: []interface{}{1, 2, 3}, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}},
// empty array to interface test
{in: map[string]interface{}{"T": []interface{}{}}, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}},
{
in: map[string]interface{}{
"Level0": 1,
"Level1b": 2,
"Level1c": 3,
"level1d": 4,
"Level1a": 5,
"LEVEL1B": 6,
"e": map[string]interface{}{
"Level1a": 8,
"Level1b": 9,
"Level1c": 10,
"Level1d": 11,
"x": 12,
},
"Loop1": 13,
"Loop2": 14,
"X": 15,
"Y": 16,
"Z": 17,
},
ptr: new(Top),
out: Top{
Level0: 1,
Embed0: Embed0{
Level1b: 2,
Level1c: 3,
},
Embed0a: &Embed0a{
Level1a: 5,
Level1b: 6,
},
Embed0b: &Embed0b{
Level1a: 8,
Level1b: 9,
Level1c: 10,
Level1d: 11,
},
Loop: Loop{
Loop1: 13,
Loop2: 14,
},
Embed0p: Embed0p{
Point: image.Point{X: 15, Y: 16},
},
Embed0q: Embed0q{
Point: Point{Z: 17},
},
},
},
{
in: map[string]interface{}{"hello": 1},
ptr: new(Ambig),
out: Ambig{First: 1},
},
{
in: map[string]interface{}{"X": 1, "Y": 2},
ptr: new(S5),
out: S5{S8: S8{S9: S9{Y: 2}}},
},
{
in: map[string]interface{}{"X": 1, "Y": 2},
ptr: new(S10),
out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}},
},
{
in: map[string]interface{}{"PPoint": map[string]interface{}{"Z": 1}, "Point": map[string]interface{}{"Z": 2}},
ptr: new(Pointer),
out: Pointer{PPoint: &Point{Z: 1}, Point: Point{Z: 2}},
},
{
in: map[string]interface{}{"Point": map[string]interface{}{"Z": 2}},
ptr: new(Pointer),
out: Pointer{PPoint: nil, Point: Point{Z: 2}},
},
{
in: map[string]interface{}{"x": 2},
ptr: new(PointerBasic),
out: PointerBasic{X: 2, Y: nil},
},
{
in: map[string]interface{}{"x": 2, "y": 2},
ptr: new(PointerBasic),
out: PointerBasic{X: 2, Y: &sampleInt},
},
}
func TestDecode(t *testing.T) {
for i, tt := range decodeTests {
if tt.ptr == nil {
continue
}
// v = new(right-type)
v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
err := Decode(v.Interface(), tt.in)
if !jsonEqual(err, tt.err) {
t.Errorf("#%d: got error %v want %v", i, err, tt.err)
continue
}
if tt.err == nil && !jsonEqual(v.Elem().Interface(), tt.out) {
t.Errorf("#%d: mismatch\nhave: %+v\nwant: %+v", i, v.Elem().Interface(), tt.out)
continue
}
// Check round trip.
if tt.err == nil {
enc, err := Encode(v.Interface())
if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err)
continue
}
vv := reflect.New(reflect.TypeOf(tt.ptr).Elem())
if err := Decode(vv.Interface(), enc); err != nil {
t.Errorf("#%d: error re-decodeing: %v", i, err)
continue
}
if !jsonEqual(v.Elem().Interface(), vv.Elem().Interface()) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface())
continue
}
}
}
}
func TestStringKind(t *testing.T) {
type aMap map[string]int
var m1, m2 map[string]int
m1 = map[string]int{
"foo": 42,
}
data, err := Encode(m1)
if err != nil {
t.Errorf("Unexpected error encoding: %v", err)
}
err = Decode(&m2, data)
if err != nil {
t.Errorf("Unexpected error decoding: %v", err)
}
if !jsonEqual(m1, m2) {
t.Error("Items should be equal after encoding and then decoding")
}
}
// Test handling of unexported fields that should be ignored.
type unexportedFields struct {
Name string
m map[string]interface{} `rethinkdb:"-"`
m2 map[string]interface{} `rethinkdb:"abcd"`
}
func TestDecodeUnexported(t *testing.T) {
input := map[string]interface{}{
"Name": "Bob",
"m": map[string]interface{}{
"x": 123,
},
"m2": map[string]interface{}{
"y": 123,
},
"abcd": map[string]interface{}{
"z": 789,
},
}
want := &unexportedFields{Name: "Bob"}
out := &unexportedFields{}
err := Decode(out, input)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
type Foo struct {
FooBar interface{} `rethinkdb:"foobar"`
}
type Bar struct {
Baz int `rethinkdb:"baz"`
}
type UnmarshalerPointer struct {
Value *UnmarshalerValue
}
type UnmarshalerValue struct {
ValueInt int64
ValueString string
}
func (v *UnmarshalerValue) MarshalRQL() (interface{}, error) {
if v.ValueInt != int64(0) {
return Encode(v.ValueInt)
}
if v.ValueString != "" {
return Encode(v.ValueString)
}
return Encode(nil)
}
func (v *UnmarshalerValue) UnmarshalRQL(b interface{}) (err error) {
n, s := int64(0), ""
if err = Decode(&s, b); err == nil {
v.ValueString = s
return
}
if err = Decode(&n, b); err == nil {
v.ValueInt = n
}
return
}
func TestDecodeUnmarshalerPointer(t *testing.T) {
input := map[string]interface{}{
"Value": "abc",
}
want := &UnmarshalerPointer{
Value: &UnmarshalerValue{ValueString: "abc"},
}
out := &UnmarshalerPointer{}
err := Decode(out, input)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %+v, want %+v", out, want)
}
}
func TestDecodeMapIntKeys(t *testing.T) {
input := map[string]int{"1": 1, "2": 2, "3": 3}
want := map[int]int{1: 1, 2: 2, 3: 3}
out := map[int]int{}
err := Decode(&out, input)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func TestDecodeCompoundKey(t *testing.T) {
input := map[string]interface{}{"id": []string{"1", "2"}, "err_a[]": "3", "err_b[": "4", "err_c]": "5"}
want := Compound{"1", "2", "3", "4", "5"}
out := Compound{}
err := Decode(&out, input)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func TestDecodeNilSlice(t *testing.T) {
input := map[string]interface{}{"X": nil}
want := SliceStruct{}
out := SliceStruct{}
err := Decode(&out, input)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func jsonEqual(a, b interface{}) bool {
// First check using reflect.DeepEqual
if reflect.DeepEqual(a, b) {
return true
}
// Then use jsonEqual
ba, err := json.Marshal(a)
if err != nil {
panic(err)
}
bb, err := json.Marshal(b)
if err != nil {
panic(err)
}
return bytes.Compare(ba, bb) == 0
}
func TestMergeStruct(t *testing.T) {
var dst struct {
Field string
AnotherField string
}
dst.Field = "change me"
dst.AnotherField = "don't blank me"
err := Merge(&dst, map[string]interface{}{"Field": "Changed!"})
if err != nil {
t.Error("Cannot merge:", err)
}
if dst.AnotherField == "" {
t.Error("Field has been wiped")
}
}
func TestMergeMap(t *testing.T) {
var dst = make(map[string]string)
dst["field"] = "change me"
dst["another_field"] = "don't blank me"
err := Merge(&dst, map[string]interface{}{"field": "Changed!"})
if err != nil {
t.Error("Cannot merge:", err)
}
if dst["another_field"] == "" {
t.Error("Field has been wiped")
}
}
func TestDecodeCustomTypeEncodingValue(t *testing.T) {
type innerType struct {
Val int
}
type outerType struct {
Inner innerType `rethinkdb:"inner"`
}
want := outerType{Inner: innerType{Val: 5}}
in := map[string]interface{}{
"inner": map[string]interface{}{
"someval": 5,
},
}
SetTypeEncoding(reflect.TypeOf(innerType{}),
nil, func(enc interface{}, val reflect.Value) error {
m := enc.(map[string]interface{})
val.Set(reflect.ValueOf(innerType{Val: m["someval"].(int)}))
return nil
})
var out outerType
err := Decode(&out, in)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func TestDecodeCustomTypeEncodingPointer(t *testing.T) {
type innerType struct {
Val int
}
type outerType struct {
Inner *innerType `rethinkdb:"inner"`
}
want := outerType{Inner: &innerType{Val: 5}}
in := map[string]interface{}{
"inner": map[string]interface{}{
"someval": 5,
},
}
SetTypeEncoding(reflect.TypeOf((*innerType)(nil)),
nil, func(enc interface{}, val reflect.Value) error {
m := enc.(map[string]interface{})
val.Set(reflect.ValueOf(&innerType{Val: m["someval"].(int)}))
return nil
})
var out outerType
err := Decode(&out, in)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %+v, want %+v", out, want)
}
}
func TestDecodeCustomRootTypeEncodingValue(t *testing.T) {
type cType struct {
Val int
}
want := cType{Val: 5}
in := map[string]interface{}{
"someval": 5,
}
SetTypeEncoding(reflect.TypeOf(cType{}),
nil, func(enc interface{}, val reflect.Value) error {
m := enc.(map[string]interface{})
val.Set(reflect.ValueOf(cType{Val: m["someval"].(int)}))
return nil
})
var out cType
err := Decode(&out, in)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func TestDecodeCustomRootTypeEncodingPointer(t *testing.T) {
type cType struct {
Val int
}
want := cType{Val: 5}
in := map[string]interface{}{
"someval": 5,
}
SetTypeEncoding(reflect.TypeOf((*cType)(nil)),
nil, func(enc interface{}, val reflect.Value) error {
m := enc.(map[string]interface{})
val.Set(reflect.ValueOf(&cType{Val: m["someval"].(int)}))
return nil
})
var out *cType
err := Decode(&out, in)
if err != nil {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
}
}
func TestDecodeCustomTypeEncodingError(t *testing.T) {
type cType struct {
Val int
}
in := map[string]interface{}{
"val": 5,
}
cerr := errors.New("decode error")
SetTypeEncoding(reflect.TypeOf(cType{}),
nil, func(enc interface{}, val reflect.Value) error {
return cerr
})
var out cType
err := Decode(&out, in)
if err == nil {
t.Errorf("got no error, expected %v", cerr)
}
if err != cerr {
t.Errorf("got %v, want %v", err, cerr)
}
}
|