@@ -146,19 +146,73 @@ func TestSchema(t *testing.T) {
146146 c .SetKeyTypeSchema ("int" , 15 )
147147 c .SetKeyTypeSchema ("obj.string" , "" )
148148 c .SetKeyTypeSchema ("obj.int" , 15 )
149+ c .SetKeyTypeSchema ("uint" , uint (15 ))
150+ c .SetKeyTypeSchema ("obj.uint" , uint (15 ))
151+ c .SetKeyTypeSchema ("array" , []string {})
152+ c .SetKeyTypeSchema ("obj.array" , []string {})
153+
154+ // Set array of string
155+ require .NoError (t , c .Set ("array" , []string {"abc" , "def" }))
156+ require .NoError (t , c .Set ("obj.array" , []string {"abc" , "def" }))
157+ require .Equal (t , []string {"abc" , "def" }, c .Get ("array" ))
158+ require .Equal (t , []string {"abc" , "def" }, c .Get ("obj.array" ))
159+ // Set array of string with array of any
160+ require .NoError (t , c .Set ("array" , []any {"abc" , "def" }))
161+ require .NoError (t , c .Set ("obj.array" , []any {"abc" , "def" }))
162+ require .Equal (t , []string {"abc" , "def" }, c .Get ("array" ))
163+ require .Equal (t , []string {"abc" , "def" }, c .Get ("obj.array" ))
164+ // Set array of string with array of int
165+ require .EqualError (t , c .Set ("array" , []any {"abc" , 123 }), "invalid type for key 'array': invalid conversion, got int but want string" )
166+ require .EqualError (t , c .Set ("obj.array" , []any {"abc" , 123 }), "invalid type for key 'obj.array': invalid conversion, got int but want string" )
167+
168+ // Set string
149169 require .NoError (t , c .Set ("string" , "abc" ))
150- require .Error (t , c .Set ("string" , 123 ))
151- require .NoError (t , c .Set ("int" , 123 ))
152- require .Error (t , c .Set ("int" , "abc" ))
170+ require .NoError (t , c .Set ("obj.string" , "abc" ))
153171 require .Equal (t , "abc" , c .Get ("string" ))
172+ require .Equal (t , "abc" , c .Get ("obj.string" ))
173+ // Set string with int
174+ require .EqualError (t , c .Set ("string" , 123 ), "invalid type for key 'string': invalid conversion, got int but want string" )
175+ require .EqualError (t , c .Set ("obj.string" , 123 ), "invalid type for key 'obj.string': invalid conversion, got int but want string" )
154176
155- json1 := [] byte ( `{"string":"abc"," int":123,"obj":{"string":"abc","int":123}}` )
156- require .NoError (t , json . Unmarshal ( json1 , & c ))
157- require .Equal (t , "abc" , c . Get ( "string" ))
177+ // Set int
178+ require .NoError (t , c . Set ( "int" , 123 ))
179+ require .NoError (t , c . Set ( "obj.int" , 123 ))
158180 require .Equal (t , 123 , c .Get ("int" ))
181+ require .Equal (t , 123 , c .Get ("obj.int" ))
182+ // Set int with string
183+ require .EqualError (t , c .Set ("int" , "abc" ), "invalid type for key 'int': invalid conversion, got string but want int" )
184+ require .EqualError (t , c .Set ("obj.int" , "abc" ), "invalid type for key 'obj.int': invalid conversion, got string but want int" )
185+
186+ // Set uint
187+ require .NoError (t , c .Set ("uint" , uint (234 )))
188+ require .NoError (t , c .Set ("obj.uint" , uint (234 )))
189+ require .Equal (t , uint (234 ), c .Get ("uint" ))
190+ require .Equal (t , uint (234 ), c .Get ("obj.uint" ))
191+ // Set uint using int
192+ require .NoError (t , c .Set ("uint" , 345 ))
193+ require .NoError (t , c .Set ("obj.uint" , 345 ))
194+ require .Equal (t , uint (345 ), c .Get ("uint" ))
195+ require .Equal (t , uint (345 ), c .Get ("obj.uint" ))
196+ // Set uint using float
197+ require .NoError (t , c .Set ("uint" , 456.0 ))
198+ require .NoError (t , c .Set ("obj.uint" , 456.0 ))
199+ require .Equal (t , uint (456 ), c .Get ("uint" ))
200+ require .Equal (t , uint (456 ), c .Get ("obj.uint" ))
201+ // Set uint using string
202+ require .EqualError (t , c .Set ("uint" , "567" ), "invalid type for key 'uint': invalid conversion, got string but want uint" )
203+ require .EqualError (t , c .Set ("obj.uint" , "567" ), "invalid type for key 'obj.uint': invalid conversion, got string but want uint" )
204+ require .Equal (t , uint (456 ), c .Get ("uint" ))
205+ require .Equal (t , uint (456 ), c .Get ("obj.uint" ))
206+
207+ json1 := []byte (`{"string":"abcd","int":1234,"obj":{"string":"abcd","int":1234}}` )
208+ require .NoError (t , json .Unmarshal (json1 , & c ))
209+ require .Equal (t , "abcd" , c .Get ("string" ))
210+ require .Equal (t , 1234 , c .Get ("int" ))
211+ require .Equal (t , "abcd" , c .Get ("obj.string" ))
212+ require .Equal (t , 1234 , c .Get ("obj.int" ))
159213
160214 json2 := []byte (`{"string":123,"int":123,"obj":{"string":"abc","int":123}}` )
161- require .Error (t , json .Unmarshal (json2 , & c ))
215+ require .EqualError (t , json .Unmarshal (json2 , & c ), "invalid type for key 'string': invalid conversion, got float64 but want string" )
162216 json3 := []byte (`{"string":"avc","int":123,"obj":{"string":123,"int":123}}` )
163- require .Error (t , json .Unmarshal (json3 , & c ))
217+ require .EqualError (t , json .Unmarshal (json3 , & c ), "invalid type for key 'obj.string': invalid conversion, got float64 but want string" )
164218}
0 commit comments