Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: address some golangci-lint findings #126

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ func (c *Compiler) compileMap(r *resource, stack []schemaRef, sref schemaRef, re
switch jsonType(item) {
case "object", "array":
allPrimitives = false
break
}
}
s.enumError = "enum failed"
Expand Down Expand Up @@ -681,7 +680,7 @@ func (c *Compiler) compileMap(r *resource, stack []schemaRef, sref schemaRef, re
if format, ok := c.Formats[s.Format]; ok {
s.format = format
} else {
s.format, _ = Formats[s.Format]
s.format = Formats[s.Format]
}
}
}
Expand All @@ -708,15 +707,15 @@ func (c *Compiler) compileMap(r *resource, stack []schemaRef, sref schemaRef, re
if decoder, ok := c.Decoders[s.ContentEncoding]; ok {
s.decoder = decoder
} else {
s.decoder, _ = Decoders[s.ContentEncoding]
s.decoder = Decoders[s.ContentEncoding]
}
}
if mediaType, ok := m["contentMediaType"]; ok {
s.ContentMediaType = mediaType.(string)
if mediaType, ok := c.MediaTypes[s.ContentMediaType]; ok {
s.mediaType = mediaType
} else {
s.mediaType, _ = MediaTypes[s.ContentMediaType]
s.mediaType = MediaTypes[s.ContentMediaType]
}
if s.ContentSchema, err = loadSchema("contentSchema", stack); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"strings"

Expand All @@ -20,7 +20,7 @@ func Example() {
log.Fatalf("%#v", err)
}

data, err := ioutil.ReadFile("testdata/person.json")
data, err := os.ReadFile("testdata/person.json")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func Example_userDefinedLoader() {
if !ok {
return nil, fmt.Errorf("%q not found", url)
}
return ioutil.NopCloser(strings.NewReader(schema)), nil
return io.NopCloser(strings.NewReader(schema)), nil
}

sch, err := jsonschema.Compile("map:///main.json")
Expand Down
4 changes: 2 additions & 2 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func isDuration(v interface{}) bool {
return len(s) == 0 // P_W
}
if len(units) > 0 {
if strings.Index("YMD", units) == -1 {
if !strings.Contains("YMD", units) {
return false
}
if len(s) == 0 {
Expand All @@ -232,7 +232,7 @@ func isDuration(v interface{}) bool {
}
s = s[1:]
units, ok = parseUnits()
return ok && len(s) == 0 && len(units) > 0 && strings.Index("HMS", units) != -1
return ok && len(s) == 0 && len(units) > 0 && strings.Contains("HMS", units)
}

// isPeriod tells whether given string is a valid period format
Expand Down
38 changes: 13 additions & 25 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -178,17 +177,6 @@ func TestExtra(t *testing.T) {
})
}

type testGroup struct {
Description string
Schema json.RawMessage
Tests []struct {
Description string
Data json.RawMessage
Valid bool
Skip *string
}
}

func TestMain(m *testing.M) {
server1 := &http.Server{Addr: "localhost:1234", Handler: http.FileServer(http.Dir("testdata/JSON-Schema-Test-Suite/remotes"))}
go func() {
Expand All @@ -206,26 +194,26 @@ func TestMain(m *testing.M) {
}

func testFolder(t *testing.T, folder string, draft *jsonschema.Draft) {
fis, err := ioutil.ReadDir(folder)
des, err := os.ReadDir(folder)
if err != nil {
t.Fatal(err)
}
for _, fi := range fis {
if fi.IsDir() {
t.Run(fi.Name(), func(t *testing.T) {
testFolder(t, path.Join(folder, fi.Name()), draft)
for _, de := range des {
if de.IsDir() {
t.Run(de.Name(), func(t *testing.T) {
testFolder(t, path.Join(folder, de.Name()), draft)
})
continue
}
if path.Ext(fi.Name()) != ".json" {
if path.Ext(de.Name()) != ".json" {
continue
}
t.Run(fi.Name(), func(t *testing.T) {
t.Run(de.Name(), func(t *testing.T) {
skip := skipTests[t.Name()]
if skip != nil && len(skip) == 0 {
t.Skip()
}
f, err := os.Open(path.Join(folder, fi.Name()))
f, err := os.Open(path.Join(folder, de.Name()))
if err != nil {
t.Fatal(err)
}
Expand All @@ -252,7 +240,7 @@ func testFolder(t *testing.T, folder string, draft *jsonschema.Draft) {
}
c := jsonschema.NewCompiler()
c.Draft = draft
if strings.Index(folder, "optional") != -1 {
if strings.Contains(folder, "optional") {
c.AssertFormat = true
c.AssertContent = true
}
Expand Down Expand Up @@ -357,7 +345,7 @@ func TestInvalidSchema(t *testing.T) {
Schema json.RawMessage
Fragment string
}
data, err := ioutil.ReadFile("testdata/invalid_schemas.json")
data, err := os.ReadFile("testdata/invalid_schemas.json")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -653,9 +641,9 @@ func TestCompiler_LoadURL(t *testing.T) {
c.LoadURL = func(s string) (io.ReadCloser, error) {
switch s {
case "map:///base.json":
return ioutil.NopCloser(strings.NewReader(base)), nil
return io.NopCloser(strings.NewReader(base)), nil
case "map:///schema.json":
return ioutil.NopCloser(strings.NewReader(schema)), nil
return io.NopCloser(strings.NewReader(schema)), nil
default:
return nil, errors.New("unsupported schema")
}
Expand Down Expand Up @@ -688,7 +676,7 @@ func TestSchemaDraftFeild(t *testing.T) {
if !ok {
return nil, fmt.Errorf("%q not found", url)
}
return ioutil.NopCloser(strings.NewReader(schema)), nil
return io.NopCloser(strings.NewReader(schema)), nil
}

sch, err := jsonschema.Compile("map:///main.json")
Expand Down