Commit 15164ca0 authored by lukas.burgey's avatar lukas.burgey

Fix answer checking in stubScript

parent 86276497
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"time"
scripts "git.scc.kit.edu/feudal/feudalScripts/v3"
......@@ -49,13 +49,41 @@ func deploy(input scripts.Input) (output scripts.Output, err error) {
}
func questionnaire(input scripts.Input) (output scripts.Output, err error) {
qa := map[string]interface{}{
"question_name": "question", // string default value
"age_question": 18, // age_question must be an integer, defaulting to 18
"list_question": []string{"person_a", "person_b"}, // list_question must be one of the listed options
"list_question_2": []int{1, 2}, // list_question_2 must be one of the listed options
"are_you_sure": false, // are_you_sure must be a boolean, with false being the default value
}
time.Sleep(time.Second)
if input.Answers != nil {
_, fooOK := input.Answers["Question-Foo"]
_, barOK := input.Answers["Question-Bar"]
missing := false
falseType := false
// check Answers against QuestionnaireAnswers
for key, qAnswer := range qa {
if answer, ok := input.Answers[key]; ok {
//check type of answer against reference
var referenceAnswer interface{}
if qaList, ok := qAnswer.([]interface{}); ok {
referenceAnswer = qaList[0]
} else {
referenceAnswer = qaList
}
if reflect.TypeOf(referenceAnswer) != reflect.TypeOf(answer) {
falseType = true
break
}
} else {
missing = true
break
}
}
if fooOK && barOK {
if !missing || !falseType {
output.State = scripts.Deployed
output.Msg = "I got all the answers"
return
......@@ -70,13 +98,7 @@ func questionnaire(input scripts.Input) (output scripts.Output, err error) {
"list_question_2": "How many do you want?",
"are_you_sure": "What you are trying is wrong. Are you sure?",
}
output.QuestionnaireAnswers = map[string]interface{}{
"question_name": "question", // string default value
"age_question": 18, // age_question must be an integer, defaulting to 18
"list_question": []string{"person_a", "person_b"}, // list_question must be one of the listed options
"list_question_2": []int{1, 2}, // list_question_2 must be one of the listed options
"are_you_sure": false, // are_you_sure must be a boolean, with false being the default value
}
output.QuestionnaireAnswers = qa
return
}
......@@ -146,6 +168,6 @@ func main() {
}
}
outputBytes, err := json.MarshalIndent(output, "", " ")
outputBytes, err := output.Marshal()
fmt.Fprintf(os.Stdout, "%s", outputBytes)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment