Skip to content

Commit

Permalink
merege go mod conflict, fix lint and bugs it found, fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Sep 5, 2024
2 parents cba7348 + f9eb658 commit 0ac89e4
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 417 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
adr-tools 3.0.0
golang 1.23.0
golangci-lint 1.60.1
golangci-lint 1.60.3
goreleaser 1.25.1
hadolint 2.12.0
markdownlint-cli2 0.13.0
shellcheck 0.10.0
shfmt 3.8.0
shfmt 3.9.0
yamllint 1.35.1
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/sanity-io/litter v1.5.5
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0Bq
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk=
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
Expand Down
85 changes: 52 additions & 33 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func PrimitiveTypeFromJSONSchemaType(
format string,
pointer,
minIntSize bool,
minimum *float64,
maximum *float64,
exclusiveMinimum *any,
exclusiveMaximum *any,
minimum **float64,
maximum **float64,
exclusiveMinimum **any,
exclusiveMaximum **any,
) (Type, error) {
var t Type

Expand Down Expand Up @@ -131,10 +131,16 @@ func PrimitiveTypeFromJSONSchemaType(
case schemas.TypeNameInteger:
t := PrimitiveType{"int"}
if minIntSize {
newType, removeBoudns := getMinIntType(minimum, maximum, exclusiveMinimum, exclusiveMaximum)
newType, removeMin, removeMax := getMinIntType(*minimum, *maximum, *exclusiveMinimum, *exclusiveMaximum)
t.Type = newType
if removeBoudns {
if removeMin {
*minimum = nil
*exclusiveMaximum = nil
}

if removeMax {
*maximum = nil
*exclusiveMinimum = nil
}
}

Expand Down Expand Up @@ -163,47 +169,60 @@ func PrimitiveTypeFromJSONSchemaType(
}

// getMinIntType returns the smallest integer type that can represent the bounds, and if the bounds can be removed
func getMinIntType(minimum *float64, maximum *float64, exclusiveMinimum *any, exclusiveMaximum *any) (string, bool) {
nMin, nMax, nExclusiveMin, nExclusiveMax := mathutils.NormalizeBounds(minimum, maximum, exclusiveMinimum, exclusiveMaximum)
func getMinIntType(
minimum *float64, maximum *float64, exclusiveMinimum *any, exclusiveMaximum *any,
) (string, bool, bool) {
nMin, nMax, nExclusiveMin, nExclusiveMax := mathutils.NormalizeBounds(
minimum, maximum, exclusiveMinimum, exclusiveMaximum,
)

if nExclusiveMin && nMin != nil {
*nMin = *nMin - 1.0
*nMin += 1.0
}

if nExclusiveMax && nMax != nil {
*nMax = *nMax + 1.0
*nMax -= 1.0
}

if nMin != nil && *nMin >= 0 {
return adjustForUnsignedBounds(nMax)
return adjustForUnsignedBounds(nMin, nMax)
}

return adjustForSignedBounds(nMin, nMax)
}

func adjustForSignedBounds(nMin *float64, nMax *float64) (string, bool) {
if nMin == nil || nMax == nil {
return "int64", true
} else if *nMin < math.MinInt32 || *nMax > math.MaxInt32 {
return "int64", *nMin == math.MinInt64 && *nMax == math.MaxInt64
} else if *nMin < math.MinInt16 || *nMax > math.MaxInt16 {
return "int32", *nMin == math.MinInt32 && *nMax == math.MaxInt32
} else if *nMin < math.MinInt8 || *nMax > math.MaxInt8 {
return "int16", *nMin == math.MinInt16 && *nMax == math.MaxInt16
func adjustForSignedBounds(nMin *float64, nMax *float64) (string, bool, bool) {
switch {
case nMin == nil && nMax == nil:
return "int64", false, false
case nMin == nil:
return "int64", false, int64(*nMax) == math.MaxInt64
case nMax == nil:
return "int64", int64(*nMin) == math.MinInt64, false
case *nMin < math.MinInt32 || *nMax > math.MaxInt32:
return "int64", int64(*nMin) == math.MinInt64, int64(*nMax) == math.MaxInt64
case *nMin < math.MinInt16 || *nMax > math.MaxInt16:
return "int32", int32(*nMin) == math.MinInt32, int32(*nMax) == math.MaxInt32
case *nMin < math.MinInt8 || *nMax > math.MaxInt8:
return "int16", int16(*nMin) == math.MinInt16, int16(*nMax) == math.MaxInt16
default:
return "int8", int8(*nMin) == math.MinInt8, int8(*nMax) == math.MaxInt8
}

return "int8", *nMin == math.MinInt8 && *nMax == math.MaxInt8
}

func adjustForUnsignedBounds(nMax *float64) (string, bool) {
if nMax == nil {
return "uint64", true
} else if *nMax > math.MaxUint32 {
return "uint64", *nMax == math.MaxUint64
} else if *nMax > math.MaxUint16 {
return "uint32", *nMax == math.MaxUint32
} else if *nMax > math.MaxUint8 {
return "uint16", *nMax == math.MaxUint16
func adjustForUnsignedBounds(nMin, nMax *float64) (string, bool, bool) {
removeMin := nMin != nil && *nMin == 0.0

switch {
case nMax == nil:
return "uint64", removeMin, false
case *nMax > math.MaxUint32:
return "uint64", removeMin, uint64(*nMax) == math.MaxUint64
case *nMax > math.MaxUint16:
return "uint32", removeMin, uint32(*nMax) == math.MaxUint32
case *nMax > math.MaxUint8:
return "uint16", removeMin, uint16(*nMax) == math.MaxUint16
default:
return "uint8", removeMin, uint8(*nMax) == math.MaxUint8
}

return "uint8", *nMax == math.MaxUint8
}
25 changes: 13 additions & 12 deletions pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ func (g *schemaGenerator) generateType(
t.Format,
typeShouldBePointer,
g.config.MinSizedInts,
t.Minimum,
t.Maximum,
t.ExclusiveMinimum,
t.ExclusiveMaximum,
&t.Minimum,
&t.Maximum,
&t.ExclusiveMinimum,
&t.ExclusiveMaximum,
)
if err != nil {
return nil, fmt.Errorf("invalid type %q: %w", t.Type[typeIndex], err)
Expand Down Expand Up @@ -729,10 +729,10 @@ func (g *schemaGenerator) generateTypeInline(
t.Format,
typeShouldBePointer,
g.config.MinSizedInts,
t.Minimum,
t.Maximum,
t.ExclusiveMinimum,
t.ExclusiveMaximum,
&t.Minimum,
&t.Maximum,
&t.ExclusiveMinimum,
&t.ExclusiveMaximum,
)
if err != nil {
return nil, fmt.Errorf("invalid type %q: %w", t.Type[typeIndex], err)
Expand Down Expand Up @@ -788,11 +788,12 @@ func (g *schemaGenerator) generateEnumType(
t.Format,
false,
g.config.MinSizedInts,
t.Minimum,
t.Maximum,
t.ExclusiveMinimum,
t.ExclusiveMaximum,
&t.Minimum,
&t.Maximum,
&t.ExclusiveMinimum,
&t.ExclusiveMaximum,
)

if err != nil {
return nil, fmt.Errorf("invalid type %q: %w", t.Type[0], err)
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,26 +381,3 @@ func getPlainName(fieldName string) string {

return fmt.Sprintf("%s.%s", varNamePlainStruct, fieldName)
}

func SafePrint(pointers ...interface{}) string {
var result string
for _, p := range pointers {
switch v := p.(type) {
case *any:
if v == nil || *v == nil {
result += "nil "
} else {
result += fmt.Sprintf("%v ", *v)
}
case *float64:
if v == nil {
result += "nil "
} else {
result += fmt.Sprintf("%v ", *v)
}
default:
result += fmt.Sprintf("%v ", v)
}
}
return result
}
11 changes: 6 additions & 5 deletions pkg/mathutils/utils.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package mathutils

// NormalizeBounds is a public function that normalizes the given bounds and exclusivity flags.
func NormalizeBounds(minimum, maximum *float64, exclusiveMinimum, exclusiveMaximum *any) (*float64, *float64, bool, bool) {
func NormalizeBounds(
minimum, maximum *float64, exclusiveMinimum, exclusiveMaximum *any,
) (*float64, *float64, bool, bool) {
var minBound, maxBound *float64

var minExclusive, maxExclusive bool

// Handle exclusiveMinimum
if exclusiveMinimum != nil {
switch v := (*exclusiveMinimum).(type) {
case bool:
minExclusive = v
minBound = minimum

case float64:
if minimum == nil || v > *minimum {
minBound = &v
Expand All @@ -25,18 +28,17 @@ func NormalizeBounds(minimum, maximum *float64, exclusiveMinimum, exclusiveMaxim
minExclusive = false
}

// Handle minimum if exclusiveMinimum was not set
if minimum != nil && minBound == nil {
minBound = minimum
minExclusive = false
}

// Handle exclusiveMaximum
if exclusiveMaximum != nil {
switch v := (*exclusiveMaximum).(type) {
case bool:
maxExclusive = v
maxBound = maximum

case float64:
if maximum == nil || v < *maximum {
maxBound = &v
Expand All @@ -51,7 +53,6 @@ func NormalizeBounds(minimum, maximum *float64, exclusiveMinimum, exclusiveMaxim
maxExclusive = false
}

// Handle maximum if exclusiveMaximum was not set
if maximum != nil && maxBound == nil {
maxBound = maximum
maxExclusive = false
Expand Down
24 changes: 16 additions & 8 deletions pkg/mathutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,58 @@ import (
)

func TestNormalizeBounds(t *testing.T) {
t.Parallel()

anyMin := 100.0
anyMax := 200.0

var anySmallerMin any = 90.0

var anyLargerMax any = 210.0

var anyLargerMin any = 110.0

var anySmallerMax any = 190.0

var tr any = true
var fa any = false

t.Run("No exclusive bounds", func(t *testing.T) {
nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, nil, nil)
assert.NotNil(t, nMin)
assert.Equal(t, anyMin, *nMin)
assert.InEpsilon(t, anyMin, *nMin, 0.000001)
assert.NotNil(t, nMax)
assert.Equal(t, anyMax, *nMax)
assert.InEpsilon(t, anyMax, *nMax, 0.000001)
assert.False(t, nMinExclusive)
assert.False(t, nMaxExclusive)
})

t.Run("Less prohibitive exclusive bounds as numbers", func(t *testing.T) {
nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &anySmallerMin, &anyLargerMax)
assert.NotNil(t, nMin)
assert.Equal(t, anyMin, *nMin)
assert.InEpsilon(t, anyMin, *nMin, 0.000001)
assert.NotNil(t, nMax)
assert.Equal(t, anyMax, *nMax)
assert.InEpsilon(t, anyMax, *nMax, 0.000001)
assert.False(t, nMinExclusive)
assert.False(t, nMaxExclusive)
})

t.Run("More prohibitive exclusive bounds as numbers", func(t *testing.T) {
nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &anyLargerMin, &anySmallerMax)
assert.NotNil(t, nMin)
assert.Equal(t, anyLargerMin, *nMin)
assert.InEpsilon(t, anyLargerMin, *nMin, 0.000001)
assert.NotNil(t, nMax)
assert.Equal(t, anySmallerMax, *nMax)
assert.InEpsilon(t, anySmallerMax, *nMax, 0.000001)
assert.True(t, nMinExclusive)
assert.True(t, nMaxExclusive)
})

t.Run("Only exclusive bounds as numbers", func(t *testing.T) {
nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(nil, nil, &anyLargerMin, &anySmallerMax)
assert.NotNil(t, nMin)
assert.Equal(t, anyLargerMin, *nMin)
assert.InEpsilon(t, anyLargerMin, *nMin, 0.000001)
assert.NotNil(t, nMax)
assert.Equal(t, anySmallerMax, *nMax)
assert.InEpsilon(t, anySmallerMax, *nMax, 0.000001)
assert.True(t, nMinExclusive)
assert.True(t, nMaxExclusive)
})
Expand Down
Loading

0 comments on commit 0ac89e4

Please sign in to comment.