yq/pkg/yqlib/context.go

119 lines
2.5 KiB
Go
Raw Normal View History

package yqlib
2021-02-03 00:54:10 +00:00
import (
"container/list"
2021-09-05 01:07:40 +00:00
"fmt"
"time"
2021-02-03 00:54:10 +00:00
"github.com/jinzhu/copier"
2021-09-05 01:07:40 +00:00
logging "gopkg.in/op/go-logging.v1"
2021-02-03 00:54:10 +00:00
)
type Context struct {
2021-02-03 00:54:10 +00:00
MatchingNodes *list.List
Variables map[string]*list.List
DontAutoCreate bool
datetimeLayout string
}
func (n *Context) SingleReadonlyChildContext(candidate *CandidateNode) Context {
list := list.New()
list.PushBack(candidate)
newContext := n.ChildContext(list)
newContext.DontAutoCreate = true
return newContext
}
func (n *Context) SingleChildContext(candidate *CandidateNode) Context {
2021-02-03 00:54:10 +00:00
list := list.New()
list.PushBack(candidate)
return n.ChildContext(list)
}
func (n *Context) SetDateTimeLayout(newDateTimeLayout string) {
n.datetimeLayout = newDateTimeLayout
}
func (n *Context) GetDateTimeLayout() string {
if n.datetimeLayout != "" {
return n.datetimeLayout
}
return time.RFC3339
}
2021-02-03 04:51:26 +00:00
func (n *Context) GetVariable(name string) *list.List {
if n.Variables == nil {
return nil
}
return n.Variables[name]
}
func (n *Context) SetVariable(name string, value *list.List) {
if n.Variables == nil {
n.Variables = make(map[string]*list.List)
}
n.Variables[name] = value
}
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)
}
2021-02-03 00:54:10 +00:00
}
clone.MatchingNodes = results
return clone
}
2021-02-04 02:47:59 +00:00
2021-09-05 01:07:40 +00:00
func (n *Context) ToString() string {
if !log.IsEnabledFor(logging.DEBUG) {
return ""
}
result := fmt.Sprintf("Context\nDontAutoCreate: %v\n", n.DontAutoCreate)
return result + NodesToString(n.MatchingNodes)
}
2021-09-12 06:55:55 +00:00
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()
for el := n.MatchingNodes.Front(); el != nil; el = el.Next() {
2023-04-03 06:16:07 +00:00
clonedNode := el.Value.(*CandidateNode).Copy()
2021-09-12 06:55:55 +00:00
clone.MatchingNodes.PushBack(clonedNode)
}
if err != nil {
log.Error("Error cloning context :(")
panic(err)
}
return clone
}
2021-02-04 02:47:59 +00:00
func (n *Context) Clone() Context {
clone := Context{}
err := copier.Copy(&clone, n)
2021-09-12 06:55:55 +00:00
2021-02-04 02:47:59 +00:00
if err != nil {
log.Error("Error cloning context :(")
panic(err)
}
return clone
}
func (n *Context) ReadOnlyClone() Context {
clone := n.Clone()
clone.DontAutoCreate = true
return clone
}
func (n *Context) WritableClone() Context {
clone := n.Clone()
clone.DontAutoCreate = false
return clone
}