Skip to content

Commit

Permalink
Support string pointers for path mapper (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-stephen authored Mar 5, 2024
1 parent 088cd78 commit 4a9fe05
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
43 changes: 43 additions & 0 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4a9fe05

Please sign in to comment.