mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 22:13:44 +03:00
[hw-4] add postgres db
This commit is contained in:
19
loms/internal/infra/tools/safecast.go
Normal file
19
loms/internal/infra/tools/safecast.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func SafeCastInt64ToUInt32(num int64) (uint32, error) {
|
||||
if num < 0 {
|
||||
return 0, fmt.Errorf("tried casting signed negative number to unsigned number")
|
||||
}
|
||||
|
||||
if num > math.MaxUint32 {
|
||||
return 0, fmt.Errorf("tried casting larger number than uint32 can store")
|
||||
}
|
||||
|
||||
// the bounds are checked, and cast should be safe.
|
||||
return uint32(num), nil // #nosec G115
|
||||
}
|
||||
Reference in New Issue
Block a user