1
9
10 package hctypes
11
12 import (
13 "fmt"
14 "net/url"
15 "strconv"
16 )
17
18 func stringify_parameters(name string, parameters interface{}) Dict {
19 ret := NewDict()
20 for key, value := range flatten_parameters(parameters) {
21 ret[fmt.Sprintf("%s%s", name, key)] = value
22 }
23 return ret
24 }
25
26 func flatten_parameters(parameters Item) Dict {
27 ret := NewDict()
28 flatten(parameters, "", ret)
29 return ret
30 }
31
32 func flatten(item Item, prefix string, ret Dict) {
33 switch val := item.(type) {
34 case Dict:
35 for key, value := range val {
36 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
37 }
38 case map[string]Item:
39 for key, value := range val {
40 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
41 }
42 case map[string]interface{}:
43 for key, value := range val {
44 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
45 }
46 case List:
47 parameters_list := NewDict()
48 for i, v := range val {
49 parameters_list[strconv.Itoa(i)] = v
50 }
51 for key, value := range parameters_list {
52 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
53 }
54 case []Item:
55 parameters_list := NewDict()
56 for i, v := range val {
57 parameters_list[strconv.Itoa(i)] = v
58 }
59 for key, value := range parameters_list {
60 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
61 }
62 case []interface{}:
63 parameters_list := NewDict()
64 for i, v := range val {
65 parameters_list[strconv.Itoa(i)] = v
66 }
67 for key, value := range parameters_list {
68 flatten(value, fmt.Sprintf("%s[%s]", prefix, key), ret)
69 }
70 default:
71 ret[prefix] = val
72 }
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 func (this Dict) ToQueryString() url.Values {
90 values := make(url.Values)
91 copy := this.Copy()
92 for key, value := range copy {
93 stringified := stringify_parameters(key, value)
94 for key1, value1 := range stringified {
95 r := fmt.Sprintf("%v", value1)
96 if r == "<nil>" {
97 r = ""
98 }
99 values.Add(key1, r)
100 }
101 }
102 return values
103 }
104
View as plain text