Made progress on the design for mutability

The work is now almost fully reflective, and *drumroll* the original order of the data is maintained when re-rendering. new elements pop in at the top of the object, but everything else will remain in the sequence it was originally added.
This commit is contained in:
Dave Shanley
2023-03-02 10:32:31 -05:00
parent 441068174c
commit 88709c389a
9 changed files with 289 additions and 30 deletions

View File

@@ -26,6 +26,11 @@ type HasValueNode[T any] interface {
*T
}
// HasValueNodeUntyped is implemented by NodeReference and ValueReference to return the yaml.Node backing the value.
type HasValueNodeUntyped interface {
GetValueNode() *yaml.Node
}
// Hashable defines any struct that implements a Hash function that returns a 256SHA hash of the state of the
// representative object. Great for equality checking!
type Hashable interface {
@@ -39,6 +44,13 @@ type HasExtensions[T any] interface {
GetExtensions() map[KeyReference[string]]ValueReference[any]
}
// HasExtensionsUntyped is implemented by any object that exposes extensions
type HasExtensionsUntyped interface {
// GetExtensions returns generic low level extensions
GetExtensions() map[KeyReference[string]]ValueReference[any]
}
// HasValue is implemented by NodeReference and ValueReference to return the yaml.Node backing the value.
type HasValue[T any] interface {
GetValue() T
@@ -101,6 +113,15 @@ func (n NodeReference[T]) IsEmpty() bool {
return n.KeyNode == nil && n.ValueNode == nil
}
func (n NodeReference[T]) NodeLineNumber() int {
if !n.IsEmpty() {
return n.ValueNode.Line
} else {
return 0
}
}
// IsReferenceNode will return true if the key node contains a $ref key.
func (n NodeReference[T]) IsReferenceNode() bool {
if n.KeyNode != nil {
for k := range n.KeyNode.Content {
@@ -142,6 +163,15 @@ func (n ValueReference[T]) IsEmpty() bool {
return n.ValueNode == nil
}
// NodeLineNumber will return the line number of the value node (or 0 if the value node is empty)
func (n ValueReference[T]) NodeLineNumber() int {
if !n.IsEmpty() {
return n.ValueNode.Line
} else {
return 0
}
}
// GenerateMapKey will return a string based on the line and column number of the node, e.g. 33:56 for line 33, col 56.
func (n ValueReference[T]) GenerateMapKey() string {
return fmt.Sprintf("%d:%d", n.ValueNode.Line, n.ValueNode.Column)