Skip to content
Draft
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
5 changes: 2 additions & 3 deletions cmd/go-jsonschema-compiler/go-jsonschema-compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go/format"
"go/token"
"io"
"io/ioutil"
"os"

"github.com/sourcegraph/go-jsonschema/compiler"
Expand Down Expand Up @@ -107,7 +106,7 @@ func readSchema(filename string) (*jsonschema.Schema, error) {
// contents at path are different to data. This is to avoid triggering file
// watchers if there is no change.
func writeFileIfDifferent(path string, data []byte) error {
old, err := ioutil.ReadFile(path)
old, err := os.ReadFile(path)
if err == nil && bytes.Equal(old, data) {
// Skip writing
return nil
Expand All @@ -116,5 +115,5 @@ func writeFileIfDifferent(path string, data []byte) error {
// other errors can occur. In any case we just want to attempt doing a write
// and return that error.

return ioutil.WriteFile(path, data, 0666)
return os.WriteFile(path, data, 0666)
}
5 changes: 3 additions & 2 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"go/ast"
"go/format"
"go/token"
"io/fs"

Check failure on line 10 in compiler/compiler_test.go

View workflow job for this annotation

GitHub Actions / go-test

"io/fs" imported and not used
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -46,7 +47,7 @@
if entry.Mode().IsDir() {
continue
}
data, err := ioutil.ReadFile(filepath.Join(dir, entry.Name()))
data, err := os.ReadFile(filepath.Join(dir, entry.Name()))
if err != nil {
t.Fatalf("read %s: %s", entry.Name(), err)
}
Expand Down Expand Up @@ -78,7 +79,7 @@

const goFile = "want.go"
if *writeWant {
if err := ioutil.WriteFile(filepath.Join(dir, goFile), out, 0600); err != nil {
if err := os.WriteFile(filepath.Join(dir, goFile), out, 0600); err != nil {
t.Fatal(err)
}
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/generator_tagged_union_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/ast"
"go/token"
"reflect"
"slices"
"text/template"

"github.com/sourcegraph/go-jsonschema/jsonschema"
Expand Down Expand Up @@ -58,11 +59,8 @@ func (g *generator) emitTaggedUnionType(schema *jsonschema.Schema) ([]ast.Decl,
prop := (*s.Properties)[discriminantPropName]

var required bool
for _, req := range s.Required {
if req == discriminantPropName {
required = true
break
}
if slices.Contains(s.Required, discriminantPropName) {
required = true
}
if !required {
return nil, nil, fmt.Errorf("invalid oneOf schema for !go.taggedUnionType extension (discriminant property %q must be required)", discriminantPropName)
Expand Down
7 changes: 4 additions & 3 deletions compiler/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestParseSchema(t *testing.T) {
},
}
schemaE := &jsonschema.Schema{
ID: strptr("e"),
ID: new("e"),
Type: jsonschema.PrimitiveTypeList{jsonschema.ArrayType},
Items: &jsonschema.SchemaOrSchemaList{Schema: schemaC},
}
Expand All @@ -35,7 +35,7 @@ func TestParseSchema(t *testing.T) {
},
}
schemaRoot := &jsonschema.Schema{
Title: strptr("root"),
Title: new("root"),
Type: jsonschema.PrimitiveTypeList{jsonschema.ObjectType},
Properties: &map[string]*jsonschema.Schema{
"a": schemaA,
Expand Down Expand Up @@ -94,4 +94,5 @@ func TestParseSchema(t *testing.T) {
}
}

func strptr(s string) *string { return &s }
//go:fix inline
func strptr(s string) *string { return new(s) }
8 changes: 2 additions & 6 deletions compiler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package compiler

import (
"go/ast"
"slices"

"github.com/sourcegraph/go-jsonschema/jsonschema"
)

func isNullable(schema *jsonschema.Schema) bool {
for _, typ := range schema.Type {
if typ == jsonschema.NullType {
return true
}
}
return false
return slices.Contains(schema.Type, jsonschema.NullType)
}

func isTypeOrNull(schema *jsonschema.Schema, typ jsonschema.PrimitiveType) bool {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/sourcegraph/go-jsonschema

go 1.25.9
go 1.26.4
8 changes: 2 additions & 6 deletions jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"slices"
)

// Schema is a JSON Schema draft-07 document (as specified in
Expand Down Expand Up @@ -72,12 +73,7 @@ type Schema struct {
// schema.
func (s *Schema) IsRequiredProperty(propertyName string) bool {
// TODO(sqs): This ignores complexity like dependencies, allOf, etc.
for _, p := range s.Required {
if p == propertyName {
return true
}
}
return false
return slices.Contains(s.Required, propertyName)
}

var trueBytes = []byte("true")
Expand Down
12 changes: 7 additions & 5 deletions jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package jsonschema
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"

"reflect"
"testing"

"github.com/sourcegraph/go-jsonschema/internal/testutil"
)

func TestSample(t *testing.T) {
data, err := ioutil.ReadFile("../testdata/json-schema-draft-07-schema.json")
data, err := os.ReadFile("../testdata/json-schema-draft-07-schema.json")
if err != nil {
t.Fatal(err)
}
Expand All @@ -35,7 +36,7 @@ func TestSample(t *testing.T) {

func TestRaw(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
b, err := json.Marshal(Schema{Comment: strptr("c")})
b, err := json.Marshal(Schema{Comment: new("c")})
if err != nil {
t.Fatal(err)
}
Expand All @@ -49,10 +50,11 @@ func TestRaw(t *testing.T) {
if err := json.Unmarshal([]byte(input), &o); err != nil {
t.Fatal(err)
}
if want := (Schema{Comment: strptr("c"), Raw: (*json.RawMessage)(&input)}); !reflect.DeepEqual(o, want) {
if want := (Schema{Comment: new("c"), Raw: (*json.RawMessage)(&input)}); !reflect.DeepEqual(o, want) {
t.Errorf("got %+v, want %+v", o, want)
}
})
}

func strptr(s string) *string { return &s }
//go:fix inline
func strptr(s string) *string { return new(s) }
Loading