2021-02-02 07:17:59 +00:00
|
|
|
package yqlib
|
|
|
|
|
2021-02-03 00:54:10 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
)
|
2021-02-02 07:17:59 +00:00
|
|
|
|
|
|
|
type Context struct {
|
2021-02-03 00:54:10 +00:00
|
|
|
MatchingNodes *list.List
|
|
|
|
Variables map[string]*list.List
|
|
|
|
DontAutoCreate bool
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func (n *Context) ChildContext(results *list.List) Context {
|
2021-02-03 00:54:10 +00:00
|
|
|
clone := Context{}
|
|
|
|
err := copier.Copy(&clone, n)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error cloning context :(")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
clone.MatchingNodes = results
|
|
|
|
return clone
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
2021-02-04 02:47:59 +00:00
|
|
|
|
|
|
|
func (n *Context) Clone() Context {
|
|
|
|
clone := Context{}
|
|
|
|
err := copier.Copy(&clone, n)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error cloning context :(")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return clone
|
|
|
|
}
|