forked from BurntSushi/toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_marshaler_test.go
218 lines (187 loc) · 4.86 KB
/
custom_marshaler_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
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
package toml_test
import (
"bytes"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"strconv"
"strings"
"testing"
)
// Test for hotfix-341
func TestCustomEncode(t *testing.T) {
var enum = Enum(OtherValue)
outer := Outer{
String: &InnerString{value: "value"},
Int: &InnerInt{value: 10},
Bool: &InnerBool{value: true},
Enum: &enum,
ArrayS: &InnerArrayString{value: []string{"text1", "text2"}},
ArrayI: &InnerArrayInt{value: []int64{5, 7, 3}},
}
var buf bytes.Buffer
err := toml.NewEncoder(&buf).Encode(outer)
if err != nil {
t.Errorf("Encode failed: %s", err)
}
have := strings.TrimSpace(buf.String())
want := strings.TrimSpace("String = \"value\"\nInt = 10\nBool = true\nEnum = \"OTHER_VALUE\"\nArrayS = [\"text1\", \"text2\"]\nArrayI = [5, 7, 3]\n")
if want != have {
t.Errorf("\nhave:\n%s\nwant:\n%s\n", have, want)
}
}
// Test for hotfix-341
func TestCustomDecode(t *testing.T) {
const testToml = "Bool = true\nString = \"test\"\nInt = 10\nEnum = \"OTHER_VALUE\"\nArrayS = [\"text1\", \"text2\"]\nArrayI = [5, 7, 3]"
outer := Outer{}
_, err := toml.Decode(testToml, &outer)
if err != nil {
t.Fatal(fmt.Sprintf("Decode failed: %s", err))
}
if outer.String.value != "test" {
t.Errorf("\nhave:\n%s\nwant:\n%s\n", outer.String.value, "test")
}
if outer.Bool.value != true {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.Bool.value, true)
}
if outer.Int.value != 10 {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.Int.value, 10)
}
if *outer.Enum != OtherValue {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.Enum, OtherValue)
}
if fmt.Sprint(outer.ArrayS.value) != fmt.Sprint([]string{"text1", "text2"}) {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.ArrayS.value, []string{"text1", "text2"})
}
if fmt.Sprint(outer.ArrayI.value) != fmt.Sprint([]int64{5, 7, 3}) {
t.Errorf("\nhave:\n%v\nwant:\n%v\n", outer.ArrayI.value, []int64{5, 7, 3})
}
}
/* Implementing MarshalTOML and UnmarshalTOML structs
An useful use could be to map a TOML value to an internal value, like emuns.
*/
type Enum int
const (
NoValue Enum = iota
SomeValue
OtherValue
)
func (e *Enum) Value() string {
switch *e {
case SomeValue:
return "SOME_VALUE"
case OtherValue:
return "OTHER_VALUE"
case NoValue:
return ""
}
return ""
}
func (e *Enum) MarshalTOML() ([]byte, error) {
return []byte("\"" + e.Value() + "\""), nil
}
func (e *Enum) UnmarshalTOML(value interface{}) error {
sValue, ok := value.(string)
if !ok {
return fmt.Errorf("value %v is not a string type", value)
}
for _, enum := range []Enum{NoValue, SomeValue, OtherValue} {
if enum.Value() == sValue {
*e = enum
return nil
}
}
return errors.New("invalid enum value")
}
type InnerString struct {
value string
}
func (s *InnerString) MarshalTOML() ([]byte, error) {
return []byte("\"" + s.value + "\""), nil
}
func (s *InnerString) UnmarshalTOML(value interface{}) error {
sValue, ok := value.(string)
if !ok {
return fmt.Errorf("value %v is not a string type", value)
}
s.value = sValue
return nil
}
type InnerInt struct {
value int
}
func (i *InnerInt) MarshalTOML() ([]byte, error) {
return []byte(strconv.Itoa(i.value)), nil
}
func (i *InnerInt) UnmarshalTOML(value interface{}) error {
iValue, ok := value.(int64)
if !ok {
return fmt.Errorf("value %v is not a int type", value)
}
i.value = int(iValue)
return nil
}
type InnerBool struct {
value bool
}
func (b *InnerBool) MarshalTOML() ([]byte, error) {
return []byte(strconv.FormatBool(b.value)), nil
}
func (b *InnerBool) UnmarshalTOML(value interface{}) error {
bValue, ok := value.(bool)
if !ok {
return fmt.Errorf("value %v is not a bool type", value)
}
b.value = bValue
return nil
}
type InnerArrayString struct {
value []string
}
func (as *InnerArrayString) MarshalTOML() ([]byte, error) {
return []byte("[\"" + strings.Join(as.value, "\", \"") + "\"]"), nil
}
func (as *InnerArrayString) UnmarshalTOML(value interface{}) error {
if value != nil {
asValue, ok := value.([]interface{})
if !ok {
return fmt.Errorf("value %v is not a [] type", value)
}
as.value = []string{}
for _, value := range asValue {
as.value = append(as.value, value.(string))
}
}
return nil
}
type InnerArrayInt struct {
value []int64
}
func (ai *InnerArrayInt) MarshalTOML() ([]byte, error) {
strArr := []string{}
for _, intV := range ai.value {
strArr = append(strArr, strconv.FormatInt(intV, 10))
}
return []byte("[" + strings.Join(strArr, ", ") + "]"), nil
}
func (ai *InnerArrayInt) UnmarshalTOML(value interface{}) error {
if value != nil {
asValue, ok := value.([]interface{})
if !ok {
return fmt.Errorf("value %v is not a [] type", value)
}
ai.value = []int64{}
for _, value := range asValue {
ai.value = append(ai.value, value.(int64))
}
}
return nil
}
type Outer struct {
String *InnerString
Int *InnerInt
Bool *InnerBool
Enum *Enum
ArrayS *InnerArrayString
ArrayI *InnerArrayInt
}