Use interface{} instead of string in tokens.
This allows the scanner and resolvers to pass Go types around rather than having to serialise/deserialise to/from strings.
This commit is contained in:
+6
-47
@@ -2,7 +2,6 @@ package kong
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
@@ -15,15 +14,15 @@ type Resolver interface {
|
||||
Validate(app *Application) error
|
||||
|
||||
// Resolve the value for a Flag.
|
||||
Resolve(context *Context, parent *Path, flag *Flag) (string, error)
|
||||
Resolve(context *Context, parent *Path, flag *Flag) (interface{}, error)
|
||||
}
|
||||
|
||||
// ResolverFunc is a convenience type for non-validating Resolvers.
|
||||
type ResolverFunc func(context *Context, parent *Path, flag *Flag) (string, error)
|
||||
type ResolverFunc func(context *Context, parent *Path, flag *Flag) (interface{}, error)
|
||||
|
||||
var _ Resolver = ResolverFunc(nil)
|
||||
|
||||
func (r ResolverFunc) Resolve(context *Context, parent *Path, flag *Flag) (string, error) { // nolint: golint
|
||||
func (r ResolverFunc) Resolve(context *Context, parent *Path, flag *Flag) (interface{}, error) { // nolint: golint
|
||||
return r(context, parent, flag)
|
||||
}
|
||||
func (r ResolverFunc) Validate(app *Application) error { return nil } // nolint: golint
|
||||
@@ -37,54 +36,14 @@ func JSON(r io.Reader) (Resolver, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var f ResolverFunc = func(context *Context, parent *Path, flag *Flag) (string, error) {
|
||||
var f ResolverFunc = func(context *Context, parent *Path, flag *Flag) (interface{}, error) {
|
||||
name := strings.Replace(flag.Name, "-", "_", -1)
|
||||
raw, ok := values[name]
|
||||
if !ok {
|
||||
return "", nil
|
||||
return nil, nil
|
||||
}
|
||||
sep := flag.Tag.Sep
|
||||
value, err := jsonDecodeValue(sep, raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return value, nil
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func jsonDecodeValue(sep rune, value interface{}) (string, error) {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return v, nil
|
||||
case float64:
|
||||
return fmt.Sprintf("%v", v), nil
|
||||
case []interface{}:
|
||||
out := []string{}
|
||||
for _, el := range v {
|
||||
sel, err := jsonDecodeValue(sep, el)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
out = append(out, sel)
|
||||
}
|
||||
return JoinEscaped(out, sep), nil
|
||||
case map[string]interface{}:
|
||||
out := []string{}
|
||||
for key, el := range v {
|
||||
sel, err := jsonDecodeValue(sep, el)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
out = append(out, fmt.Sprintf("%s=%s", key, sel))
|
||||
}
|
||||
return JoinEscaped(out, ';'), nil
|
||||
case bool:
|
||||
if v {
|
||||
return "true", nil
|
||||
}
|
||||
return "false", nil
|
||||
}
|
||||
return "", fmt.Errorf("unsupported JSON value %v (of type %T)", value, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user