Skip to content

Commit

Permalink
Removing copier lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Oct 18, 2023
1 parent 13d1bbb commit c8f4ba7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
40 changes: 16 additions & 24 deletions pkg/yqlib/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/jinzhu/copier"
logging "gopkg.in/op/go-logging.v1"
)

Expand Down Expand Up @@ -58,13 +57,19 @@ func (n *Context) SetVariable(name string, value *list.List) {
func (n *Context) ChildContext(results *list.List) Context {
clone := Context{DontAutoCreate: n.DontAutoCreate, datetimeLayout: n.datetimeLayout}
clone.Variables = make(map[string]*list.List)
if len(n.Variables) > 0 {
err := copier.Copy(&clone.Variables, n.Variables)
if err != nil {
log.Error("Error cloning context :(")
panic(err)
for variableKey, originalValueList := range n.Variables {

variableCopyList := list.New()
for el := originalValueList.Front(); el != nil; el = el.Next() {
// 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
return clone
}
Expand All @@ -78,31 +83,18 @@ func (n *Context) ToString() string {
}

func (n *Context) DeepClone() Context {
clone := Context{}
err := copier.Copy(&clone, n)
// copier doesn't do lists properly for some reason
clone.MatchingNodes = list.New()

clonedContent := list.New()
for el := n.MatchingNodes.Front(); el != nil; el = el.Next() {
clonedNode := el.Value.(*CandidateNode).Copy()
clone.MatchingNodes.PushBack(clonedNode)
clonedContent.PushBack(clonedNode)
}

if err != nil {
log.Error("Error cloning context :(")
panic(err)
}
return clone
return n.ChildContext(clonedContent)
}

func (n *Context) Clone() Context {
clone := Context{}
err := copier.Copy(&clone, n)

if err != nil {
log.Error("Error cloning context :(")
panic(err)
}
return clone
return n.ChildContext(n.MatchingNodes)
}

func (n *Context) ReadOnlyClone() Context {
Expand Down
51 changes: 51 additions & 0 deletions pkg/yqlib/context_test.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit c8f4ba7

Please sign in to comment.