mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +00:00
chore: some cleanup
This commit is contained in:
@@ -200,8 +200,6 @@ func (n *NodeBuilder) add(key string, i int) {
|
||||
return lines[i] < lines[j]
|
||||
})
|
||||
nodeEntry.Line = lines[0]
|
||||
case reflect.Map:
|
||||
panic("only ordered maps are supported")
|
||||
case reflect.Struct:
|
||||
y := value.Interface()
|
||||
nodeEntry.Line = 9999 + i
|
||||
@@ -330,11 +328,7 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
|
||||
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
|
||||
valueNode = utils.CreateFloatNode(val)
|
||||
valueNode.Line = line
|
||||
case reflect.Map:
|
||||
panic("only ordered maps are supported")
|
||||
|
||||
case reflect.Slice:
|
||||
|
||||
var rawNode yaml.Node
|
||||
m := reflect.ValueOf(value)
|
||||
sl := utils.CreateEmptySequenceNode()
|
||||
|
||||
@@ -21,10 +21,6 @@ type MapToYamlNoder interface {
|
||||
ToYamlNode(n NodeBuilder, l any) *yaml.Node
|
||||
}
|
||||
|
||||
type hasKeyNode interface {
|
||||
GetKeyNode() *yaml.Node
|
||||
}
|
||||
|
||||
type hasValueNode interface {
|
||||
GetValueNode() *yaml.Node
|
||||
}
|
||||
@@ -37,6 +33,7 @@ type findValueUntyped interface {
|
||||
FindValueUntyped(k string) any
|
||||
}
|
||||
|
||||
// ToYamlNode converts the ordered map to a yaml node ready for marshalling.
|
||||
func (o *Map[K, V]) ToYamlNode(n NodeBuilder, l any) *yaml.Node {
|
||||
p := utils.CreateEmptyMapNode()
|
||||
|
||||
@@ -105,6 +102,7 @@ func findKeyNode(key string, m *yaml.Node) *yaml.Node {
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindValueUntyped finds a value in the ordered map by key if the stored value for that key implements GetValueUntyped otherwise just returns the value.
|
||||
func (o *Map[K, V]) FindValueUntyped(key string) any {
|
||||
for pair := First(o); pair != nil; pair = pair.Next() {
|
||||
var k any = pair.Key()
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
wk8orderedmap "github.com/wk8/go-ordered-map/v2"
|
||||
)
|
||||
|
||||
// Pair represents a key/value pair in an ordered map returned for iteration.
|
||||
type Pair[K comparable, V any] interface {
|
||||
Key() K
|
||||
KeyPtr() *K
|
||||
@@ -22,6 +23,7 @@ type Pair[K comparable, V any] interface {
|
||||
Next() Pair[K, V]
|
||||
}
|
||||
|
||||
// Map represents an ordered map where the key must be a comparable type, the ordering is based on insertion order.
|
||||
type Map[K comparable, V any] struct {
|
||||
*wk8orderedmap.OrderedMap[K, V]
|
||||
}
|
||||
@@ -37,14 +39,17 @@ func New[K comparable, V any]() *Map[K, V] {
|
||||
}
|
||||
}
|
||||
|
||||
// GetKeyType returns the reflection type of the key.
|
||||
func (o *Map[K, V]) GetKeyType() reflect.Type {
|
||||
return reflect.TypeOf(new(K))
|
||||
}
|
||||
|
||||
// GetValueType returns the reflection type of the value.
|
||||
func (o *Map[K, V]) GetValueType() reflect.Type {
|
||||
return reflect.TypeOf(new(V))
|
||||
}
|
||||
|
||||
// GetOrZero will return the value for the key if it exists, otherwise it will return the zero value for the value type.
|
||||
func (o *Map[K, V]) GetOrZero(k K) V {
|
||||
v, ok := o.OrderedMap.Get(k)
|
||||
if !ok {
|
||||
@@ -54,6 +59,7 @@ func (o *Map[K, V]) GetOrZero(k K) V {
|
||||
return v
|
||||
}
|
||||
|
||||
// First returns the first pair in the map useful for iteration.
|
||||
func (o *Map[K, V]) First() Pair[K, V] {
|
||||
if o == nil {
|
||||
return nil
|
||||
@@ -93,6 +99,7 @@ func (o *Map[K, V]) IsZero() bool {
|
||||
return Len(o) == 0
|
||||
}
|
||||
|
||||
// Next returns the next pair in the map when iterating.
|
||||
func (p *wrapPair[K, V]) Next() Pair[K, V] {
|
||||
next := p.Pair.Next()
|
||||
if next == nil {
|
||||
@@ -103,18 +110,22 @@ func (p *wrapPair[K, V]) Next() Pair[K, V] {
|
||||
}
|
||||
}
|
||||
|
||||
// Key returns the key of the pair.
|
||||
func (p *wrapPair[K, V]) Key() K {
|
||||
return p.Pair.Key
|
||||
}
|
||||
|
||||
// KeyPtr returns a pointer to the key of the pair.
|
||||
func (p *wrapPair[K, V]) KeyPtr() *K {
|
||||
return &p.Pair.Key
|
||||
}
|
||||
|
||||
// Value returns the value of the pair.
|
||||
func (p *wrapPair[K, V]) Value() V {
|
||||
return p.Pair.Value
|
||||
}
|
||||
|
||||
// ValuePtr returns a pointer to the value of the pair.
|
||||
func (p *wrapPair[K, V]) ValuePtr() *V {
|
||||
return &p.Pair.Value
|
||||
}
|
||||
@@ -183,6 +194,7 @@ func Cast[K comparable, V any](v any) *Map[K, V] {
|
||||
return m
|
||||
}
|
||||
|
||||
// SortAlpha sorts the map by keys in alphabetical order.
|
||||
func SortAlpha[K comparable, V any](m *Map[K, V]) *Map[K, V] {
|
||||
if m == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user