fix: cleanup

This commit is contained in:
Tristan Cartledge
2023-12-01 17:47:55 +00:00
parent a4ad09aab3
commit 1a384fd982
9 changed files with 14 additions and 23 deletions

View File

@@ -31,7 +31,7 @@ func NewDiscriminator(disc *low.Discriminator) *Discriminator {
d.low = disc d.low = disc
d.PropertyName = disc.PropertyName.Value d.PropertyName = disc.PropertyName.Value
mapping := orderedmap.New[string, string]() mapping := orderedmap.New[string, string]()
for pair := disc.Mapping.Value.First(); pair != nil; pair = pair.Next() { for pair := orderedmap.First(disc.Mapping.Value); pair != nil; pair = pair.Next() {
mapping.Set(pair.Key().Value, pair.Value().Value) mapping.Set(pair.Key().Value, pair.Value().Value)
} }
d.Mapping = mapping d.Mapping = mapping

View File

@@ -12,12 +12,12 @@ import (
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap" "github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func TestComponents_MarshalYAML(t *testing.T) { func TestComponents_MarshalYAML(t *testing.T) {
comp := &Components{ comp := &Components{
Responses: orderedmap.ToOrderedMap(map[string]*Response{ Responses: orderedmap.ToOrderedMap(map[string]*Response{
"200": { "200": {
@@ -34,7 +34,7 @@ func TestComponents_MarshalYAML(t *testing.T) {
"body": { "body": {
Content: orderedmap.ToOrderedMap(map[string]*MediaType{ Content: orderedmap.ToOrderedMap(map[string]*MediaType{
"application/json": { "application/json": {
Example: "why?", Example: utils.CreateStringNode("why?"),
}, },
}), }),
}, },

View File

@@ -19,7 +19,7 @@ import (
// - https://spec.openapis.org/oas/v3.1.0#media-type-object // - https://spec.openapis.org/oas/v3.1.0#media-type-object
type MediaType struct { type MediaType struct {
Schema *base.SchemaProxy `json:"schema,omitempty" yaml:"schema,omitempty"` Schema *base.SchemaProxy `json:"schema,omitempty" yaml:"schema,omitempty"`
Example any `json:"example,omitempty" yaml:"example,omitempty"` Example *yaml.Node `json:"example,omitempty" yaml:"example,omitempty"`
Examples *orderedmap.Map[string, *base.Example] `json:"examples,omitempty" yaml:"examples,omitempty"` Examples *orderedmap.Map[string, *base.Example] `json:"examples,omitempty" yaml:"examples,omitempty"`
Encoding *orderedmap.Map[string, *Encoding] `json:"encoding,omitempty" yaml:"encoding,omitempty"` Encoding *orderedmap.Map[string, *Encoding] `json:"encoding,omitempty" yaml:"encoding,omitempty"`
Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"` Extensions *orderedmap.Map[string, *yaml.Node] `json:"-" yaml:"-"`

View File

@@ -13,6 +13,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -40,7 +41,7 @@ func TestMediaType_MarshalYAMLInline(t *testing.T) {
assert.Equal(t, op, strings.TrimSpace(string(yml))) assert.Equal(t, op, strings.TrimSpace(string(yml)))
// modify the media type to have an example // modify the media type to have an example
mt.Example = "testing a nice mutation" mt.Example = utils.CreateStringNode("testing a nice mutation")
op = `schema: op = `schema:
required: required:
@@ -128,7 +129,7 @@ func TestMediaType_MarshalYAML(t *testing.T) {
assert.Equal(t, op, strings.TrimSpace(string(yml))) assert.Equal(t, op, strings.TrimSpace(string(yml)))
// modify the media type to have an example // modify the media type to have an example
mt.Example = "testing a nice mutation" mt.Example = utils.CreateStringNode("testing a nice mutation")
op = `schema: {"$ref": "#/components/schemas/Pet"} op = `schema: {"$ref": "#/components/schemas/Pet"}
example: testing a nice mutation` example: testing a nice mutation`

View File

@@ -11,6 +11,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low"
v3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
"github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -73,7 +74,7 @@ trace:
expectedOrder := []string{"get", "put", "post", "patch", "delete", "head", "options", "trace"} expectedOrder := []string{"get", "put", "post", "patch", "delete", "head", "options", "trace"}
i := 0 i := 0
for pair := r.GetOperations().First(); pair != nil; pair = pair.Next() { for pair := orderedmap.First(r.GetOperations()); pair != nil; pair = pair.Next() {
assert.Equal(t, expectedOrder[i], pair.Value().Description) assert.Equal(t, expectedOrder[i], pair.Value().Description)
i++ i++
} }

View File

@@ -138,7 +138,7 @@ allTheThings:
assert.Equal(t, 324938249028.98234892374892374923874823974, hd.Mustard.Value) assert.Equal(t, 324938249028.98234892374892374923874823974, hd.Mustard.Value)
allTheThings := hd.AllTheThings.Value allTheThings := hd.AllTheThings.Value
for pair := allTheThings.First(); pair != nil; pair = pair.Next() { for pair := orderedmap.First(allTheThings); pair != nil; pair = pair.Next() {
if pair.Key().Value == "beer" { if pair.Key().Value == "beer" {
assert.Equal(t, "isGood", pair.Value().Value) assert.Equal(t, "isGood", pair.Value().Value)
} }

View File

@@ -52,7 +52,7 @@ func (o *Map[K, V]) ToYamlNode(n NodeBuilder, l any) *yaml.Node {
} }
} }
for pair := o.First(); pair != nil; pair = pair.Next() { for pair := First(o); pair != nil; pair = pair.Next() {
var k any = pair.Key() var k any = pair.Key()
if m, ok := k.(Marshaler); ok { // TODO marshal inline? if m, ok := k.(Marshaler); ok { // TODO marshal inline?
k, _ = m.MarshalYAML() k, _ = m.MarshalYAML()
@@ -114,7 +114,7 @@ func findKeyNode(key string, m *yaml.Node) *yaml.Node {
} }
func (o *Map[K, V]) FindValueUntyped(key string) any { func (o *Map[K, V]) FindValueUntyped(key string) any {
for pair := o.First(); pair != nil; pair = pair.Next() { for pair := First(o); pair != nil; pair = pair.Next() {
var k any = pair.Key() var k any = pair.Key()
if hvut, ok := k.(HasValueUntyped); ok { if hvut, ok := k.(HasValueUntyped); ok {
if fmt.Sprintf("%v", hvut.GetValueUntyped()) == key { if fmt.Sprintf("%v", hvut.GetValueUntyped()) == key {

View File

@@ -140,7 +140,7 @@ func Iterate[K comparable, V any](ctx context.Context, m *Map[K, V]) <-chan Pair
} }
go func() { go func() {
defer close(c) defer close(c)
for pair := m.First(); pair != nil; pair = pair.Next() { for pair := First(m); pair != nil; pair = pair.Next() {
select { select {
case c <- pair: case c <- pair:
case <-ctx.Done(): case <-ctx.Done():
@@ -196,7 +196,7 @@ func SortAlpha[K comparable, V any](m *Map[K, V]) *Map[K, V] {
} }
keys := []key{} keys := []key{}
for pair := m.First(); pair != nil; pair = pair.Next() { for pair := First(m); pair != nil; pair = pair.Next() {
keys = append(keys, key{ keys = append(keys, key{
key: fmt.Sprintf("%v", pair.Key()), key: fmt.Sprintf("%v", pair.Key()),
k: pair.Key(), k: pair.Key(),

View File

@@ -70,17 +70,6 @@ func CreateContext(l, r *yaml.Node) *ChangeContext {
return ctx return ctx
} }
func FlattenLowLevelMap[T any](
lowMap map[low.KeyReference[string]]low.ValueReference[T],
) map[string]*low.ValueReference[T] {
flat := make(map[string]*low.ValueReference[T])
for i := range lowMap {
l := lowMap[i]
flat[i.Value] = &l
}
return flat
}
func FlattenLowLevelOrderedMap[T any]( func FlattenLowLevelOrderedMap[T any](
lowMap *orderedmap.Map[low.KeyReference[string], low.ValueReference[T]], lowMap *orderedmap.Map[low.KeyReference[string], low.ValueReference[T]],
) map[string]*low.ValueReference[T] { ) map[string]*low.ValueReference[T] {