allow unmarshalling consts as strings
This commit is contained in:
parent
e7689eabad
commit
194c0b0405
@ -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"}
|
||||
]
|
||||
}
|
||||
|
33
config.go
33
config.go
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user