Skip to content

Commit

Permalink
WIP: using go-json
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Jan 9, 2024
1 parent 096f80c commit 73720f1
Show file tree
Hide file tree
Showing 45 changed files with 70 additions and 50 deletions.
9 changes: 1 addition & 8 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package spec

import (
"sync"

jsoniter "github.com/json-iterator/go"
)

// ResolutionCache a cache for resolving urls
Expand Down Expand Up @@ -73,19 +71,14 @@ var (
resCache *simpleCache
onceCache sync.Once

_ ResolutionCache = &simpleCache{}
json jsoniter.API
_ ResolutionCache = &simpleCache{}
)

// initResolutionCache initializes the URI resolution cache. To be wrapped in a sync.Once.Do call.
func initResolutionCache() {
resCache = defaultResolutionCache()
}

func init() {
json = jsoniter.ConfigFastest
}

func defaultResolutionCache() *simpleCache {
return &simpleCache{store: map[string]interface{}{
"http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
Expand Down
1 change: 1 addition & 0 deletions circular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions contact_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spec

import (
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// ContactInfo contact information for the exposed API.
Expand Down
1 change: 1 addition & 0 deletions contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
13 changes: 7 additions & 6 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package spec

import (
stdjson "encoding/json"
"fmt"

json "github.com/goccy/go-json"
)

// ExpandOptions provides options for the spec expander.
Expand All @@ -28,11 +29,11 @@ import (
//
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
type ExpandOptions struct {
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
ContinueOnError bool // continue expanding even after and error is found
PathLoader func(string) (stdjson.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
ContinueOnError bool // continue expanding even after and error is found
PathLoader func(string) (json.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
}

func optionsOrDefault(opts *ExpandOptions) *ExpandOptions {
Expand Down
4 changes: 2 additions & 2 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package spec

import (
stdjson "encoding/json"
"io"
"log"
"net/http"
Expand All @@ -24,6 +23,7 @@ import (
"path/filepath"
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -38,7 +38,7 @@ const (

var (
// PetStoreJSONMessage json raw message for Petstore20
PetStoreJSONMessage = stdjson.RawMessage([]byte(PetStore20))
PetStoreJSONMessage = json.RawMessage([]byte(PetStore20))
specs = filepath.Join("fixtures", "specs")
)

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require (
github.com/go-openapi/jsonpointer v0.20.2
github.com/go-openapi/jsonreference v0.20.4
github.com/go-openapi/swag v0.22.6
github.com/json-iterator/go v1.1.12
github.com/goccy/go-json v0.10.2
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -13,8 +13,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

Expand Down
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
Expand All @@ -7,25 +6,17 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/swag v0.22.6 h1:dnqg1XfHXL9aBxSbktBqFR5CxVyVI+7fYWhAf1JOeTw=
github.com/go-openapi/swag v0.22.6/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

const (
Expand Down
1 change: 1 addition & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
9 changes: 4 additions & 5 deletions helpers_spec_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
package spec_test

import (
"encoding/json"
stdjson "encoding/json"
"fmt"
"regexp"
"strings"
"testing"

"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)
testLoader func(string) (stdjson.RawMessage, error)
testLoader func(string) (json.RawMessage, error)
)

func init() {
// mimics what the go-openapi/load does
testLoader = func(path string) (stdjson.RawMessage, error) {
testLoader = func(path string) (json.RawMessage, error) {
if swag.YAMLMatcher(path) {
return swag.YAMLDoc(path)
}
data, err := swag.LoadFromFileOrHTTP(path)
if err != nil {
return nil, err
}
return stdjson.RawMessage(data), nil
return json.RawMessage(data), nil
}
}

Expand Down
6 changes: 3 additions & 3 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package spec

import (
stdjson "encoding/json"
"fmt"
"regexp"
"strings"
"testing"

"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)

func jsonDoc(path string) (stdjson.RawMessage, error) {
func jsonDoc(path string) (json.RawMessage, error) {
data, err := swag.LoadFromFileOrHTTP(path)
if err != nil {
return nil, err
}
return stdjson.RawMessage(data), nil
return json.RawMessage(data), nil
}

func docAndOpts(t testing.TB, fixturePath string) ([]byte, *ExpandOptions) {
Expand Down
1 change: 1 addition & 0 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// Extensions vendor specific extensions
Expand Down
1 change: 1 addition & 0 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions items.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

const (
Expand Down
1 change: 1 addition & 0 deletions items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions license.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spec

import (
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// License information for the exposed API.
Expand Down
1 change: 1 addition & 0 deletions license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

func init() {
Expand Down
1 change: 1 addition & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/gob"
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// QueryParam creates a query parameter
Expand Down
1 change: 1 addition & 0 deletions parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// PathItemProps the path item specific properties
Expand Down
1 change: 1 addition & 0 deletions path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
5 changes: 2 additions & 3 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"fmt"
"strings"

stdjson "encoding/json"

"github.com/go-openapi/swag"
json "github.com/goccy/go-json"
)

// Paths holds the relative paths to the individual endpoints.
Expand All @@ -47,7 +46,7 @@ func (p Paths) JSONLookup(token string) (interface{}, error) {

// UnmarshalJSON hydrates this items instance with the data from JSON
func (p *Paths) UnmarshalJSON(data []byte) error {
var res map[string]stdjson.RawMessage
var res map[string]json.RawMessage
if err := json.Unmarshal(data, &res); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spec
import (
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 2 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"reflect"
"sort"

json "github.com/goccy/go-json"
)

// OrderSchemaItem holds a named schema (e.g. from a property of an object)
Expand Down
1 change: 1 addition & 0 deletions ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

"github.com/go-openapi/jsonreference"
json "github.com/goccy/go-json"
)

// Refable is a struct for things that accept a $ref property
Expand Down
1 change: 1 addition & 0 deletions ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/gob"
"testing"

json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
1 change: 1 addition & 0 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/go-openapi/jsonpointer"
json "github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
Loading

0 comments on commit 73720f1

Please sign in to comment.