Skip to content

Commit

Permalink
chore: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Apr 20, 2024
1 parent f648635 commit 8228fbb
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 25 deletions.
9 changes: 6 additions & 3 deletions .rules/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

# Options for analysis running.
run:
go: "1.22"
timeout: 5m
skip-files:
exclude-files:
- .*\.gen\.go$
- tests/data/.*
modules-download-mode: readonly
Expand All @@ -27,6 +28,7 @@ linters:
- godox
- ireturn
- nestif
- perfsprint
- revive
- varnamelen
# unsupported
Expand Down Expand Up @@ -76,12 +78,11 @@ linters-settings:
capital: true
period: true
gofumpt:
lang-version: "1.22"
extra-rules: true
goimports:
local-prefixes: github.com/atombender
govet:
check-shadowing: true
shadow: true
grouper:
const-require-single-const: true
const-require-grouping: false
Expand Down Expand Up @@ -460,6 +461,8 @@ issues:
# Default: []
exclude:
- "local replacement are not allowed: github.com/atombender/go-jsonschema/tests/data"
- "exporting a pointer for the loop variable i"
- "G601: Implicit memory aliasing in for loop."
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
Expand Down
1 change: 1 addition & 0 deletions internal/x/text/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func splitIdentifierByCaseAndSeparators(s string) []string {
if i > j {
result = append(result, runes[j:i])
}

j = i
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (e *Emitter) Newline() {

func (e *Emitter) checkIndent() {
if e.start {
for i := uint(0); i < e.indent; i++ {
for range e.indent {
e.sb.WriteRune('\t')
}

Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func (s *StructType) Generate(out *Emitter) {

f.Generate(out)
out.Newline()

i++
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package codegen

import (
"errors"
"fmt"

"github.com/atombender/go-jsonschema/pkg/schemas"
)

var (
errUnexpectedType = fmt.Errorf("unexpected type")
errUnknownJSONSchemaType = fmt.Errorf("unknown JSON Schema type")
errUnexpectedType = errors.New("unexpected type")
errUnknownJSONSchemaType = errors.New("unknown JSON Schema type")
)

func WrapTypeInPointer(t Type) Type {
Expand Down
1 change: 1 addition & 0 deletions pkg/generator/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (o *output) uniqueTypeName(name string) string {

return suffixed
}

count++
}
}
14 changes: 9 additions & 5 deletions pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (g *schemaGenerator) generateReferencedType(ref string) (codegen.Type, erro
} else {
def = (*schemas.Type)(schema.ObjectAsType)
defName = g.getRootTypeName(schema, fileName)

if len(def.Type) == 0 {
// Minor hack to make definitions default to being objects.
def.Type = schemas.TypeList{schemas.TypeNameObject}
Expand Down Expand Up @@ -181,7 +182,6 @@ func (g *schemaGenerator) generateReferencedType(ref string) (codegen.Type, erro
var imp *codegen.Import

for _, i := range g.output.file.Package.Imports {
i := i
if i.Name == sg.output.file.Package.Name() && i.QualifiedName == sg.output.file.Package.QualifiedName {
imp = &i

Expand Down Expand Up @@ -267,8 +267,6 @@ func (g *schemaGenerator) generateDeclaredType(
}

for _, formatter := range g.formatters {
formatter := formatter

formatter.addImport(g.output.file)

g.output.file.Package.AddDecl(&codegen.Method{
Expand Down Expand Up @@ -691,22 +689,28 @@ func (g *schemaGenerator) generateEnumType(
}

var primitiveType string

for _, v := range t.Enum {
var valueType string

if v == nil {
valueType = interfaceTypeName
} else {
switch v.(type) {
case string:
valueType = "string"

case float64:
valueType = "float64"

case bool:
valueType = "bool"

default:
return nil, fmt.Errorf("%w %v", errEnumNonPrimitiveVal, v)
}
}

if primitiveType == "" {
primitiveType = valueType
} else if primitiveType != valueType {
Expand All @@ -715,9 +719,11 @@ func (g *schemaGenerator) generateEnumType(
break
}
}

if primitiveType == interfaceTypeName {
wrapInStruct = true
}

enumType = codegen.PrimitiveType{Type: primitiveType}
}

Expand Down Expand Up @@ -754,8 +760,6 @@ func (g *schemaGenerator) generateEnumType(
g.output.file.Package.AddImport("reflect", "")

for _, formatter := range g.formatters {
formatter := formatter

formatter.addImport(g.output.file)

if wrapInStruct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func (v *nullTypeValidator) generate(out *codegen.Emitter) {
value := fmt.Sprintf("%s.%s", varNamePlainStruct, v.fieldName)
fieldName := v.jsonName

var indexes []string
indexes := make([]string, v.arrayDepth)

for i := 0; i < v.arrayDepth; i++ {
for i := range v.arrayDepth {
index := fmt.Sprintf("i%d", i)
indexes = append(indexes, index)
indexes[i] = index
out.Printlnf(`for %s := range %s {`, index, value)
value += fmt.Sprintf("[%s]", index)
fieldName += "[%d]"
Expand All @@ -85,7 +85,7 @@ func (v *nullTypeValidator) generate(out *codegen.Emitter) {
out.Indent(-1)
out.Printlnf("}")

for i := 0; i < v.arrayDepth; i++ {
for range v.arrayDepth {
out.Indent(-1)
out.Printlnf("}")
}
Expand Down
5 changes: 5 additions & 0 deletions tests/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,16 @@ func testExampleFile(t *testing.T, cfg generator.Config, fileName string) {
if !os.IsNotExist(err) {
t.Fatal(err)
}

goldenData = source

t.Log("File does not exist; creating it")

if err = os.WriteFile(goldenFileName, goldenData, 0o655); err != nil {
t.Fatal(err)
}
}

if diff, ok := diffStrings(t, string(goldenData), string(source)); !ok {
t.Fatalf("Contents different (left is expected, right is actual):\n%s", *diff)
}
Expand All @@ -238,6 +242,7 @@ func testFailingExampleFile(t *testing.T, cfg generator.Config, fileName string)
if err != nil {
t.Fatal(err)
}

if err := g.DoFile(fileName); err == nil {
t.Fatal("Expected test to fail")
}
Expand Down
2 changes: 0 additions & 2 deletions tests/serializable_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ func TestSerializableDateUnmarshalJSONReturnsErrorForInvalidString(t *testing.T)
}

for _, testCase := range testCases {
testCase := testCase

t.Run(fmt.Sprintf("Given '%s' expected UnmarshalJSON to return an error", testCase), func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 0 additions & 2 deletions tests/serializable_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func TestSerializableTimeUnmarshalJSONReturnsErrorForInvalidString(t *testing.T)
}

for _, testCase := range testCases {
testCase := testCase

t.Run(fmt.Sprintf("Given '%s' expected UnmarshalJSON to return an error", testCase), func(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 0 additions & 6 deletions tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func TestMaxStringLength(t *testing.T) {
},
}
for _, tC := range testCases {
tC := tC

t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -100,8 +98,6 @@ func TestMinStringLength(t *testing.T) {
},
}
for _, tC := range testCases {
tC := tC

t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -139,8 +135,6 @@ func TestRequiredFields(t *testing.T) {
},
}
for _, tC := range testCases {
tC := tC

t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 8228fbb

Please sign in to comment.