Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-cli
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Operations
Operations
Metrics
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-cli
Commits
cec1fdb8
Commit
cec1fdb8
authored
May 06, 2024
by
Cristian Maglie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved array handling in configmap, and unit-tests
parent
c355478f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
10 deletions
+66
-10
internal/go-configmap/configuration.go
internal/go-configmap/configuration.go
+4
-2
internal/go-configmap/configuration_test.go
internal/go-configmap/configuration_test.go
+62
-8
No files found.
internal/go-configmap/configuration.go
View file @
cec1fdb8
...
...
@@ -100,14 +100,16 @@ func tryConversion(current any, desiredType reflect.Type) (any, error) {
if
!
ok
&&
current
!=
nil
{
break
}
resArray
:=
reflect
.
MakeSlice
(
desiredType
,
len
(
currentArray
),
len
(
currentArray
))
for
i
,
elem
:=
range
currentArray
{
newElem
,
err
:=
tryConversion
(
elem
,
desiredType
.
Elem
())
if
err
!=
nil
{
return
nil
,
err
}
currentArray
[
i
]
=
newElem
resArray
.
Index
(
i
)
.
Set
(
reflect
.
ValueOf
(
newElem
))
}
return
currentArray
,
nil
return
resArray
.
Interface
()
,
nil
}
currentTypeString
:=
currentType
.
String
()
...
...
internal/go-configmap/configuration_test.go
View file @
cec1fdb8
...
...
@@ -146,19 +146,73 @@ func TestSchema(t *testing.T) {
c
.
SetKeyTypeSchema
(
"int"
,
15
)
c
.
SetKeyTypeSchema
(
"obj.string"
,
""
)
c
.
SetKeyTypeSchema
(
"obj.int"
,
15
)
c
.
SetKeyTypeSchema
(
"uint"
,
uint
(
15
))
c
.
SetKeyTypeSchema
(
"obj.uint"
,
uint
(
15
))
c
.
SetKeyTypeSchema
(
"array"
,
[]
string
{})
c
.
SetKeyTypeSchema
(
"obj.array"
,
[]
string
{})
// Set array of string
require
.
NoError
(
t
,
c
.
Set
(
"array"
,
[]
string
{
"abc"
,
"def"
}))
require
.
NoError
(
t
,
c
.
Set
(
"obj.array"
,
[]
string
{
"abc"
,
"def"
}))
require
.
Equal
(
t
,
[]
string
{
"abc"
,
"def"
},
c
.
Get
(
"array"
))
require
.
Equal
(
t
,
[]
string
{
"abc"
,
"def"
},
c
.
Get
(
"obj.array"
))
// Set array of string with array of any
require
.
NoError
(
t
,
c
.
Set
(
"array"
,
[]
any
{
"abc"
,
"def"
}))
require
.
NoError
(
t
,
c
.
Set
(
"obj.array"
,
[]
any
{
"abc"
,
"def"
}))
require
.
Equal
(
t
,
[]
string
{
"abc"
,
"def"
},
c
.
Get
(
"array"
))
require
.
Equal
(
t
,
[]
string
{
"abc"
,
"def"
},
c
.
Get
(
"obj.array"
))
// Set array of string with array of int
require
.
EqualError
(
t
,
c
.
Set
(
"array"
,
[]
any
{
"abc"
,
123
}),
"invalid type for key 'array': invalid conversion, got int but want string"
)
require
.
EqualError
(
t
,
c
.
Set
(
"obj.array"
,
[]
any
{
"abc"
,
123
}),
"invalid type for key 'obj.array': invalid conversion, got int but want string"
)
// Set string
require
.
NoError
(
t
,
c
.
Set
(
"string"
,
"abc"
))
require
.
Error
(
t
,
c
.
Set
(
"string"
,
123
))
require
.
NoError
(
t
,
c
.
Set
(
"int"
,
123
))
require
.
Error
(
t
,
c
.
Set
(
"int"
,
"abc"
))
require
.
NoError
(
t
,
c
.
Set
(
"obj.string"
,
"abc"
))
require
.
Equal
(
t
,
"abc"
,
c
.
Get
(
"string"
))
require
.
Equal
(
t
,
"abc"
,
c
.
Get
(
"obj.string"
))
// Set string with int
require
.
EqualError
(
t
,
c
.
Set
(
"string"
,
123
),
"invalid type for key 'string': invalid conversion, got int but want string"
)
require
.
EqualError
(
t
,
c
.
Set
(
"obj.string"
,
123
),
"invalid type for key 'obj.string': invalid conversion, got int but want string"
)
json1
:=
[]
byte
(
`{"string":"abc","int":123,"obj":{"string":"abc","int":123}}`
)
require
.
NoError
(
t
,
json
.
Unmarshal
(
json1
,
&
c
))
require
.
Equal
(
t
,
"abc"
,
c
.
Get
(
"string"
))
// Set int
require
.
NoError
(
t
,
c
.
Set
(
"int"
,
123
))
require
.
NoError
(
t
,
c
.
Set
(
"obj.int"
,
123
))
require
.
Equal
(
t
,
123
,
c
.
Get
(
"int"
))
require
.
Equal
(
t
,
123
,
c
.
Get
(
"obj.int"
))
// Set int with string
require
.
EqualError
(
t
,
c
.
Set
(
"int"
,
"abc"
),
"invalid type for key 'int': invalid conversion, got string but want int"
)
require
.
EqualError
(
t
,
c
.
Set
(
"obj.int"
,
"abc"
),
"invalid type for key 'obj.int': invalid conversion, got string but want int"
)
// Set uint
require
.
NoError
(
t
,
c
.
Set
(
"uint"
,
uint
(
234
)))
require
.
NoError
(
t
,
c
.
Set
(
"obj.uint"
,
uint
(
234
)))
require
.
Equal
(
t
,
uint
(
234
),
c
.
Get
(
"uint"
))
require
.
Equal
(
t
,
uint
(
234
),
c
.
Get
(
"obj.uint"
))
// Set uint using int
require
.
NoError
(
t
,
c
.
Set
(
"uint"
,
345
))
require
.
NoError
(
t
,
c
.
Set
(
"obj.uint"
,
345
))
require
.
Equal
(
t
,
uint
(
345
),
c
.
Get
(
"uint"
))
require
.
Equal
(
t
,
uint
(
345
),
c
.
Get
(
"obj.uint"
))
// Set uint using float
require
.
NoError
(
t
,
c
.
Set
(
"uint"
,
456.0
))
require
.
NoError
(
t
,
c
.
Set
(
"obj.uint"
,
456.0
))
require
.
Equal
(
t
,
uint
(
456
),
c
.
Get
(
"uint"
))
require
.
Equal
(
t
,
uint
(
456
),
c
.
Get
(
"obj.uint"
))
// Set uint using string
require
.
EqualError
(
t
,
c
.
Set
(
"uint"
,
"567"
),
"invalid type for key 'uint': invalid conversion, got string but want uint"
)
require
.
EqualError
(
t
,
c
.
Set
(
"obj.uint"
,
"567"
),
"invalid type for key 'obj.uint': invalid conversion, got string but want uint"
)
require
.
Equal
(
t
,
uint
(
456
),
c
.
Get
(
"uint"
))
require
.
Equal
(
t
,
uint
(
456
),
c
.
Get
(
"obj.uint"
))
json1
:=
[]
byte
(
`{"string":"abcd","int":1234,"obj":{"string":"abcd","int":1234}}`
)
require
.
NoError
(
t
,
json
.
Unmarshal
(
json1
,
&
c
))
require
.
Equal
(
t
,
"abcd"
,
c
.
Get
(
"string"
))
require
.
Equal
(
t
,
1234
,
c
.
Get
(
"int"
))
require
.
Equal
(
t
,
"abcd"
,
c
.
Get
(
"obj.string"
))
require
.
Equal
(
t
,
1234
,
c
.
Get
(
"obj.int"
))
json2
:=
[]
byte
(
`{"string":123,"int":123,"obj":{"string":"abc","int":123}}`
)
require
.
E
rror
(
t
,
json
.
Unmarshal
(
json2
,
&
c
)
)
require
.
E
qualError
(
t
,
json
.
Unmarshal
(
json2
,
&
c
),
"invalid type for key 'string': invalid conversion, got float64 but want string"
)
json3
:=
[]
byte
(
`{"string":"avc","int":123,"obj":{"string":123,"int":123}}`
)
require
.
E
rror
(
t
,
json
.
Unmarshal
(
json3
,
&
c
)
)
require
.
E
qualError
(
t
,
json
.
Unmarshal
(
json3
,
&
c
),
"invalid type for key 'obj.string': invalid conversion, got float64 but want string"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment