diff --git a/flagarize.go b/flagarize.go index f886905..5b19ea1 100644 --- a/flagarize.go +++ b/flagarize.go @@ -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) @@ -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, +} diff --git a/flagarize_ext_test.go b/flagarize_ext_test.go index 0358e57..33f012d 100644 --- a/flagarize_ext_test.go +++ b/flagarize_ext_test.go @@ -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 {