diff --git a/pkg/types/args.go b/pkg/types/args.go index 66dcf9ea..bd8640fc 100644 --- a/pkg/types/args.go +++ b/pkg/types/args.go @@ -63,6 +63,12 @@ func GetKeyField(keyString string, v reflect.Value) reflect.Value { return v.Elem().FieldByName(keyString) } +// UnmarshalableArgsError is used to indicate error unmarshalling args +// from the args-string in the form "K=V;K2=V2;..." +type UnmarshalableArgsError struct { + error +} + // LoadArgs parses args from a string in the form "K=V;K2=V2;..." func LoadArgs(args string, container interface{}) error { if args == "" { @@ -85,8 +91,13 @@ func LoadArgs(args string, container interface{}) error { unknownArgs = append(unknownArgs, pair) continue } - - u := keyField.Addr().Interface().(encoding.TextUnmarshaler) + keyFieldIface := keyField.Addr().Interface() + u, ok := keyFieldIface.(encoding.TextUnmarshaler) + if !ok { + return UnmarshalableArgsError{fmt.Errorf( + "ARGS: cannot unmarshal into field '%s' - type '%s' does not implement encoding.TextUnmarshaler", + keyString, reflect.TypeOf(keyFieldIface))} + } err := u.UnmarshalText([]byte(valueString)) if err != nil { return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err) diff --git a/pkg/types/args_test.go b/pkg/types/args_test.go index 3a53d9a4..23772ca9 100644 --- a/pkg/types/args_test.go +++ b/pkg/types/args_test.go @@ -118,4 +118,15 @@ var _ = Describe("LoadArgs", func() { Expect(err).NotTo(HaveOccurred()) }) }) + + Context("When known arguments are passed and cannot be unmarshalled", func() { + It("LoadArgs should fail", func() { + conf := struct { + IP IPNet + }{} + err := LoadArgs("IP=10.0.0.0/24", &conf) + Expect(err).To(HaveOccurred()) + + }) + }) })