allow unmarshalling consts as strings

This commit is contained in:
mappu 2017-03-27 20:03:19 +13:00
parent e7689eabad
commit 194c0b0405
2 changed files with 35 additions and 6 deletions

View File

@ -10,10 +10,10 @@
{
"Title": "Ping",
"Execution": [
{"ParamType": 0, "Value": "/bin/ping"},
{"ParamType": 0, "Value": "-c"},
{"ParamType": 0, "Value": "4"},
{"ParamType": 0, "Value": "--"},
"/bin/ping",
"-c",
"4",
"--",
{"ParamType": 1, "Value": "example.com", "Description": "Target host"}
]
}

View File

@ -1,5 +1,10 @@
package webcmd
import (
"encoding/json"
"errors"
)
type ParamType int
const (
@ -16,15 +21,39 @@ const (
)
type InputParam struct {
Description string `json:",omitempty"` // only use for editable parameters
Description string `json:",omitempty"` // only used for editable parameters
ParamType ParamType
Value string
}
func (ip *InputParam) JSONUnmarshal(b []byte) error {
if b[0] == '"' {
ip.Description = ""
ip.ParamType = PARAMTYPE_CONST
return json.Unmarshal(b, &ip.Value)
} else if b[0] == '{' {
read := struct {
Description string `json:",omitempty"`
ParamType ParamType
Value string
}{}
err := json.Unmarshal(b, &read)
if err != nil {
return err
}
*ip = read
return nil
} else {
return errors.New("Malformed InputParam")
}
}
type CommandConfig struct {
Title string
WorkingDir string // default empty-string: getcwd()
Execution []InputParam // TODO allow plain strings as a shorthand for PARAMTYPE_CONST
Execution []InputParam // Can be unmarshalled using plain strings
}
type AppConfig struct {