mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
7103b78d38
* toml wip * wip * Fixed auto parsing toml * Added build flag not to include toml * Parse toml docs and tests * minor updates
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type formatScenario struct {
|
|
input string
|
|
indent int
|
|
expression string
|
|
expected string
|
|
description string
|
|
subdescription string
|
|
skipDoc bool
|
|
scenarioType string
|
|
expectedError string
|
|
}
|
|
|
|
func processFormatScenario(s formatScenario, decoder Decoder, encoder Encoder) (string, error) {
|
|
var output bytes.Buffer
|
|
writer := bufio.NewWriter(&output)
|
|
|
|
if decoder == nil {
|
|
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
|
|
}
|
|
|
|
inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
expression := s.expression
|
|
if expression == "" {
|
|
expression = "."
|
|
}
|
|
|
|
exp, err := getExpressionParser().ParseExpression(expression)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, exp)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
printer := NewPrinter(encoder, NewSinglePrinterWriter(writer))
|
|
err = printer.PrintResults(context.MatchingNodes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
writer.Flush()
|
|
|
|
return output.String(), nil
|
|
}
|
|
|
|
func mustProcessFormatScenario(s formatScenario, decoder Decoder, encoder Encoder) string {
|
|
|
|
result, err := processFormatScenario(s, decoder, encoder)
|
|
if err != nil {
|
|
panic(fmt.Errorf("Bad scenario %v: %w", s.description, err))
|
|
}
|
|
return result
|
|
|
|
}
|