Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SVN Value Conversion Logic into a Separate Function #149

Merged
merged 7 commits into from
Jan 8, 2025
Merged
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
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
Loading