forked from viant/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
123 lines (106 loc) · 3.15 KB
/
decoder_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
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
package toolbox_test
import (
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
"os"
"path"
"strings"
"testing"
)
func TestDecoderFactory(t *testing.T) {
{
reader := strings.NewReader("[1, 2, 3]")
decoder := toolbox.NewJSONDecoderFactory().Create(reader)
aSlice := make([]int, 0)
err := decoder.Decode(&aSlice)
assert.Nil(t, err)
assert.Equal(t, 3, len(aSlice))
}
{
reader := strings.NewReader("[1, 2, 3]")
decoder := toolbox.NewJSONDecoderFactoryWithOption(true).Create(reader)
aSlice := make([]int, 0)
err := decoder.Decode(&aSlice)
assert.Nil(t, err)
assert.Equal(t, 3, len(aSlice))
}
}
func TestUnMarshalerDecoderFactory(t *testing.T) {
reader := strings.NewReader("abc")
decoder := toolbox.NewUnMarshalerDecoderFactory().Create(reader)
foo := &Foo100{}
err := decoder.Decode(foo)
assert.Nil(t, err)
assert.Equal(t, "abc", foo.Attr)
err = decoder.Decode(&Foo101{})
assert.NotNil(t, err)
}
type Foo100 struct {
Attr string
}
func (m *Foo100) Unmarshal(data []byte) error {
m.Attr = string(data)
return nil
}
type Foo101 struct {
Attr string
}
func TestDelimiterDecoderFactory(t *testing.T) {
record := &toolbox.DelimitedRecord{
Delimiter: ",",
}
{
decoder := toolbox.NewDelimiterDecoderFactory().Create(strings.NewReader("column1,\"column2\", column3,column4"))
err := decoder.Decode(record)
if assert.Nil(t, err) {
assert.Equal(t, []string{"column1", "column2", "column3", "column4"}, record.Columns)
}
}
{
decoder := toolbox.NewDelimiterDecoderFactory().Create(strings.NewReader("1,2,\"ab,cd\",3"))
err := decoder.Decode(record)
if assert.Nil(t, err) {
assert.EqualValues(t, "1", record.Record["column1"])
assert.EqualValues(t, "2", record.Record["column2"])
assert.EqualValues(t, "ab,cd", record.Record["column3"])
assert.EqualValues(t, "3", record.Record["column4"])
}
}
{
decoder := toolbox.NewDelimiterDecoderFactory().Create(strings.NewReader("1,2,\" \"\"location:[\\\"\"BE\\\"\"]\"\" \",3"))
err := decoder.Decode(record)
if assert.Nil(t, err) {
assert.EqualValues(t, "1", record.Record["column1"])
assert.EqualValues(t, "2", record.Record["column2"])
assert.EqualValues(t, " \"location:[\\\"BE\\\"]\" ", record.Record["column3"])
assert.EqualValues(t, "3", record.Record["column4"])
}
}
}
func TestTestYamlDecoder(t *testing.T) {
var filename = path.Join(os.Getenv("TMPDIR"), "test.yaml")
toolbox.RemoveFileIfExist(filename)
defer toolbox.RemoveFileIfExist(filename)
var aMap = map[string]interface{}{
"a": 1,
"b": "123",
"c": []int{1, 3, 6},
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
if assert.Nil(t, err) {
err = toolbox.NewYamlEncoderFactory().Create(file).Encode(aMap)
assert.Nil(t, err)
}
var cloneMap = make(map[string]interface{})
file.Close()
file, err = os.OpenFile(filename, os.O_RDONLY, 0644)
if assert.Nil(t, err) {
defer file.Close()
err = toolbox.NewYamlDecoderFactory().Create(file).Decode(&cloneMap)
if assert.Nil(t, err) {
assert.EqualValues(t, aMap["a"], cloneMap["a"])
assert.EqualValues(t, aMap["b"], cloneMap["b"])
assert.EqualValues(t, toolbox.AsSlice(aMap["c"]), cloneMap["c"])
}
}
}