Skip to content

Commit

Permalink
Addressed #159
Browse files Browse the repository at this point in the history
Cleaned up all interesting static checks. All that is left is cosmetics.

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Feb 7, 2024
1 parent 561396d commit 7afb1e9
Show file tree
Hide file tree
Showing 39 changed files with 180 additions and 279 deletions.
18 changes: 7 additions & 11 deletions datamodel/high/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,9 @@ func NewSchema(schema *base.Schema) *Schema {
}
j := 0
for j < totalSchemas {
select {
case r := <-bChan:
j++
(*items)[r.idx] = r.s
}
r := <-bChan
j++
(*items)[r.idx] = r.s
}
doneChan <- true
}
Expand Down Expand Up @@ -440,12 +438,10 @@ func NewSchema(schema *base.Schema) *Schema {
if children > 0 {
allDone:
for {
select {
case <-polyCompletedChan:
completeChildren++
if children == completeChildren {
break allDone
}
<-polyCompletedChan
completeChildren++
if children == completeChildren {
break allDone
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions datamodel/high/v2/path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/pb33f/libopenapi/datamodel/low"
lowV2 "github.com/pb33f/libopenapi/datamodel/low/v2"
v2 "github.com/pb33f/libopenapi/datamodel/low/v2"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -37,7 +36,7 @@ options:
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n v2.PathItem
var n lowV2.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

Expand Down Expand Up @@ -85,10 +84,10 @@ func TestPathItem_GetOperations_LowWithUnsetOperations(t *testing.T) {

func TestPathItem_NewPathItem_WithParameters(t *testing.T) {
pi := NewPathItem(&lowV2.PathItem{
Parameters: low.NodeReference[[]low.ValueReference[*v2.Parameter]]{
Value: []low.ValueReference[*v2.Parameter]{
Parameters: low.NodeReference[[]low.ValueReference[*lowV2.Parameter]]{
Value: []low.ValueReference[*lowV2.Parameter]{
{
Value: &v2.Parameter{},
Value: &lowV2.Parameter{},
},
},
ValueNode: &yaml.Node{},
Expand Down
3 changes: 1 addition & 2 deletions datamodel/high/v3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func buildComponent[IN any, OUT any](inMap *orderedmap.Map[lowmodel.KeyReference
func buildSchema(inMap *orderedmap.Map[lowmodel.KeyReference[string], lowmodel.ValueReference[*base.SchemaProxy]], outMap *orderedmap.Map[string, *highbase.SchemaProxy]) {
translateFunc := func(pair orderedmap.Pair[lowmodel.KeyReference[string], lowmodel.ValueReference[*base.SchemaProxy]]) (componentResult[*highbase.SchemaProxy], error) {
value := pair.Value()
var sch *highbase.SchemaProxy
sch = highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
sch := highbase.NewSchemaProxy(&lowmodel.NodeReference[*base.SchemaProxy]{
Value: value.Value,
ValueNode: value.ValueNode,
})
Expand Down
1 change: 1 addition & 0 deletions datamodel/high/v3/oauth_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestOAuthFlow_MarshalYAML(t *testing.T) {
}

rend, _ := oflow.Render()
assert.NotNil(t, rend)

desired := `authorizationUrl: https://pb33f.io
tokenUrl: https://pb33f.io/token
Expand Down
39 changes: 19 additions & 20 deletions datamodel/high/v3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,26 @@ func NewPathItem(pathItem *lowV3.PathItem) *PathItem {
complete := false
opCount := 0
for !complete {
select {
case opRes := <-opChan:
switch opRes.method {
case get:
pi.Get = opRes.op
case put:
pi.Put = opRes.op
case post:
pi.Post = opRes.op
case del:
pi.Delete = opRes.op
case options:
pi.Options = opRes.op
case head:
pi.Head = opRes.op
case patch:
pi.Patch = opRes.op
case trace:
pi.Trace = opRes.op
}
opRes := <-opChan
switch opRes.method {
case get:
pi.Get = opRes.op
case put:
pi.Put = opRes.op
case post:
pi.Post = opRes.op
case del:
pi.Delete = opRes.op
case options:
pi.Options = opRes.op
case head:
pi.Head = opRes.op
case patch:
pi.Patch = opRes.op
case trace:
pi.Trace = opRes.op
}

opCount++
if opCount == 8 {
complete = true
Expand Down
5 changes: 2 additions & 3 deletions datamodel/high/v3/path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/pb33f/libopenapi/datamodel/low"
lowV3 "github.com/pb33f/libopenapi/datamodel/low/v3"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
Expand All @@ -29,7 +28,7 @@ func TestPathItem(t *testing.T) {
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n v3.PathItem
var n lowV3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

Expand Down Expand Up @@ -63,7 +62,7 @@ trace:
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n v3.PathItem
var n lowV3.PathItem
_ = low.BuildModel(&idxNode, &n)
_ = n.Build(context.Background(), nil, idxNode.Content[0], idx)

Expand Down
1 change: 1 addition & 0 deletions datamodel/low/base/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ x-cake: hot`

var example string
err = n.Value.Value.Decode(&example)
require.NoError(t, err)
assert.Equal(t, "a string example", example)

var xCake string
Expand Down
1 change: 1 addition & 0 deletions datamodel/low/base/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ description: the ranch`
// create low level objects
var lDoc License
err := low.BuildModel(lNode.Content[0], &lDoc)
assert.NoError(t, err)

err = lDoc.Build(context.Background(), nil, lNode.Content[0], nil)

Expand Down
34 changes: 15 additions & 19 deletions datamodel/low/base/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,15 +1134,13 @@ func buildSchema(ctx context.Context, schemas chan schemaProxyBuildResult, label
// this only runs once, however to keep things consistent, it makes sense to use the same async method
// that arrays will use.
go build(foundCtx, labelNode, valueNode, refNode, -1, syncChan, isRef, refLocation)
select {
case r := <-syncChan:
schemas <- schemaProxyBuildResult{
k: low.KeyReference[string]{
KeyNode: labelNode,
Value: labelNode.Value,
},
v: *r.res,
}
r := <-syncChan
schemas <- schemaProxyBuildResult{
k: low.KeyReference[string]{
KeyNode: labelNode,
Value: labelNode.Value,
},
v: *r.res,
}
} else if utils.IsNodeArray(valueNode) {
refBuilds := 0
Expand Down Expand Up @@ -1171,11 +1169,9 @@ func buildSchema(ctx context.Context, schemas chan schemaProxyBuildResult, label

completedBuilds := 0
for completedBuilds < refBuilds {
select {
case res := <-syncChan:
completedBuilds++
results[res.idx] = res.res
}
res := <-syncChan
completedBuilds++
results[res.idx] = res.res
}

for _, r := range results {
Expand Down Expand Up @@ -1225,14 +1221,14 @@ func ExtractSchema(ctx context.Context, root *yaml.Node, idx *index.SpecIndex) (
if schNode != nil {
h := false
if h, _, refLocation = utils.IsNodeRefValue(schNode); h {
ref, foundIdx, _, nCtx := low.LocateRefNodeWithContext(ctx, schNode, idx)
ref, _, _, nCtx := low.LocateRefNodeWithContext(ctx, schNode, idx)
if ref != nil {
refNode = schNode
schNode = ref
if foundIdx != nil {
// TODO: check on this
// idx = foundIdx
}
//if foundIdx != nil {
// TODO: check on this
// idx = foundIdx
//}
ctx = nCtx
} else {
v := schNode.Content[1].Value
Expand Down
1 change: 1 addition & 0 deletions datamodel/low/base/schema_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ properties:

// do it again, but with no index
err = schC.Build(context.Background(), nil, idxNodeA.Content[0], nil)
assert.NoError(t, err)
origin = schC.GetSchemaReferenceLocation()
assert.Nil(t, origin)
}
8 changes: 3 additions & 5 deletions datamodel/low/base/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1876,11 +1876,9 @@ func TestBuildSchema_BadNodeTypes(t *testing.T) {
var err error
go func() {
for {
select {
case e := <-eChan:
err = e
doneChan <- true
}
e := <-eChan
err = e
doneChan <- true
}
}()

Expand Down
9 changes: 5 additions & 4 deletions datamodel/low/extraction_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ type mappingResult[T any] struct {
v ValueReference[T]
}

type buildInput struct {
label *yaml.Node
value *yaml.Node
}

// ExtractMapExtensions will extract a map of KeyReference and ValueReference from a root yaml.Node. The 'label' is
// used to locate the node to be extracted from the root node supplied. Supply a bit to decide if extensions should
// be included or not. required in some use cases.
Expand Down Expand Up @@ -655,10 +660,6 @@ func ExtractMapExtensions[PT Buildable[N], N any](
if valueNode != nil {
valueMap := orderedmap.New[KeyReference[string], ValueReference[PT]]()

type buildInput struct {
label *yaml.Node
value *yaml.Node
}
in := make(chan buildInput)
out := make(chan mappingResult[PT])
done := make(chan struct{})
Expand Down
16 changes: 4 additions & 12 deletions datamodel/low/extraction_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,6 @@ func TestExtractObject_DoubleRef_Circular_Direct_Fail(t *testing.T) {
assert.Error(t, err)
}

type test_borked struct {
DontWork int
}

func (t test_borked) Build(_ context.Context, _, root *yaml.Node, idx *index.SpecIndex) error {
return fmt.Errorf("I am always going to fail, every thing")
}

type test_noGood struct {
DontWork int
}
Expand Down Expand Up @@ -1898,11 +1890,11 @@ func TestLocateRefNode_DoARealLookup(t *testing.T) {

func TestLocateRefEndNoRef_NoName(t *testing.T) {
r := &yaml.Node{Content: []*yaml.Node{{Kind: yaml.ScalarNode, Value: "$ref"}, {Kind: yaml.ScalarNode, Value: ""}}}
n, i, e, c := LocateRefEnd(nil, r, nil, 0)
n, i, e, c := LocateRefEnd(context.TODO(), r, nil, 0)
assert.Nil(t, n)
assert.Nil(t, i)
assert.Error(t, e)
assert.Nil(t, c)
assert.NotNil(t, c)
}

func TestLocateRefEndNoRef(t *testing.T) {
Expand All @@ -1916,11 +1908,11 @@ func TestLocateRefEndNoRef(t *testing.T) {

func TestLocateRefEnd_TooDeep(t *testing.T) {
r := &yaml.Node{Content: []*yaml.Node{{Kind: yaml.ScalarNode, Value: "$ref"}, {Kind: yaml.ScalarNode, Value: ""}}}
n, i, e, c := LocateRefEnd(nil, r, nil, 100)
n, i, e, c := LocateRefEnd(context.TODO(), r, nil, 100)
assert.Nil(t, n)
assert.Nil(t, i)
assert.Error(t, e)
assert.Nil(t, c)
assert.NotNil(t, c)
}

func TestLocateRefEnd_Loop(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion datamodel/low/model_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) {
// we want to ignore everything else, each model handles its own complex types.
break
}
return
}

// BuildModelAsync is a convenience function for calling BuildModel from a goroutine, requires a sync.WaitGroup
Expand Down
1 change: 0 additions & 1 deletion datamodel/low/model_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ func TestHandleSlicesOfBools(t *testing.T) {

func TestSetField_Ignore(t *testing.T) {
type Complex struct {
name string
}
type internal struct {
Thing *Complex
Expand Down
7 changes: 0 additions & 7 deletions datamodel/low/v2/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,12 @@ func (p *PathItem) Build(ctx context.Context, _, root *yaml.Node, idx *index.Spe
// the only thing we now care about is handling operations, filter out anything that's not a verb.
switch currentNode.Value {
case GetLabel:
break
case PostLabel:
break
case PutLabel:
break
case PatchLabel:
break
case DeleteLabel:
break
case HeadLabel:
break
case OptionsLabel:
break
default:
continue // ignore everything else.
}
Expand Down
8 changes: 0 additions & 8 deletions datamodel/low/v3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,13 @@ func (p *PathItem) Build(ctx context.Context, keyNode, root *yaml.Node, idx *ind
// the only thing we now care about is handling operations, filter out anything that's not a verb.
switch currentNode.Value {
case GetLabel:
break
case PostLabel:
break
case PutLabel:
break
case PatchLabel:
break
case DeleteLabel:
break
case HeadLabel:
break
case OptionsLabel:
break
case TraceLabel:
break
default:
continue // ignore everything else.
}
Expand Down
1 change: 1 addition & 0 deletions datamodel/low/v3/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ x-shoes: old`
assert.NoError(t, err)

err = n.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)

// check hash
assert.Equal(t, "3da5051dcd82a06f8e4c7698cdec03550ae1988ee54d96d4c4a90a5c8f9d7b2b",
Expand Down
5 changes: 0 additions & 5 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,6 @@ func BenchmarkReferenceOrigin(b *testing.B) {
items := mediaType.A.Schema()

origin := items.ParentProxy.GetReferenceOrigin()
if origin == nil {
// fmt.Println("nil origin")
} else {
// fmt.Println(origin.AbsoluteLocation)
}
assert.NotNil(b, origin)
assert.True(b, strings.HasSuffix(origin.AbsoluteLocation, "test_specs/burgershop.openapi.yaml"))
}
Expand Down
Loading

0 comments on commit 7afb1e9

Please sign in to comment.