Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions flagarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func parseStruct(r KingpinRegistry, value reflect.Value, o opts) error {
return errors.Errorf("flagarize struct Tag found on non-addressable field %q", field.Name)
}

if _, ok := pointerToNativeMap[field.Type.String()]; ok {
return errors.Errorf("flagarize pointer to native type is not allowed for field %q", field.Name)
}

// Favor custom Flagarizers if specified.
d := &dedupFlagRegisterer{KingpinRegistry: r}
ok, err := invokeFlagarizersIfImplements(d, tag, fieldValue, field.Name)
Expand Down Expand Up @@ -521,3 +525,20 @@ func isTrue(v string) bool {
}
return b
}

var pointerToNativeMap = map[string]bool {
"*string": true,
"*bool": true,
"*uint": true,
"*uint8": true,
"*uint16": true,
"*uint32": true,
"*uint64": true,
"*int": true,
"*int8": true,
"*int16": true,
"*int32": true,
"*int64": true,
"*float32": true,
"*float64": true,
}
44 changes: 44 additions & 0 deletions flagarize_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ func newTestKingpin(t *testing.T) *kingpin.Application {
return app
}

func TestFlagarize_PointerToNative(t *testing.T) {
t.Run("flagarize on field with pointer to int", func(t *testing.T) {
x := 2
type wrong struct {
F *int `flagarize:"help=help"`
}
w := &wrong{F: &x}

app := newTestKingpin(t)
err := flagarize.Flagarize(app, w)
testutil.NotOk(t, err)
testutil.Equals(t, "flagarize: flagarize pointer to native type is not allowed for field \"F\"", err.Error())
})
t.Run("flagarize on field with pointer to string", func(t *testing.T) {
x := ""
type wrong struct {
F *string `flagarize:"help=help"`
}
w := &wrong{F: &x}

app := newTestKingpin(t)
err := flagarize.Flagarize(app, w)
testutil.NotOk(t, err)
testutil.Equals(t, "flagarize: flagarize pointer to native type is not allowed for field \"F\"", err.Error())
})
t.Run("flagarize on field with pointer to bool", func(t *testing.T) {
x := true

type wrongInner struct {
F *bool `flagarize:"help=help"`
}
type wrongOuter struct {
WrongInner *wrongInner `flagarize:"help=help"`
}

wInner := wrongInner{F: &x}
wOuter := &wrongOuter{WrongInner: &wInner}

app := newTestKingpin(t)
err := flagarize.Flagarize(app, wOuter)
testutil.NotOk(t, err)
testutil.Equals(t, "flagarize: flagarize struct Tag found on not supported type ptr *flagarize_test.wrongInner for field \"WrongInner\"", err.Error())
})
}
func TestFlagarize_Errors(t *testing.T) {
t.Run("flagarize on basic private field", func(t *testing.T) {
type wrong struct {
Expand Down