-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformam_test.go
131 lines (117 loc) · 3.66 KB
/
formam_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
package cloudy
import (
"encoding/hex"
"fmt"
"net/url"
"testing"
"time"
)
type Text string
func (s *Text) UnmarshalText(text []byte) error {
var n Text
n = "the string has changed by UnmarshalText method"
*s = n
return nil
}
type UUID [16]byte
func (u *UUID) UnmarshalText(text []byte) error {
if len(text) != 32 {
return fmt.Errorf("text must be exactly 16 bytes long, got %d bytes", len(text))
}
_, err := hex.Decode(u[:], text)
if err != nil {
return err
}
return nil
}
type Anonymous struct {
Int int
AnonymousField string
}
type PtrStruct struct {
String *string
}
type TestStruct struct {
Anonymous
Int []string
Nest struct {
Children []struct {
ID string
Name string
}
}
String string
Slice []int
MapSlice map[string][]string
MapMap map[string]map[string]string
MapRecursive map[string]map[string]map[string]struct {
Recursive bool
}
Bool bool
Ptr *string
Tag string `formam:"tag"`
Time time.Time
URL url.URL
PtrStruct *PtrStruct
UnmarshalText Text
MapCustomKey map[UUID]string
MapCustomKeyPtr map[*UUID]string
InterfaceStruct interface{}
Interface interface{}
}
type InterfaceStruct struct {
ID int
Name string
}
var structValues = url.Values{
"Nest.Children[0].ID": []string{"monoculum_id"},
"Nest.Children[0].ParamKey": []string{"Monoculum"},
"MapSlice.names[0]": []string{"shinji"},
"MapSlice.names[2]": []string{"sasuka"},
"MapSlice.names[4]": []string{"carla"},
"MapSlice.countries[0]": []string{"japan"},
"MapSlice.countries[1]": []string{"spain"},
"MapSlice.countries[2]": []string{"germany"},
"MapSlice.countries[3]": []string{"united states"},
"MapMap.titles.es_es": []string{"El viaje de Chihiro"},
"MapMap.titles.en_us": []string{"The spirit away"},
"MapRecursive.map.struct.are.Recursive": []string{"true"},
"Slice[0]": []string{"1"},
"Slice[1]": []string{"2"},
"Int[0]": []string{"10"}, // Int is located inside Anonymous struct
"AnonymousField": []string{"anonymous!"},
"Bool": []string{"true"},
"tag": []string{"tagged"},
"Ptr": []string{"this is a pointer to string"},
"Time": []string{"2006-10-08"},
"URL": []string{"https://www.golang.org"},
"PtrStruct.String": []string{"dashaus"},
"UnmarshalText": []string{"unmarshal text"},
"MapCustomKey.11e5bf2d3e403a8c86740023dffe5350": []string{"Princess Mononoke"},
"MapCustomKeyPtr.11e5bf2d3e403a8c86740023dffe5350": []string{"*Princess Mononoke"},
"InterfaceStruct.ID": []string{"1"},
"InterfaceStruct.ParamKey": []string{"Go"},
"Interface": []string{"only interface"},
}
func TestDecodeInStruct(t *testing.T) {
var t1 TestStruct
t1.InterfaceStruct = &InterfaceStruct{}
err := formamDecoder(structValues, &t1)
if err != nil {
t.Error(err)
}
fmt.Println("RESULT: ", t1)
}
type TestSlice []string
var sliceValues = url.Values{
"[0]": []string{"spanish"},
"[1]": []string{"english"},
}
func TestDecodeInSlice(t *testing.T) {
var t2 TestSlice
err := formamDecoder(sliceValues, &t2)
if err != nil {
t.Error(err)
}
fmt.Println("RESULT: ", t2)
}