-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions_test.go
196 lines (161 loc) · 5.88 KB
/
conversions_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
package conversions_test
import (
"fmt"
"reflect"
"strconv"
"github.com/levelfourab/conversions-go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func testTypeExtractor(v any) (reflect.Type, error) {
return reflect.TypeOf(v), nil
}
var strToInt conversions.Converter[any] = func(v any) (any, error) {
i, err := strconv.ParseInt(v.(string), 0, 0)
if err == nil {
return int(i), nil
}
return 0, fmt.Errorf("unable to convert string to int")
}
var intToStr conversions.Converter[any] = func(v any) (any, error) {
i := v.(int)
return strconv.FormatInt(int64(i), 10), nil
}
var intToUint conversions.Converter[any] = func(v any) (any, error) {
i := v.(int)
if i >= 0 {
return uint(i), nil
}
return 0, fmt.Errorf("unable to convert negative int to uint")
}
var uintToFloat32 conversions.Converter[any] = func(v any) (any, error) {
i := v.(uint)
return float32(i), nil
}
type MediaType string
type StringWithMediaType struct {
MediaType MediaType
Value string
}
var _ = Describe("Conversions", func() {
It("will fail if no type extractor is provided", func() {
_, err := conversions.New[string, any]()
Expect(err).To(HaveOccurred())
})
It("convert will fail if the type extractor returns an error", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(func(v any) (reflect.Type, error) {
return nil, fmt.Errorf("test error")
}),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(int(0)), strToInt)
_, err = conversions.Convert("123", reflect.TypeOf(int(0)))
Expect(err).To(HaveOccurred())
})
It("convert will fail if no conversion is found", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
_, err = conversions.Convert("123", reflect.TypeOf(int(0)))
Expect(err).To(HaveOccurred())
})
It("can convert a same type to same type", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
converted, err := conversions.Convert("test", reflect.TypeOf(""))
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal("test"))
})
It("can convert directly", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(0), strToInt)
converted, err := conversions.Convert("123", reflect.TypeOf(0))
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal(123))
})
It("can convert indirectly", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(0), strToInt)
conversions.AddConversion(reflect.TypeOf(0), reflect.TypeOf(uint(0)), intToUint)
converted, err := conversions.Convert("123", reflect.TypeOf(uint(0)))
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal(uint(123)))
})
It("can convert indirectly with 1 intermediate step", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(0), strToInt)
conversions.AddConversion(reflect.TypeOf(0), reflect.TypeOf(uint(0)), intToUint)
conversions.AddConversion(reflect.TypeOf(uint(0)), reflect.TypeOf(float32(0)), uintToFloat32)
converted, err := conversions.Convert("123", reflect.TypeOf(float32(0)))
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal(float32(123)))
})
It("can convert indirectly with 2 intermediate steps", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(0), strToInt)
conversions.AddConversion(reflect.TypeOf(0), reflect.TypeOf(uint(0)), intToUint)
conversions.AddConversion(reflect.TypeOf(uint(0)), reflect.TypeOf(float32(0)), uintToFloat32)
conversions.AddConversion(reflect.TypeOf(float32(0)), reflect.TypeOf(float64(0)), func(v any) (any, error) {
return float64(v.(float32)), nil
})
converted, err := conversions.Convert("123", reflect.TypeOf(float64(0)))
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal(float64(123)))
})
It("will handle loops in the conversion graph", func() {
// This a special case where looking up a conversion requires traversing
// a loop.
//
// A loop can be created by adding a conversion from A to B, and then
// from B to A. In this test we force this by only adding a symmetrical
// conversion.
conversions, err := conversions.New(
conversions.WithTypeExtractor(testTypeExtractor),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion(reflect.TypeOf(""), reflect.TypeOf(0), strToInt)
conversions.AddConversion(reflect.TypeOf(0), reflect.TypeOf(""), intToStr)
// This should not get stuck in an infinite loop, but will error
_, err = conversions.Convert("123", reflect.TypeOf(uint(0)))
Expect(err).To(HaveOccurred())
})
It("will handle custom type", func() {
conversions, err := conversions.New(
conversions.WithTypeExtractor(func(v StringWithMediaType) (MediaType, error) {
return v.MediaType, nil
}),
)
Expect(err).ToNot(HaveOccurred())
conversions.AddConversion("text/html", "text/plain", func(v StringWithMediaType) (StringWithMediaType, error) {
return StringWithMediaType{
MediaType: "text/plain",
Value: v.Value,
}, nil
})
converted, err := conversions.Convert(StringWithMediaType{
MediaType: "text/html",
Value: "test",
}, "text/plain")
Expect(err).ToNot(HaveOccurred())
Expect(converted).To(Equal(StringWithMediaType{
MediaType: "text/plain",
Value: "test",
}))
})
})