2021-02-02 07:17:59 +00:00
|
|
|
package yqlib
|
|
|
|
|
2021-02-03 00:54:10 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
2021-09-05 01:07:40 +00:00
|
|
|
"fmt"
|
2022-02-14 04:37:43 +00:00
|
|
|
"time"
|
2021-02-03 00:54:10 +00:00
|
|
|
|
2021-09-05 01:07:40 +00:00
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
2021-02-03 00:54:10 +00:00
|
|
|
)
|
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
|
2022-02-14 04:37:43 +00:00
|
|
|
datetimeLayout string
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 04:00:30 +00:00
|
|
|
func (n *Context) SingleReadonlyChildContext(candidate *CandidateNode) Context {
|
|
|
|
list := list.New()
|
|
|
|
list.PushBack(candidate)
|
|
|
|
newContext := n.ChildContext(list)
|
|
|
|
newContext.DontAutoCreate = true
|
|
|
|
return newContext
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-14 04:37:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func (n *Context) ChildContext(results *list.List) Context {
|
2022-02-14 04:37:43 +00:00
|
|
|
clone := Context{DontAutoCreate: n.DontAutoCreate, datetimeLayout: n.datetimeLayout}
|
2021-10-11 01:56:27 +00:00
|
|
|
clone.Variables = make(map[string]*list.List)
|
2023-10-18 01:36:52 +00:00
|
|
|
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)
|
2021-10-11 01:56:27 +00:00
|
|
|
}
|
2023-10-18 01:36:52 +00:00
|
|
|
|
|
|
|
clone.Variables[variableKey] = variableCopyList
|
2021-02-03 00:54:10 +00:00
|
|
|
}
|
2023-10-18 01:36:52 +00:00
|
|
|
|
2021-02-03 00:54:10 +00:00
|
|
|
clone.MatchingNodes = results
|
|
|
|
return clone
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
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 {
|
2023-10-18 01:36:52 +00:00
|
|
|
|
|
|
|
clonedContent := list.New()
|
2021-09-12 06:55:55 +00:00
|
|
|
for el := n.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2023-10-18 01:11:53 +00:00
|
|
|
clonedNode := el.Value.(*CandidateNode).Copy()
|
2023-10-18 01:36:52 +00:00
|
|
|
clonedContent.PushBack(clonedNode)
|
2021-09-12 06:55:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:36:52 +00:00
|
|
|
return n.ChildContext(clonedContent)
|
2021-09-12 06:55:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 02:47:59 +00:00
|
|
|
func (n *Context) Clone() Context {
|
2023-10-18 01:36:52 +00:00
|
|
|
return n.ChildContext(n.MatchingNodes)
|
2021-02-04 02:47:59 +00:00
|
|
|
}
|
2021-05-16 04:17:13 +00:00
|
|
|
|
|
|
|
func (n *Context) ReadOnlyClone() Context {
|
|
|
|
clone := n.Clone()
|
|
|
|
clone.DontAutoCreate = true
|
|
|
|
return clone
|
|
|
|
}
|
2021-07-07 04:29:24 +00:00
|
|
|
|
|
|
|
func (n *Context) WritableClone() Context {
|
|
|
|
clone := n.Clone()
|
|
|
|
clone.DontAutoCreate = false
|
|
|
|
return clone
|
|
|
|
}
|