Skip to content

Commit

Permalink
refactor: put SVN conversion logic into a separate function
Browse files Browse the repository at this point in the history
Refactor SVN conversion logic into a separate function.

Signed-off-by: Akhilesh Kr. Yadav <[email protected]>
  • Loading branch information
Akhilesh-max authored Jan 8, 2025
1 parent a148490 commit ca718d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 56 deletions.
98 changes: 44 additions & 54 deletions comid/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions comid/svn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand Down

0 comments on commit ca718d8

Please sign in to comment.