2021-11-16 04:29:16 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-11-25 01:05:56 +00:00
|
|
|
var LoadYamlPreferences = YamlPreferences{
|
|
|
|
LeadingContentPreProcessing: false,
|
|
|
|
PrintDocSeparators: true,
|
|
|
|
UnwrapScalar: true,
|
|
|
|
EvaluateTogether: false,
|
|
|
|
}
|
|
|
|
|
2021-11-16 04:29:16 +00:00
|
|
|
type loadPrefs struct {
|
|
|
|
loadAsString bool
|
2021-12-21 04:02:07 +00:00
|
|
|
decoder Decoder
|
2021-11-16 04:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadString(filename string) (*CandidateNode, error) {
|
2021-11-25 09:24:51 +00:00
|
|
|
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
|
2021-11-16 04:29:16 +00:00
|
|
|
// and ensuring that it's not possible to give a path to a file outside that directory.
|
|
|
|
|
2022-01-21 08:50:53 +00:00
|
|
|
filebytes, err := os.ReadFile(filename) // #nosec
|
2021-11-16 04:29:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: string(filebytes)}, nil
|
2021-11-16 04:29:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) {
|
2023-03-01 02:19:06 +00:00
|
|
|
if decoder == nil {
|
|
|
|
return nil, fmt.Errorf("could not load %s", filename)
|
|
|
|
}
|
2021-11-16 04:29:16 +00:00
|
|
|
|
|
|
|
file, err := os.Open(filename) // #nosec
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reader := bufio.NewReader(file)
|
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
documents, err := readDocuments(reader, filename, 0, decoder)
|
2021-11-16 04:29:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if documents.Len() == 0 {
|
|
|
|
// return null candidate
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{Kind: ScalarNode, Tag: "!!null"}, nil
|
2021-11-16 04:29:16 +00:00
|
|
|
} else if documents.Len() == 1 {
|
2022-11-25 01:05:56 +00:00
|
|
|
candidate := documents.Front().Value.(*CandidateNode)
|
|
|
|
return candidate, nil
|
2021-11-16 04:29:16 +00:00
|
|
|
|
|
|
|
}
|
2024-01-11 02:17:34 +00:00
|
|
|
sequenceNode := &CandidateNode{Kind: SequenceNode}
|
|
|
|
for doc := documents.Front(); doc != nil; doc = doc.Next() {
|
|
|
|
sequenceNode.AddChild(doc.Value.(*CandidateNode))
|
|
|
|
}
|
|
|
|
return sequenceNode, nil
|
2021-11-16 04:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadYamlOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("loadYamlOperator")
|
|
|
|
|
|
|
|
loadPrefs := expressionNode.Operation.Preferences.(loadPrefs)
|
|
|
|
|
|
|
|
// need to evaluate the 1st parameter against the context
|
|
|
|
// and return the data accordingly.
|
|
|
|
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
|
2021-11-16 04:29:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
if rhs.MatchingNodes.Front() == nil {
|
2023-10-18 01:11:53 +00:00
|
|
|
return Context{}, fmt.Errorf("filename expression returned nil")
|
2021-11-16 04:29:16 +00:00
|
|
|
}
|
|
|
|
nameCandidateNode := rhs.MatchingNodes.Front().Value.(*CandidateNode)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
filename := nameCandidateNode.Value
|
2021-11-16 04:29:16 +00:00
|
|
|
|
|
|
|
var contentsCandidate *CandidateNode
|
|
|
|
|
|
|
|
if loadPrefs.loadAsString {
|
|
|
|
contentsCandidate, err = loadString(filename)
|
|
|
|
} else {
|
2021-12-21 04:02:07 +00:00
|
|
|
contentsCandidate, err = loadYaml(filename, loadPrefs.decoder)
|
2021-11-16 04:29:16 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, fmt.Errorf("Failed to load %v: %w", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results.PushBack(contentsCandidate)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|