Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
feudalScripts
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
feudal
feudalScripts
Commits
86276497
Commit
86276497
authored
May 01, 2020
by
lukas.burgey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix handling of interface slices
parent
229d752a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
54 deletions
+37
-54
v3/scripts.go
v3/scripts.go
+17
-15
v3/scripts_test.go
v3/scripts_test.go
+20
-39
No files found.
v3/scripts.go
View file @
86276497
...
...
@@ -195,27 +195,29 @@ func UnmarshalOutput(inputBytes []byte, o *Output) (err error) {
// check answer type
switch
t
:=
answer
.
(
type
)
{
case
bool
:
break
case
string
:
break
case
[]
string
:
break
case
int
:
break
case
[]
int
:
case
bool
,
string
,
int
:
break
// converting floats to ints here, as floats are _not_ intended here
case
float64
:
o
.
QuestionnaireAnswers
[
qName
]
=
int
(
answer
.
(
float64
))
break
case
[]
float64
:
floats
:=
answer
.
([]
float64
)
ints
:=
make
([]
int
,
len
(
floats
))
for
i
,
v
:=
range
floats
{
ints
[
i
]
=
int
(
v
)
case
[]
interface
{}
:
list
:=
answer
.
([]
interface
{})
if
len
(
list
)
>
0
{
// determine type of list
switch
list
[
0
]
.
(
type
)
{
case
string
:
continue
case
float64
:
intList
:=
make
([]
int
,
len
(
list
))
for
i
,
v
:=
range
list
{
intList
[
i
]
=
int
(
v
.
(
float64
))
}
o
.
QuestionnaireAnswers
[
qName
]
=
intList
default
:
err
=
fmt
.
Errorf
(
"QuestionnaireAnswers contains invalid answer: %v (%T)"
,
answer
,
t
)
}
}
o
.
QuestionnaireAnswers
[
qName
]
=
ints
break
default
:
err
=
fmt
.
Errorf
(
"QuestionnaireAnswers contains invalid answer: %v (%T)"
,
answer
,
t
)
...
...
v3/scripts_test.go
View file @
86276497
package
scripts
import
(
"encoding/json"
"fmt"
"testing"
)
func
checkOutput
(
o
*
Output
)
(
err
error
)
{
if
o
.
Msg
==
""
{
return
fmt
.
Errorf
(
"Output.Msg is empty"
)
}
return
}
func
testOutput
(
rawOutput
[]
byte
,
t
*
testing
.
T
)
{
var
err
error
output
:=
new
(
Output
)
err
=
json
.
Unmarshal
(
rawOutput
,
output
)
if
err
!=
nil
{
t
.
Errorf
(
"Unmarshal failed: %s"
,
err
)
}
err
=
checkOutput
(
output
)
err
=
UnmarshalOutput
(
rawOutput
,
output
)
if
err
!=
nil
{
t
.
Errorf
(
"
checkOutput failed: %s"
,
err
)
t
.
Errorf
(
"
Unmarshal failed: %s for output:
\n
%s"
,
err
,
rawOutput
)
}
}
func
TestArbitraryQuestionnaire
(
t
*
testing
.
T
)
{
rawOutputs
:=
[][]
byte
{
[]
byte
(
`{
"message": "foo",
"questionnaire": {
"A string question": "answer"
}
}`
),
[]
byte
(
`{
"message": "foo",
"questionnaire": {
"An integer question": 0
}
}`
),
[]
byte
(
`{
"message": "foo",
"questionnaire": {
"A boolean question": false
}
}`
),
[]
byte
(
`{
func
TestQuestionnaireAnswers
(
t
*
testing
.
T
)
{
baseOutput
:=
`{
"message": "foo",
"state": "questionnaire",
"questionnaire": {
"A selector question": ["option a", "option b"]
"qname": "foo"
},
"questionnaire_answers": {
"qname": %s
}
}`
),
}`
rawOutputs
:=
[]
string
{
`"answer"`
,
"18"
,
"false"
,
`["option a", "option b"]`
,
"[8, 19]"
,
}
for
_
,
rawOutput
:=
range
rawOutputs
{
testOutput
(
rawOutput
,
t
)
for
_
,
qa
:=
range
rawOutputs
{
testOutput
(
[]
byte
(
fmt
.
Sprintf
(
baseOutput
,
qa
))
,
t
)
}
}
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