-
Notifications
You must be signed in to change notification settings - Fork 125
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
chore(primitives): consolidated isValidHex usage #2031
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,6 +20,8 @@ | |||||||||||
|
||||||||||||
package hex | ||||||||||||
|
||||||||||||
import "errors" | ||||||||||||
|
||||||||||||
// has0xPrefix returns true if s has a 0x prefix. | ||||||||||||
func has0xPrefix[T []byte | string](s T) bool { | ||||||||||||
return len(s) >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') | ||||||||||||
|
@@ -56,28 +58,26 @@ | |||||||||||
|
||||||||||||
// formatAndValidateText validates the input text for a hex string. | ||||||||||||
func formatAndValidateText(input []byte) ([]byte, error) { | ||||||||||||
if len(input) == 0 { | ||||||||||||
switch err := isValidHex(input); { | ||||||||||||
case err == nil: | ||||||||||||
input = input[2:] | ||||||||||||
if len(input)%2 != 0 { | ||||||||||||
return nil, ErrOddLength | ||||||||||||
} | ||||||||||||
return input, nil | ||||||||||||
case errors.Is(err, ErrEmptyString): | ||||||||||||
return nil, nil // empty strings are allowed | ||||||||||||
default: | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
if !has0xPrefix(input) { | ||||||||||||
return nil, ErrMissingPrefix | ||||||||||||
} | ||||||||||||
input = input[2:] | ||||||||||||
if len(input)%2 != 0 { | ||||||||||||
return nil, ErrOddLength | ||||||||||||
} | ||||||||||||
return input, nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
// formatAndValidateNumber checks the input text for a hex number. | ||||||||||||
func formatAndValidateNumber[T []byte | string](input T) (T, error) { | ||||||||||||
// realistically, this shouldn't rarely error if called on | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typographical error in comment There's a typographical error in the comment which could cause confusion. Consider correcting it for clarity. Suggested change: -// realistically, this shouldn't rarely error if called on unwrapped hex.String
+// Realistically, this should rarely error if called on unwrapped hex.String 📝 Committable suggestion
Suggested change
|
||||||||||||
// unwrapped hex.String | ||||||||||||
if len(input) == 0 { | ||||||||||||
return *new(T), ErrEmptyString | ||||||||||||
} | ||||||||||||
if !has0xPrefix(input) { | ||||||||||||
return *new(T), ErrMissingPrefix | ||||||||||||
if err := isValidHex(input); err != nil { | ||||||||||||
return *new(T), err | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Simplify zero value initialization for generic type In the Apply this diff to improve readability: if err := isValidHex(input); err != nil {
- return *new(T), err
+ var zero T
+ return zero, err
} 📝 Committable suggestion
Suggested change
|
||||||||||||
} | ||||||||||||
input = input[2:] | ||||||||||||
if len(input) == 0 { | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -55,7 +55,7 @@ func (s *String) UnmarshalText(text []byte) error { | |||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func isValidHex(str string) error { | ||||||||||||||||||||
func isValidHex[T []byte | string](str T) error { | ||||||||||||||||||||
if len(str) == 0 { | ||||||||||||||||||||
return ErrEmptyString | ||||||||||||||||||||
} else if !has0xPrefix(str) { | ||||||||||||||||||||
|
@@ -67,13 +67,10 @@ func isValidHex(str string) error { | |||||||||||||||||||
// NewStringStrict creates a hex string with 0x prefix. It errors if any of the | ||||||||||||||||||||
// string invariants are violated. | ||||||||||||||||||||
func NewStringStrict[T []byte | string](s T) (String, error) { | ||||||||||||||||||||
str := string(s) | ||||||||||||||||||||
if len(str) == 0 { | ||||||||||||||||||||
return "", ErrEmptyString | ||||||||||||||||||||
} else if !has0xPrefix(str) { | ||||||||||||||||||||
return "", ErrMissingPrefix | ||||||||||||||||||||
if err := isValidHex(s); err != nil { | ||||||||||||||||||||
return "", err | ||||||||||||||||||||
} | ||||||||||||||||||||
return String(str), nil | ||||||||||||||||||||
return String(s), nil | ||||||||||||||||||||
Comment on lines
+70
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix type conversion when returning In Apply the following diff to fix the type conversion: func NewStringStrict[T []byte | string](s T) (String, error) {
if err := isValidHex(s); err != nil {
return "", err
}
- return String(s), nil
+ return String(string(s)), nil
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// FromBytes creates a hex string with 0x prefix. | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can leave a comment to bypass nilaway given that isValidHex assumes that the string must be prefixed by 0x, so we know its safe to strip if err was nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Niaway is definitely allucinating here. Not sure how to suppress the error, since
//nolint:nilawa
does not seem to work