From 194c0b04056882a82e5317fa27f1505899f9732a Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 27 Mar 2017 20:03:19 +1300 Subject: [PATCH] allow unmarshalling consts as strings --- cmd/webcmd/webcmd.conf-sample | 8 ++++---- config.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/cmd/webcmd/webcmd.conf-sample b/cmd/webcmd/webcmd.conf-sample index a6459f2..42f0a2c 100644 --- a/cmd/webcmd/webcmd.conf-sample +++ b/cmd/webcmd/webcmd.conf-sample @@ -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"} ] } diff --git a/config.go b/config.go index a634109..320b5da 100644 --- a/config.go +++ b/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 {