-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: add initial code #2
Conversation
@ldemailly could you please review ? |
99cd50f
to
b9308ff
Compare
the test coverage is limited for now I will need to consider to generate the tests. Using generics in the code has huge consequences on unit tests
@@ -0,0 +1,5 @@ | |||
module github.com/ccoveille/go-safecast | |||
|
|||
go 1.23.0 |
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.
1.23.1 is out
(and personally I would put just 1.23 so you don't have to keep changing it)
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.
or more importantly... you don't want to force users to use 1.23, I wouldn't yet for anything prod
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.
That's right, 1.22 is enough
I simply ran go mod init
that set it up like this
Apparently, I'm still go 1.23.0 locally
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.
Fixed in 328cfff
I think I have a much simpler implementation package safecast
import (
"errors"
)
type Integer interface {
~int | ~uint | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64
}
var ErrOutOfRange = errors.New("out of range")
func Convert[Tout Integer, Tin Integer](orig Tin) (converted Tout, err error) {
converted = Tout(orig)
if Tin(converted) != orig {
err = ErrOutOfRange
}
return
} done, for all types! |
sample test/demo: func TestConvert(t *testing.T) {
var inp uint32 = 42
out, err := safecast.Convert[int8](inp)
t.Logf("Out is %T: %v", out, out)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if out != 42 {
t.Errorf("unexpected value: %v", out)
}
inp = 129
_, err = safecast.Convert[int8](inp)
t.Logf("Got err: %v", err)
if err == nil {
t.Errorf("expected error")
}
} outputs
|
unfortunately your implementation is not safe about the modulo that int conversion implies int8(0) == int8(-256) == int8(256) == int8(512) ... https://go.dev/play/p/0g3MBAC36we I will iterate. Thanks for your inspiring example |
Closed in favor of #9 |
Unfortunately this implementation has some caveats |
In case someone does archeology here, thanks for finding that bug, the fixed version is (added the 2 tests too) |
the test coverage is limited for now
I will need to consider to generate the tests.
Using generics in the code has huge consequences on unit tests