Skip to content

Commit

Permalink
Try with floats instead...
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Sep 5, 2024
1 parent d4317ed commit a4d5c33
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit a4d5c33

Please sign in to comment.