Skip to content

Commit

Permalink
fix more lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Sep 5, 2024
1 parent 0ac89e4 commit ec60cd3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
28 changes: 21 additions & 7 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ func PrimitiveTypeFromJSONSchemaType(

case schemas.TypeNameInteger:
t := PrimitiveType{"int"}

if minIntSize {
newType, removeMin, removeMax := getMinIntType(*minimum, *maximum, *exclusiveMinimum, *exclusiveMaximum)
t.Type = newType

if removeMin {
*minimum = nil
*exclusiveMaximum = nil
Expand Down Expand Up @@ -168,9 +170,9 @@ func PrimitiveTypeFromJSONSchemaType(
return nil, fmt.Errorf("%w %q", errUnknownJSONSchemaType, jsType)
}

// getMinIntType returns the smallest integer type that can represent the bounds, and if the bounds can be removed
// 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,
minimum, maximum *float64, exclusiveMinimum, exclusiveMaximum *any,
) (string, bool, bool) {
nMin, nMax, nExclusiveMin, nExclusiveMax := mathutils.NormalizeBounds(
minimum, maximum, exclusiveMinimum, exclusiveMaximum,
Expand All @@ -191,20 +193,28 @@ func getMinIntType(
return adjustForSignedBounds(nMin, nMax)
}

func adjustForSignedBounds(nMin *float64, nMax *float64) (string, bool, bool) {
const i64 = "int64"

func adjustForSignedBounds(nMin, nMax *float64) (string, bool, bool) {
switch {
case nMin == nil && nMax == nil:
return "int64", false, false
return i64, false, false

case nMin == nil:
return "int64", false, int64(*nMax) == math.MaxInt64
return i64, false, int64(*nMax) == math.MaxInt64

case nMax == nil:
return "int64", int64(*nMin) == math.MinInt64, false
return i64, int64(*nMin) == math.MinInt64, false

case *nMin < math.MinInt32 || *nMax > math.MaxInt32:
return "int64", int64(*nMin) == math.MinInt64, int64(*nMax) == math.MaxInt64
return i64, 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
}
Expand All @@ -216,12 +226,16 @@ func adjustForUnsignedBounds(nMin, nMax *float64) (string, bool, bool) {
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
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ func (g *schemaGenerator) generateEnumType(

if len(t.Type) == 1 {
var err error
enumType, err = codegen.PrimitiveTypeFromJSONSchemaType(
if enumType, err = codegen.PrimitiveTypeFromJSONSchemaType(
t.Type[0],
t.Format,
false,
Expand All @@ -792,9 +792,7 @@ func (g *schemaGenerator) generateEnumType(
&t.Maximum,
&t.ExclusiveMinimum,
&t.ExclusiveMaximum,
)

if err != nil {
); err != nil {
return nil, fmt.Errorf("invalid type %q: %w", t.Type[0], err)
}

Expand Down
23 changes: 19 additions & 4 deletions pkg/mathutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ func TestNormalizeBounds(t *testing.T) {
var anySmallerMax any = 190.0

var tr any = true

var fa any = false

t.Run("No exclusive bounds", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, nil, nil)
assert.NotNil(t, nMin)
assert.InEpsilon(t, anyMin, *nMin, 0.000001)
Expand All @@ -36,6 +39,8 @@ func TestNormalizeBounds(t *testing.T) {
})

t.Run("Less prohibitive exclusive bounds as numbers", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &anySmallerMin, &anyLargerMax)
assert.NotNil(t, nMin)
assert.InEpsilon(t, anyMin, *nMin, 0.000001)
Expand All @@ -46,6 +51,8 @@ func TestNormalizeBounds(t *testing.T) {
})

t.Run("More prohibitive exclusive bounds as numbers", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &anyLargerMin, &anySmallerMax)
assert.NotNil(t, nMin)
assert.InEpsilon(t, anyLargerMin, *nMin, 0.000001)
Expand All @@ -56,6 +63,8 @@ func TestNormalizeBounds(t *testing.T) {
})

t.Run("Only exclusive bounds as numbers", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(nil, nil, &anyLargerMin, &anySmallerMax)
assert.NotNil(t, nMin)
assert.InEpsilon(t, anyLargerMin, *nMin, 0.000001)
Expand All @@ -66,26 +75,32 @@ func TestNormalizeBounds(t *testing.T) {
})

t.Run("Exclusive bounds as bools", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &tr, &tr)
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.True(t, nMinExclusive)
assert.True(t, nMaxExclusive)
})

t.Run("Exclusive bounds as false bools", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(&anyMin, &anyMax, &fa, &fa)
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("No bounds", func(t *testing.T) {
t.Parallel()

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(nil, nil, nil, nil)
assert.Nil(t, nMin)
assert.Nil(t, nMax)
Expand Down
2 changes: 2 additions & 0 deletions pkg/schemas/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,12 @@ func fileExists(fileName string) bool {

func toExtensionSet(items []string) map[string]bool {
set := make(map[string]bool, len(items))

for _, item := range items {
if !strings.HasPrefix(item, ".") {
item = "." + item
}

set[item] = true
}

Expand Down
2 changes: 2 additions & 0 deletions tests/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ func TestExtraImportsYAML(t *testing.T) {

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

cfg := basicConfig
cfg.MinSizedInts = true

testExamples(t, cfg, "./data/minSizedInts")
}

Expand Down
8 changes: 8 additions & 0 deletions tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestRequiredFields(t *testing.T) {

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

testCases := []struct {
desc string
data string
Expand All @@ -171,6 +172,7 @@ func TestPattern(t *testing.T) {
wantErr: errors.New("field ^0x[0-9a-f]{10}$ pattern match: must match MyString"),
},
}

for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
t.Parallel()
Expand All @@ -186,6 +188,7 @@ func TestPattern(t *testing.T) {

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

testCases := []struct {
desc string
data string
Expand All @@ -205,6 +208,7 @@ func TestPrimitiveDefs(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

prim := testPrimitiveDefs.PrimitiveDefs{}

err := json.Unmarshal([]byte(tC.data), &prim)
Expand Down Expand Up @@ -251,6 +255,7 @@ func TestMultipleOf(t *testing.T) {

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

testCases := []struct {
desc string
data string
Expand Down Expand Up @@ -290,6 +295,7 @@ func TestMaximum(t *testing.T) {

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

testCases := []struct {
desc string
data string
Expand Down Expand Up @@ -329,6 +335,7 @@ func TestMinimum(t *testing.T) {

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

testCases := []struct {
desc string
data string
Expand Down Expand Up @@ -403,6 +410,7 @@ func TestExclusiveMinimum(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

mo := testExclusiveMinimum.ExclusiveMinimum{}

err := json.Unmarshal([]byte(tC.data), &mo)
Expand Down

0 comments on commit ec60cd3

Please sign in to comment.