mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Refactoring Props encoder prefs
This commit is contained in:
parent
2866e90c49
commit
f44d47a204
@ -184,12 +184,13 @@ func configureEncoder() (yqlib.Encoder, error) {
|
|||||||
|
|
||||||
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
|
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
|
||||||
yqlib.ConfiguredXMLPreferences.Indent = indent
|
yqlib.ConfiguredXMLPreferences.Indent = indent
|
||||||
|
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
case yqlib.JSONOutputFormat:
|
case yqlib.JSONOutputFormat:
|
||||||
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
|
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
|
||||||
case yqlib.PropsOutputFormat:
|
case yqlib.PropsOutputFormat:
|
||||||
return yqlib.NewPropertiesEncoder(unwrapScalar, yqlib.ConfiguredPropertiesPreferences), nil
|
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
|
||||||
case yqlib.CSVOutputFormat:
|
case yqlib.CSVOutputFormat:
|
||||||
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
|
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
|
||||||
case yqlib.TSVOutputFormat:
|
case yqlib.TSVOutputFormat:
|
||||||
|
@ -11,14 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type propertiesEncoder struct {
|
type propertiesEncoder struct {
|
||||||
unwrapScalar bool
|
prefs PropertiesPreferences
|
||||||
prefs PropertiesPreferences
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPropertiesEncoder(unwrapScalar bool, prefs PropertiesPreferences) Encoder {
|
func NewPropertiesEncoder(prefs PropertiesPreferences) Encoder {
|
||||||
return &propertiesEncoder{
|
return &propertiesEncoder{
|
||||||
unwrapScalar: unwrapScalar,
|
prefs: prefs,
|
||||||
prefs: prefs,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +93,7 @@ func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *CandidateN
|
|||||||
switch node.Kind {
|
switch node.Kind {
|
||||||
case ScalarNode:
|
case ScalarNode:
|
||||||
var nodeValue string
|
var nodeValue string
|
||||||
if pe.unwrapScalar || !strings.Contains(node.Value, " ") {
|
if pe.prefs.UnwrapScalar || !strings.Contains(node.Value, " ") {
|
||||||
nodeValue = node.Value
|
nodeValue = node.Value
|
||||||
} else {
|
} else {
|
||||||
nodeValue = fmt.Sprintf("%q", node.Value)
|
nodeValue = fmt.Sprintf("%q", node.Value)
|
||||||
|
@ -56,7 +56,7 @@ func yamlToProps(sampleYaml string, unwrapScalar bool, separator string) string
|
|||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
writer := bufio.NewWriter(&output)
|
writer := bufio.NewWriter(&output)
|
||||||
|
|
||||||
var propsEncoder = NewPropertiesEncoder(unwrapScalar, PropertiesPreferences{KeyValueSeparator: separator})
|
var propsEncoder = NewPropertiesEncoder(PropertiesPreferences{KeyValueSeparator: separator, UnwrapScalar: unwrapScalar})
|
||||||
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -15,7 +15,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
|
|||||||
case JSONOutputFormat:
|
case JSONOutputFormat:
|
||||||
return NewJSONEncoder(indent, false, false)
|
return NewJSONEncoder(indent, false, false)
|
||||||
case PropsOutputFormat:
|
case PropsOutputFormat:
|
||||||
return NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)
|
return NewPropertiesEncoder(ConfiguredPropertiesPreferences)
|
||||||
case CSVOutputFormat:
|
case CSVOutputFormat:
|
||||||
return NewCsvEncoder(ConfiguredCsvPreferences)
|
return NewCsvEncoder(ConfiguredCsvPreferences)
|
||||||
case TSVOutputFormat:
|
case TSVOutputFormat:
|
||||||
|
@ -1,15 +1,25 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
type PropertiesPreferences struct {
|
type PropertiesPreferences struct {
|
||||||
|
UnwrapScalar bool
|
||||||
KeyValueSeparator string
|
KeyValueSeparator string
|
||||||
UseArrayBrackets bool
|
UseArrayBrackets bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultPropertiesPreferences() PropertiesPreferences {
|
func NewDefaultPropertiesPreferences() PropertiesPreferences {
|
||||||
return PropertiesPreferences{
|
return PropertiesPreferences{
|
||||||
|
UnwrapScalar: true,
|
||||||
KeyValueSeparator: " = ",
|
KeyValueSeparator: " = ",
|
||||||
UseArrayBrackets: false,
|
UseArrayBrackets: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PropertiesPreferences) Copy() PropertiesPreferences {
|
||||||
|
return PropertiesPreferences{
|
||||||
|
UnwrapScalar: p.UnwrapScalar,
|
||||||
|
KeyValueSeparator: p.KeyValueSeparator,
|
||||||
|
UseArrayBrackets: p.UseArrayBrackets,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var ConfiguredPropertiesPreferences = NewDefaultPropertiesPreferences()
|
var ConfiguredPropertiesPreferences = NewDefaultPropertiesPreferences()
|
||||||
|
@ -326,8 +326,9 @@ func documentUnwrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario)
|
|||||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props%v%v sample.yml\n```\n", useArrayBracketsFlag, useCustomSeparatorFlag))
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props%v%v sample.yml\n```\n", useArrayBracketsFlag, useCustomSeparatorFlag))
|
||||||
}
|
}
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
prefs.UnwrapScalar = true
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, prefs))))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -351,8 +352,9 @@ func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
writeOrPanic(w, "```bash\nyq -o=props --unwrapScalar=false sample.yml\n```\n")
|
writeOrPanic(w, "```bash\nyq -o=props --unwrapScalar=false sample.yml\n```\n")
|
||||||
}
|
}
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
prefs := ConfiguredPropertiesPreferences.Copy()
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(false, ConfiguredPropertiesPreferences))))
|
prefs.UnwrapScalar = false
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -402,7 +404,7 @@ func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
|
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences))))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(ConfiguredPropertiesPreferences))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentPropertyScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
|
func documentPropertyScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
@ -429,17 +431,24 @@ func TestPropertyScenarios(t *testing.T) {
|
|||||||
for _, s := range propertyScenarios {
|
for _, s := range propertyScenarios {
|
||||||
switch s.scenarioType {
|
switch s.scenarioType {
|
||||||
case "":
|
case "":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)), s.description)
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description)
|
||||||
case "decode":
|
case "decode":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
|
||||||
case "encode-wrapped":
|
case "encode-wrapped":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(false, ConfiguredPropertiesPreferences)), s.description)
|
prefs := ConfiguredPropertiesPreferences.Copy()
|
||||||
|
prefs.UnwrapScalar = false
|
||||||
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
|
||||||
case "encode-array-brackets":
|
case "encode-array-brackets":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, PropertiesPreferences{KeyValueSeparator: " = ", UseArrayBrackets: true})), s.description)
|
prefs := ConfiguredPropertiesPreferences.Copy()
|
||||||
|
prefs.KeyValueSeparator = " = "
|
||||||
|
prefs.UseArrayBrackets = true
|
||||||
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
|
||||||
case "encode-custom-separator":
|
case "encode-custom-separator":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, PropertiesPreferences{KeyValueSeparator: " :@ "})), s.description)
|
prefs := ConfiguredPropertiesPreferences.Copy()
|
||||||
|
prefs.KeyValueSeparator = " :@ "
|
||||||
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
|
||||||
case "roundtrip":
|
case "roundtrip":
|
||||||
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)), s.description)
|
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
||||||
|
Loading…
Reference in New Issue
Block a user