Support string pointers for path mapper (#360)

This commit is contained in:
Chris Stephen
2024-03-05 04:59:11 -04:00
committed by GitHub
parent 088cd7874e
commit 4a9fe056ea
2 changed files with 49 additions and 0 deletions
+6
View File
@@ -580,6 +580,12 @@ func pathMapper(r *Registry) MapperFunc {
if target.Kind() == reflect.Slice {
return sliceDecoder(r)(ctx, target)
}
if target.Kind() == reflect.Ptr && target.Elem().Kind() == reflect.String {
if target.IsNil() {
return nil
}
target = target.Elem()
}
if target.Kind() != reflect.String {
return fmt.Errorf("\"path\" type must be applied to a string not %s", target.Type())
}
+43
View File
@@ -487,6 +487,49 @@ func TestFileContentMapper(t *testing.T) {
assert.Contains(t, err.Error(), "is a directory")
}
func TestPathMapperUsingStringPointer(t *testing.T) {
type CLI struct {
Path *string `type:"path"`
}
var cli CLI
t.Run("With value", func(t *testing.T) {
p := mustNew(t, &cli)
_, err := p.Parse([]string{"--path", "/foo/bar"})
assert.NoError(t, err)
assert.NotZero(t, cli.Path)
assert.Equal(t, "/foo/bar", *cli.Path)
})
t.Run("Zero value", func(t *testing.T) {
p := mustNew(t, &cli)
_, err := p.Parse([]string{"--path", ""})
assert.NoError(t, err)
assert.NotZero(t, cli.Path)
wd, err := os.Getwd()
assert.NoError(t, err)
assert.Equal(t, wd, *cli.Path)
})
t.Run("Without value", func(t *testing.T) {
p := mustNew(t, &cli)
_, err := p.Parse([]string{"--"})
assert.NoError(t, err)
assert.Equal(t, nil, cli.Path)
})
t.Run("Non-string pointer", func(t *testing.T) {
type CLI struct {
Path *any `type:"path"`
}
var cli CLI
p := mustNew(t, &cli)
_, err := p.Parse([]string{"--path", ""})
assert.Error(t, err)
assert.Contains(t, err.Error(), `"path" type must be applied to a string`)
})
}
//nolint:dupl
func TestExistingFileMapper(t *testing.T) {
type CLI struct {