diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go index d6482fa..029344e 100644 --- a/pkg/codegen/utils.go +++ b/pkg/codegen/utils.go @@ -196,47 +196,54 @@ func getMinIntType( const i64 = "int64" func adjustForSignedBounds(nMin, nMax *float64) (string, bool, bool) { - switch { - case nMin == nil && nMax == nil: + if nMin == nil && nMax == nil { return i64, false, false + } + + minRounded := math.Round(*nMin) + maxRounded := math.Round(*nMax) + switch { case nMin == nil: - return i64, false, int64(math.Round(*nMax)) == math.MaxInt64 + return i64, false, maxRounded == float64(math.MaxInt64) case nMax == nil: - return i64, int64(math.Round(*nMin)) == math.MinInt64, false + return i64, minRounded == float64(math.MinInt64), false - case *nMin < math.MinInt32 || *nMax > math.MaxInt32: - return i64, int64(math.Round(*nMin)) == math.MinInt64, int64(math.Round(*nMax)) == math.MaxInt64 + case minRounded < float64(math.MinInt32) || maxRounded > float64(math.MaxInt32): + return i64, minRounded == float64(math.MinInt64), maxRounded == float64(math.MaxInt64) - case *nMin < math.MinInt16 || *nMax > math.MaxInt16: - return "int32", int32(math.Round(*nMin)) == math.MinInt32, int32(math.Round(*nMax)) == math.MaxInt32 + case minRounded < float64(math.MinInt16) || maxRounded > float64(math.MaxInt16): + return "int32", minRounded == float64(math.MinInt32), maxRounded == float64(math.MaxInt32) - case *nMin < math.MinInt8 || *nMax > math.MaxInt8: - return "int16", int16(math.Round(*nMin)) == math.MinInt16, int16(math.Round(*nMax)) == math.MaxInt16 + case minRounded < float64(math.MinInt8) || maxRounded > float64(math.MaxInt8): + return "int16", minRounded == float64(math.MinInt16), maxRounded == float64(math.MaxInt16) default: - return "int8", int8(math.Round(*nMin)) == math.MinInt8, int8(math.Round(*nMax)) == math.MaxInt8 + return "int8", minRounded == float64(math.MinInt8), maxRounded == float64(math.MaxInt8) } } func adjustForUnsignedBounds(nMin, nMax *float64) (string, bool, bool) { removeMin := nMin != nil && *nMin == 0.0 - switch { - case nMax == nil: + if nMax == nil { return "uint64", removeMin, false + } - case *nMax > math.MaxUint32: - return "uint64", removeMin, uint64(math.Round(*nMax)) == math.MaxUint64 + maxRounded := math.Round(*nMax) + + switch { + case maxRounded > float64(math.MaxUint32): + return "uint64", removeMin, maxRounded == float64(math.MaxUint64) - case *nMax > math.MaxUint16: - return "uint32", removeMin, uint32(math.Round(*nMax)) == math.MaxUint32 + case maxRounded > float64(math.MaxUint16): + return "uint32", removeMin, maxRounded == float64(math.MaxUint32) - case *nMax > math.MaxUint8: - return "uint16", removeMin, uint16(math.Round(*nMax)) == math.MaxUint16 + case maxRounded > float64(math.MaxUint8): + return "uint16", removeMin, maxRounded == float64(math.MaxUint16) default: - return "uint8", removeMin, uint8(math.Round(*nMax)) == math.MaxUint8 + return "uint8", removeMin, maxRounded == float64(math.MaxUint8) } }