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