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", "Title": "Ping",
"Execution": [ "Execution": [
{"ParamType": 0, "Value": "/bin/ping"}, "/bin/ping",
{"ParamType": 0, "Value": "-c"}, "-c",
{"ParamType": 0, "Value": "4"}, "4",
{"ParamType": 0, "Value": "--"}, "--",
{"ParamType": 1, "Value": "example.com", "Description": "Target host"} {"ParamType": 1, "Value": "example.com", "Description": "Target host"}
] ]
} }

View File

@ -1,5 +1,10 @@
package webcmd package webcmd
import (
"encoding/json"
"errors"
)
type ParamType int type ParamType int
const ( const (
@ -16,15 +21,39 @@ const (
) )
type InputParam struct { type InputParam struct {
Description string `json:",omitempty"` // only use for editable parameters Description string `json:",omitempty"` // only used for editable parameters
ParamType ParamType ParamType ParamType
Value string 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 { type CommandConfig struct {
Title string Title string
WorkingDir string // default empty-string: getcwd() 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 { type AppConfig struct {