Removing copier lib

This commit is contained in:
Mike Farah 2023-10-18 12:36:52 +11:00
parent 13d1bbb45f
commit c8f4ba7f45
2 changed files with 67 additions and 24 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/jinzhu/copier"
logging "gopkg.in/op/go-logging.v1" logging "gopkg.in/op/go-logging.v1"
) )
@ -58,13 +57,19 @@ func (n *Context) SetVariable(name string, value *list.List) {
func (n *Context) ChildContext(results *list.List) Context { func (n *Context) ChildContext(results *list.List) Context {
clone := Context{DontAutoCreate: n.DontAutoCreate, datetimeLayout: n.datetimeLayout} clone := Context{DontAutoCreate: n.DontAutoCreate, datetimeLayout: n.datetimeLayout}
clone.Variables = make(map[string]*list.List) clone.Variables = make(map[string]*list.List)
if len(n.Variables) > 0 { for variableKey, originalValueList := range n.Variables {
err := copier.Copy(&clone.Variables, n.Variables)
if err != nil { variableCopyList := list.New()
log.Error("Error cloning context :(") for el := originalValueList.Front(); el != nil; el = el.Next() {
panic(err) // note that we dont make a copy of the candidate node
// this is so the 'ref' operator can work correctly.
clonedNode := el.Value.(*CandidateNode)
variableCopyList.PushBack(clonedNode)
} }
clone.Variables[variableKey] = variableCopyList
} }
clone.MatchingNodes = results clone.MatchingNodes = results
return clone return clone
} }
@ -78,31 +83,18 @@ func (n *Context) ToString() string {
} }
func (n *Context) DeepClone() Context { func (n *Context) DeepClone() Context {
clone := Context{}
err := copier.Copy(&clone, n) clonedContent := list.New()
// copier doesn't do lists properly for some reason
clone.MatchingNodes = list.New()
for el := n.MatchingNodes.Front(); el != nil; el = el.Next() { for el := n.MatchingNodes.Front(); el != nil; el = el.Next() {
clonedNode := el.Value.(*CandidateNode).Copy() clonedNode := el.Value.(*CandidateNode).Copy()
clone.MatchingNodes.PushBack(clonedNode) clonedContent.PushBack(clonedNode)
} }
if err != nil { return n.ChildContext(clonedContent)
log.Error("Error cloning context :(")
panic(err)
}
return clone
} }
func (n *Context) Clone() Context { func (n *Context) Clone() Context {
clone := Context{} return n.ChildContext(n.MatchingNodes)
err := copier.Copy(&clone, n)
if err != nil {
log.Error("Error cloning context :(")
panic(err)
}
return clone
} }
func (n *Context) ReadOnlyClone() Context { func (n *Context) ReadOnlyClone() Context {

51
pkg/yqlib/context_test.go Normal file
View File

@ -0,0 +1,51 @@
package yqlib
import (
"container/list"
"testing"
"github.com/mikefarah/yq/v4/test"
)
func TestChildContext(t *testing.T) {
expectedOriginal := make(map[string]*list.List)
expectedOriginal["dog"] = list.New()
expectedOriginal["dog"].PushBack(&CandidateNode{Value: "woof"})
originalVariables := make(map[string]*list.List)
originalVariables["dog"] = list.New()
originalVariables["dog"].PushBack(&CandidateNode{Value: "woof"})
original := Context{
DontAutoCreate: true,
datetimeLayout: "cat",
Variables: originalVariables,
}
newResults := list.New()
newResults.PushBack(&CandidateNode{Value: "bar"})
clone := original.ChildContext(newResults)
test.AssertResultComplex(t, originalVariables, clone.Variables)
clone.Variables["dog"].PushBack("bark")
// ensure this is a separate copy
test.AssertResultComplex(t, 1, originalVariables["dog"].Len())
}
func TestChildContextNoVariables(t *testing.T) {
original := Context{
DontAutoCreate: true,
datetimeLayout: "cat",
}
newResults := list.New()
newResults.PushBack(&CandidateNode{Value: "bar"})
clone := original.ChildContext(newResults)
test.AssertResultComplex(t, make(map[string]*list.List), clone.Variables)
}