Skip to content

Commit

Permalink
fix: cache schema building via schema proxy to ensure speed and memor…
Browse files Browse the repository at this point in the history
…y efficiency of dealing with large docs
  • Loading branch information
TristanSpeakEasy committed Sep 25, 2024
1 parent 8e9d97c commit 2874552
Show file tree
Hide file tree
Showing 162 changed files with 74,530 additions and 1,650 deletions.
10 changes: 3 additions & 7 deletions datamodel/high/base/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ package base
import (
"context"
"fmt"
"testing"

lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)

func TestNewContact(t *testing.T) {

var cNode yaml.Node

yml := `name: pizza
Expand All @@ -34,11 +34,9 @@ email: buckaroo@pb33f.io`
assert.Equal(t, "https://pb33f.io", highContact.URL)
assert.Equal(t, "buckaroo@pb33f.io", highContact.Email)
assert.Equal(t, 1, highContact.GoLow().Name.KeyNode.Line)

}

func ExampleNewContact() {

// define a Contact using yaml (or JSON, it doesn't matter)
yml := `name: Buckaroo
url: https://pb33f.io
Expand All @@ -59,7 +57,6 @@ email: buckaroo@pb33f.io`
}

func TestContact_MarshalYAML(t *testing.T) {

yml := `name: Buckaroo
url: https://pb33f.io
email: buckaroo@pb33f.io
Expand All @@ -71,13 +68,12 @@ email: buckaroo@pb33f.io
// build low
var lowContact lowbase.Contact
_ = lowmodel.BuildModel(cNode.Content[0], &lowContact)
_ = lowContact.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowContact.Build(context.Background(), nil, cNode.Content[0], nil)

// build high
highContact := NewContact(&lowContact)

// marshal high back to yaml, should be the same as the original, in same order.
bytes, _ := highContact.Render()
assert.Equal(t, yml, string(bytes))

}
8 changes: 4 additions & 4 deletions datamodel/high/base/dynamic_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ func TestDynamicValue_MarshalYAMLInline(t *testing.T) {
_ = yaml.Unmarshal([]byte(ymlSchema), &node)

lowProxy := new(lowbase.SchemaProxy)
err := lowProxy.Build(context.Background(), nil, node.Content[0], idx)
_, err := lowProxy.Build(context.Background(), nil, node.Content[0], idx)
assert.NoError(t, err)

lowRef := low.NodeReference[*lowbase.SchemaProxy]{
Value: lowProxy,
}

sp := NewSchemaProxy(&lowRef)
sp := NewSchemaProxy(&lowRef, idx)

rend, _ := sp.MarshalYAMLInline()

Expand Down Expand Up @@ -157,14 +157,14 @@ func TestDynamicValue_MarshalYAMLInline_Error(t *testing.T) {
_ = yaml.Unmarshal([]byte(ymlSchema), &node)

lowProxy := new(lowbase.SchemaProxy)
err := lowProxy.Build(context.Background(), nil, node.Content[0], idx)
_, err := lowProxy.Build(context.Background(), nil, node.Content[0], idx)
assert.NoError(t, err)

lowRef := low.NodeReference[*lowbase.SchemaProxy]{
Value: lowProxy,
}

sp := NewSchemaProxy(&lowRef)
sp := NewSchemaProxy(&lowRef, idx)

rend, er := sp.MarshalYAMLInline()
assert.Nil(t, rend)
Expand Down
7 changes: 4 additions & 3 deletions datamodel/high/base/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/high"
"github.com/pb33f/libopenapi/datamodel/low"
lowBase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"gopkg.in/yaml.v3"
)
Expand All @@ -26,7 +27,7 @@ type Example struct {
}

// NewExample will create a new instance of an Example, using a low-level Example.
func NewExample(example *lowBase.Example) *Example {
func NewExample(example *lowBase.Example, idx *index.SpecIndex) *Example {
e := new(Example)
e.low = example
e.Summary = example.Summary.Value
Expand Down Expand Up @@ -69,6 +70,6 @@ func (e *Example) MarshalJSON() ([]byte, error) {

// ExtractExamples will convert a low-level example map, into a high level one that is simple to navigate.
// no fidelity is lost, everything is still available via GoLow()
func ExtractExamples(elements *orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowBase.Example]]) *orderedmap.Map[string, *Example] {
return low.FromReferenceMapWithFunc(elements, NewExample)
func ExtractExamples(elements *orderedmap.Map[low.KeyReference[string], low.ValueReference[*lowBase.Example]], idx *index.SpecIndex) *orderedmap.Map[string, *Example] {
return low.FromReferenceMapWithFunc(elements, NewExample, idx)
}
13 changes: 6 additions & 7 deletions datamodel/high/base/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ x-hack: code`
var lowExample lowbase.Example
_ = lowmodel.BuildModel(cNode.Content[0], &lowExample)

_ = lowExample.Build(context.Background(), &cNode, cNode.Content[0], nil)
_, _ = lowExample.Build(context.Background(), &cNode, cNode.Content[0], nil)

// build high
highExample := NewExample(&lowExample)
highExample := NewExample(&lowExample, nil)

var xHack string
_ = highExample.Extensions.GetOrZero("x-hack").Decode(&xHack)
Expand Down Expand Up @@ -68,7 +68,6 @@ x-hack: code`
assert.Equal(t, "https://pb33f.io", j["externalValue"])
assert.Equal(t, "code", j["x-hack"])
assert.Equal(t, "a thing", j["value"])

}

func TestExtractExamples(t *testing.T) {
Expand All @@ -82,15 +81,15 @@ func TestExtractExamples(t *testing.T) {
var lowExample lowbase.Example
_ = lowmodel.BuildModel(cNode.Content[0], &lowExample)

_ = lowExample.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowExample.Build(context.Background(), nil, cNode.Content[0], nil)

examplesMap := orderedmap.New[lowmodel.KeyReference[string], lowmodel.ValueReference[*lowbase.Example]]()
examplesMap.Set(
lowmodel.KeyReference[string]{Value: "green"},
lowmodel.ValueReference[*lowbase.Example]{Value: &lowExample},
)

assert.Equal(t, "herbs", ExtractExamples(examplesMap).GetOrZero("green").Summary)
assert.Equal(t, "herbs", ExtractExamples(examplesMap, nil).GetOrZero("green").Summary)
}

func ExampleNewExample() {
Expand All @@ -109,10 +108,10 @@ x-hack: code`
_ = lowmodel.BuildModel(node.Content[0], &lowExample)

// build out low-level example
_ = lowExample.Build(context.Background(), nil, node.Content[0], nil)
_, _ = lowExample.Build(context.Background(), nil, node.Content[0], nil)

// create a new high-level example
highExample := NewExample(&lowExample)
highExample := NewExample(&lowExample, nil)

fmt.Print(highExample.ExternalValue)
// Output: https://pb33f.io
Expand Down
4 changes: 2 additions & 2 deletions datamodel/high/base/external_doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ x-hack: code`
var lowExt lowbase.ExternalDoc
_ = lowmodel.BuildModel(cNode.Content[0], &lowExt)

_ = lowExt.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowExt.Build(context.Background(), nil, cNode.Content[0], nil)

highExt := NewExternalDoc(&lowExt)

Expand Down Expand Up @@ -63,7 +63,7 @@ x-hack: code`
_ = lowmodel.BuildModel(node.Content[0], &lowExt)

// build out low-level properties (like extensions)
_ = lowExt.Build(context.Background(), nil, node.Content[0], nil)
_, _ = lowExt.Build(context.Background(), nil, node.Content[0], nil)

// create new high-level ExternalDoc
highExt := NewExternalDoc(&lowExt)
Expand Down
10 changes: 5 additions & 5 deletions datamodel/high/base/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ x-cli-name: chicken cli`

var lowInfo lowbase.Info
_ = lowmodel.BuildModel(cNode.Content[0], &lowInfo)
_ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)

highInfo := NewInfo(&lowInfo)

Expand Down Expand Up @@ -80,7 +80,7 @@ version: 1.2.3`
// build out the low-level model
var lowInfo lowbase.Info
_ = lowmodel.BuildModel(&node, &lowInfo)
_ = lowInfo.Build(context.Background(), nil, node.Content[0], nil)
_, _ = lowInfo.Build(context.Background(), nil, node.Content[0], nil)

// build the high level model
highInfo := NewInfo(&lowInfo)
Expand All @@ -103,7 +103,7 @@ url: https://opensource.org/licenses/MIT`
// build out the low-level model
var lowLicense lowbase.License
_ = lowmodel.BuildModel(node.Content[0], &lowLicense)
_ = lowLicense.Build(context.Background(), nil, node.Content[0], nil)
_, _ = lowLicense.Build(context.Background(), nil, node.Content[0], nil)

// build the high level model
highLicense := NewLicense(&lowLicense)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestInfo_Render(t *testing.T) {
// build low
var lowInfo lowbase.Info
_ = lowmodel.BuildModel(cNode.Content[0], &lowInfo)
_ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)

// build high
highInfo := NewInfo(&lowInfo)
Expand Down Expand Up @@ -188,7 +188,7 @@ x-cake:
// build low
var lowInfo lowbase.Info
_ = lowmodel.BuildModel(cNode.Content[0], &lowInfo)
_ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowInfo.Build(context.Background(), nil, cNode.Content[0], nil)

// build high
highInfo := NewInfo(&lowInfo)
Expand Down
14 changes: 4 additions & 10 deletions datamodel/high/base/licence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package base

import (
"context"
"testing"

lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)

func TestLicense_Render(t *testing.T) {

highL := &License{Name: "MIT", URL: "https://pb33f.io"}
dat, _ := highL.Render()

Expand All @@ -30,11 +30,9 @@ func TestLicense_Render(t *testing.T) {

assert.Equal(t, "MIT", highLicense.Name)
assert.Equal(t, "https://pb33f.io", highLicense.URL)

}

func TestLicense_RenderEqual(t *testing.T) {

yml := `name: MIT
url: https://pb33f.io/not-real
`
Expand All @@ -45,7 +43,7 @@ url: https://pb33f.io/not-real
// build low
var lowLicense lowbase.License
_ = lowmodel.BuildModel(cNode.Content[0], &lowLicense)
_ = lowLicense.Build(context.Background(), nil, cNode.Content[0], nil)
_, _ = lowLicense.Build(context.Background(), nil, cNode.Content[0], nil)

// build high
highLicense := NewLicense(&lowLicense)
Expand All @@ -56,11 +54,9 @@ url: https://pb33f.io/not-real
// re-render and ensure everything is in the same order as before.
bytes, _ := highLicense.Render()
assert.Equal(t, yml, string(bytes))

}

func TestLicense_Render_Identifier(t *testing.T) {

highL := &License{Name: "MIT", Identifier: "MIT"}
dat, _ := highL.Render()

Expand All @@ -77,11 +73,9 @@ func TestLicense_Render_Identifier(t *testing.T) {

assert.Equal(t, "MIT", highLicense.Name)
assert.Equal(t, "MIT", highLicense.Identifier)

}

func TestLicense_Render_IdentifierAndURL_Error(t *testing.T) {

// this used to fail because you can't have both an identifier and a URL
// however in v0.18.0 I deleted this logic, because it's dumb.
highL := &License{Name: "MIT", Identifier: "MIT", URL: "https://pb33f.io"}
Expand All @@ -94,7 +88,7 @@ func TestLicense_Render_IdentifierAndURL_Error(t *testing.T) {
// build low
var lowLicense lowbase.License
_ = lowmodel.BuildModel(cNode.Content[0], &lowLicense)
err := lowLicense.Build(context.Background(), nil, cNode.Content[0], nil)
_, err := lowLicense.Build(context.Background(), nil, cNode.Content[0], nil)

assert.NoError(t, err)
}
Loading

0 comments on commit 2874552

Please sign in to comment.