Allow build without json and xml support

This commit is contained in:
Anders F Björklund 2023-02-14 16:39:28 +01:00
parent ad456c1846
commit a82e10e439
16 changed files with 104 additions and 21 deletions

View File

@ -62,6 +62,9 @@ func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) {
return nil, err return nil, err
} }
yqlibDecoder, err := createDecoder(yqlibInputFormat, evaluateTogether) yqlibDecoder, err := createDecoder(yqlibInputFormat, evaluateTogether)
if yqlibDecoder == nil {
return nil, fmt.Errorf("no support for %s input format", inputFormat)
}
return yqlibDecoder, err return yqlibDecoder, err
} }
@ -108,6 +111,9 @@ func configureEncoder() (yqlib.Encoder, error) {
return nil, err return nil, err
} }
yqlibEncoder, err := createEncoder(yqlibOutputFormat) yqlibEncoder, err := createEncoder(yqlibOutputFormat)
if yqlibEncoder == nil {
return nil, fmt.Errorf("no support for %s output format", outputFormat)
}
return yqlibEncoder, err return yqlibEncoder, err
} }

View File

@ -1,3 +1,5 @@
//go:build !yq_nojson
package yqlib package yqlib
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !yq_noxml
package yqlib package yqlib
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !yq_nojson
package yqlib package yqlib
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !yq_nojson
package yqlib package yqlib
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !yq_noxml
package yqlib package yqlib
import ( import (

View File

@ -1,3 +1,5 @@
//go:build !yq_nojson
package yqlib package yqlib
import ( import (

11
pkg/yqlib/no_json.go Normal file
View File

@ -0,0 +1,11 @@
//go:build yq_nojson
package yqlib
func NewJSONDecoder() Decoder {
return nil
}
func NewJSONEncoder(indent int, colorise bool, unwrapScalar bool) Encoder {
return nil
}

11
pkg/yqlib/no_xml.go Normal file
View File

@ -0,0 +1,11 @@
//go:build yq_noxml
package yqlib
func NewXMLDecoder(prefs XmlPreferences) Decoder {
return nil
}
func NewXMLEncoder(indent int, prefs XmlPreferences) Encoder {
return nil
}

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"container/list" "container/list"
"errors"
"regexp" "regexp"
"strings" "strings"
@ -39,6 +40,9 @@ func encodeToString(candidate *CandidateNode, prefs encoderPreferences) (string,
log.Debug("printing with indent: %v", prefs.indent) log.Debug("printing with indent: %v", prefs.indent)
encoder := configureEncoder(prefs.format, prefs.indent) encoder := configureEncoder(prefs.format, prefs.indent)
if encoder == nil {
return "", errors.New("no support for output format")
}
printer := NewPrinter(encoder, NewSinglePrinterWriter(bufio.NewWriter(&output))) printer := NewPrinter(encoder, NewSinglePrinterWriter(bufio.NewWriter(&output)))
err := printer.PrintResults(candidate.AsList()) err := printer.PrintResults(candidate.AsList())
@ -127,6 +131,9 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
preferences := expressionNode.Operation.Preferences.(decoderPreferences) preferences := expressionNode.Operation.Preferences.(decoderPreferences)
decoder := createDecoder(preferences.format) decoder := createDecoder(preferences.format)
if decoder == nil {
return Context{}, errors.New("no support for input format")
}
var results = list.New() var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {

View File

@ -8,15 +8,17 @@ var prefix = "D0, P[], (doc)::a:\n cool:\n bob: dylan\n"
var encoderDecoderOperatorScenarios = []expressionScenario{ var encoderDecoderOperatorScenarios = []expressionScenario{
{ {
description: "Encode value as json string", requiresFormat: "json",
document: `{a: {cool: "thing"}}`, description: "Encode value as json string",
expression: `.b = (.a | to_json)`, document: `{a: {cool: "thing"}}`,
expression: `.b = (.a | to_json)`,
expected: []string{ expected: []string{
`D0, P[], (doc)::{a: {cool: "thing"}, b: "{\n \"cool\": \"thing\"\n}\n"} `D0, P[], (doc)::{a: {cool: "thing"}, b: "{\n \"cool\": \"thing\"\n}\n"}
`, `,
}, },
}, },
{ {
requiresFormat: "json",
description: "Encode value as json string, on one line", description: "Encode value as json string, on one line",
subdescription: "Pass in a 0 indent to print json on a single line.", subdescription: "Pass in a 0 indent to print json on a single line.",
document: `{a: {cool: "thing"}}`, document: `{a: {cool: "thing"}}`,
@ -27,6 +29,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
requiresFormat: "json",
description: "Encode value as json string, on one line shorthand", description: "Encode value as json string, on one line shorthand",
subdescription: "Pass in a 0 indent to print json on a single line.", subdescription: "Pass in a 0 indent to print json on a single line.",
document: `{a: {cool: "thing"}}`, document: `{a: {cool: "thing"}}`,
@ -37,6 +40,7 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
requiresFormat: "json",
description: "Decode a json encoded string", description: "Decode a json encoded string",
subdescription: "Keep in mind JSON is a subset of YAML. If you want idiomatic yaml, pipe through the style operator to clear out the JSON styling.", subdescription: "Keep in mind JSON is a subset of YAML. If you want idiomatic yaml, pipe through the style operator to clear out the JSON styling.",
document: `a: '{"cool":"thing"}'`, document: `a: '{"cool":"thing"}'`,
@ -193,33 +197,37 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Encode value as xml string", requiresFormat: "xml",
document: `{a: {cool: {foo: "bar", +@id: hi}}}`, description: "Encode value as xml string",
expression: `.a | to_xml`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | to_xml`,
expected: []string{ expected: []string{
"D0, P[a], (!!str)::<cool id=\"hi\">\n <foo>bar</foo>\n</cool>\n\n", "D0, P[a], (!!str)::<cool id=\"hi\">\n <foo>bar</foo>\n</cool>\n\n",
}, },
}, },
{ {
description: "Encode value as xml string on a single line", requiresFormat: "xml",
document: `{a: {cool: {foo: "bar", +@id: hi}}}`, description: "Encode value as xml string on a single line",
expression: `.a | @xml`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `.a | @xml`,
expected: []string{ expected: []string{
"D0, P[a], (!!str)::<cool id=\"hi\"><foo>bar</foo></cool>\n\n", "D0, P[a], (!!str)::<cool id=\"hi\"><foo>bar</foo></cool>\n\n",
}, },
}, },
{ {
description: "Encode value as xml string with custom indentation", requiresFormat: "xml",
document: `{a: {cool: {foo: "bar", +@id: hi}}}`, description: "Encode value as xml string with custom indentation",
expression: `{"cat": .a | to_xml(1)}`, document: `{a: {cool: {foo: "bar", +@id: hi}}}`,
expression: `{"cat": .a | to_xml(1)}`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::cat: |\n <cool id=\"hi\">\n <foo>bar</foo>\n </cool>\n", "D0, P[], (!!map)::cat: |\n <cool id=\"hi\">\n <foo>bar</foo>\n </cool>\n",
}, },
}, },
{ {
description: "Decode a xml encoded string", requiresFormat: "xml",
document: `a: "<foo>bar</foo>"`, description: "Decode a xml encoded string",
expression: `.b = (.a | from_xml)`, document: `a: "<foo>bar</foo>"`,
expression: `.b = (.a | from_xml)`,
expected: []string{ expected: []string{
"D0, P[], (doc)::a: \"<foo>bar</foo>\"\nb:\n foo: bar\n", "D0, P[], (doc)::a: \"<foo>bar</foo>\"\nb:\n foo: bar\n",
}, },
@ -303,9 +311,10 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "empty xml decode", requiresFormat: "xml",
skipDoc: true, description: "empty xml decode",
expression: `"" | @xmld`, skipDoc: true,
expression: `"" | @xmld`,
expected: []string{ expected: []string{
"D0, P[], (!!null)::\n", "D0, P[], (!!null)::\n",
}, },

View File

@ -34,6 +34,9 @@ func loadString(filename string) (*CandidateNode, error) {
} }
func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) { func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) {
if decoder == nil {
return nil, fmt.Errorf("could not load %s", filename)
}
file, err := os.Open(filename) // #nosec file, err := os.Open(filename) // #nosec
if err != nil { if err != nil {

View File

@ -74,9 +74,10 @@ var loadScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Load from XML", requiresFormat: "xml",
document: "cool: things", description: "Load from XML",
expression: `.more_stuff = load_xml("../../examples/small.xml")`, document: "cool: things",
expression: `.more_stuff = load_xml("../../examples/small.xml")`,
expected: []string{ expected: []string{
"D0, P[], (doc)::cool: things\nmore_stuff:\n this: is some xml\n", "D0, P[], (doc)::cool: things\nmore_stuff:\n this: is some xml\n",
}, },

View File

@ -28,6 +28,7 @@ type expressionScenario struct {
skipDoc bool skipDoc bool
expectedError string expectedError string
dontFormatInputForDoc bool // dont format input doc for documentation generation dontFormatInputForDoc bool // dont format input doc for documentation generation
requiresFormat string
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -103,6 +104,23 @@ func testScenario(t *testing.T, s *expressionScenario) {
return return
} }
if s.requiresFormat != "" {
format := s.requiresFormat
inputFormat, err := InputFormatFromString(format)
if err != nil {
t.Error(err)
}
if decoder := createDecoder(inputFormat); decoder == nil {
t.Skipf("no support for %s input format", format)
}
outputFormat, err := OutputFormatFromString(format)
if err != nil {
t.Error(err)
}
if encoder := configureEncoder(outputFormat, 4); encoder == nil {
t.Skipf("no support for %s output format", format)
}
}
if err != nil { if err != nil {
t.Error(fmt.Errorf("%w: %v: %v", err, s.description, s.expression)) t.Error(fmt.Errorf("%w: %v: %v", err, s.description, s.expression))
return return

View File

@ -315,6 +315,9 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
// note printDocSeparators is true, it should still not print document separators // note printDocSeparators is true, it should still not print document separators
// when outputing JSON. // when outputing JSON.
encoder := NewJSONEncoder(0, false, false) encoder := NewJSONEncoder(0, false, false)
if encoder == nil {
t.Skipf("no support for %s output format", "json")
}
printer := NewPrinter(encoder, NewSinglePrinterWriter(writer)) printer := NewPrinter(encoder, NewSinglePrinterWriter(writer))
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))

View File

@ -1,3 +1,5 @@
//go:build !yq_noxml
package yqlib package yqlib
import ( import (