diff --git a/comid/svn.go b/comid/svn.go index 6d634a2..692fad1 100644 --- a/comid/svn.go +++ b/comid/svn.go @@ -113,34 +113,11 @@ func NewTaggedSVN(val any) (*SVN, error) { return &SVN{&ret}, nil } - switch t := val.(type) { - case string: - u, err := strconv.ParseUint(t, 10, 64) - if err != nil { - return nil, err - } - ret = TaggedSVN(u) - case TaggedSVN: - ret = t - case *TaggedSVN: - ret = *t - case uint64: - ret = TaggedSVN(t) - case uint: - ret = TaggedSVN(t) - case int: - if t < 0 { - return nil, fmt.Errorf("SVN cannot be negative: %d", t) - } - ret = TaggedSVN(t) - case int64: - if t < 0 { - return nil, fmt.Errorf("SVN cannot be negative: %d", t) - } - ret = TaggedSVN(t) - default: - return nil, fmt.Errorf("unexpected type for SVN exact-value: %T", t) + u, err := convertToSVNUint64(val) + if err != nil { + return nil, err } + ret = TaggedSVN(u) return &SVN{&ret}, nil } @@ -175,34 +152,11 @@ func NewTaggedMinSVN(val any) (*SVN, error) { return &SVN{&ret}, nil } - switch t := val.(type) { - case string: - u, err := strconv.ParseUint(t, 10, 64) - if err != nil { - return nil, err - } - ret = TaggedMinSVN(u) - case TaggedMinSVN: - ret = t - case *TaggedMinSVN: - ret = *t - case uint64: - ret = TaggedMinSVN(t) - case uint: - ret = TaggedMinSVN(t) - case int: - if t < 0 { - return nil, fmt.Errorf("SVN cannot be negative: %d", t) - } - ret = TaggedMinSVN(t) - case int64: - if t < 0 { - return nil, fmt.Errorf("SVN cannot be negative: %d", t) - } - ret = TaggedMinSVN(t) - default: - return nil, fmt.Errorf("unexpected type for SVN min-value: %T", t) + u, err := convertToSVNUint64(val) + if err != nil { + return nil, err } + ret = TaggedMinSVN(u) return &SVN{&ret}, nil } @@ -228,6 +182,42 @@ func (o TaggedMinSVN) Valid() error { return nil } +// convertToSVNUint64 converts various SVN types to uint64. +func convertToSVNUint64(val any) (uint64, error) { + switch t := val.(type) { + case string: + u, err := strconv.ParseUint(t, 10, 64) + if err != nil { + return 0, err + } + return u, nil + case uint64: + return t, nil + case uint: + return uint64(t), nil + case int: + if t < 0 { + return 0, fmt.Errorf("SVN cannot be negative: %d", t) + } + return uint64(t), nil + case int64: + if t < 0 { + return 0, fmt.Errorf("SVN cannot be negative: %d", t) + } + return uint64(t), nil + case TaggedSVN: + return uint64(t), nil + case *TaggedSVN: + return uint64(*t), nil + case TaggedMinSVN: + return uint64(t), nil + case *TaggedMinSVN: + return uint64(*t), nil + default: + return 0, fmt.Errorf("unexpected type for SVN: %T", t) + } +} + // ISVNFactory defines the signature for the factory functions that may be // registred using RegisterSVNType to provide a new implementation of the // corresponding type choice. The factory function should create a new *SVN diff --git a/comid/svn_test.go b/comid/svn_test.go index 6127e0b..b9d29ad 100644 --- a/comid/svn_test.go +++ b/comid/svn_test.go @@ -103,7 +103,7 @@ func Test_NewSVN(t *testing.T) { assert.NoError(t, err) _, err = NewSVN(true, "exact-value") - assert.EqualError(t, err, "unexpected type for SVN exact-value: bool") + assert.EqualError(t, err, "unexpected type for SVN: bool") inMin := TaggedMinSVN(7) @@ -114,7 +114,7 @@ func Test_NewSVN(t *testing.T) { assert.NoError(t, err) _, err = NewSVN(true, "min-value") - assert.EqualError(t, err, "unexpected type for SVN min-value: bool") + assert.EqualError(t, err, "unexpected type for SVN: bool") _, err = NewSVN(true, "test") assert.EqualError(t, err, "unknown SVN type: test")