Decoders are now field mappers.

Mappers are responsible for mapping from command-line input to Go. This
is typically just decoding, but also includes other information such as
if the field is a bool.
This commit is contained in:
Alec Thomas
2018-06-03 16:07:30 +10:00
committed by Gerald Kaszuba
parent c8b487e49c
commit 48af58cefa
9 changed files with 337 additions and 288 deletions
+42
View File
@@ -0,0 +1,42 @@
package kong
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestValueMapper(t *testing.T) {
var cli struct {
Flag string
}
k := mustNew(t, &cli, ValueMapper(&cli.Flag, testMooMapper{}))
_, err := k.Parse(nil)
require.NoError(t, err)
require.Equal(t, "", cli.Flag)
_, err = k.Parse([]string{"--flag"})
require.NoError(t, err)
require.Equal(t, "MOO", cli.Flag)
}
func TestNamedMapper(t *testing.T) {
var cli struct {
Flag string `type:"moo"`
}
k := mustNew(t, &cli, NamedMapper("moo", testMooMapper{}))
_, err := k.Parse(nil)
require.NoError(t, err)
require.Equal(t, "", cli.Flag)
_, err = k.Parse([]string{"--flag"})
require.NoError(t, err)
require.Equal(t, "MOO", cli.Flag)
}
type testMooMapper struct{}
func (testMooMapper) Decode(ctx *DecoderContext, scan *Scanner, target reflect.Value) error {
target.SetString("MOO")
return nil
}
func (testMooMapper) IsBool() bool { return true }