-
Notifications
You must be signed in to change notification settings - Fork 13
/
watson_test.go
67 lines (56 loc) · 1.25 KB
/
watson_test.go
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
package watson_test
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/genkami/watson"
"github.com/genkami/watson/pkg/types"
)
type User struct {
FullName string `watson:"fullName"`
Age int `watson:"age"`
}
type Department struct {
Name *DepartmentName
Manager *User
}
type DepartmentName struct {
Value string
}
func (d *DepartmentName) MarshalWatson() (*types.Value, error) {
return types.NewStringValue([]byte(d.Value)), nil
}
var _ types.Marshaler = &DepartmentName{}
func (d *DepartmentName) UnmarshalWatson(v *types.Value) error {
if v.Kind != types.String {
return fmt.Errorf("expected string but got %#v", v.Kind)
}
d.Value = string(v.String)
return nil
}
func TestEncodeAndDecode(t *testing.T) {
want := Department{
Name: &DepartmentName{
Value: "marketing",
},
Manager: &User{
FullName: "Tanaka Taro",
Age: 41,
},
}
var got Department
err := encodeThenDecode(&want, &got)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(&want, &got); diff != "" {
t.Fatalf("expected %#v but got %#v", &want, &got)
}
}
func encodeThenDecode(in interface{}, out interface{}) error {
encoded, err := watson.Marshal(in)
if err != nil {
return err
}
return watson.Unmarshal(encoded, out)
}