Skip to content

Commit f44d47a

Browse files
committed
Refactoring Props encoder prefs
1 parent 2866e90 commit f44d47a

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

cmd/utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,13 @@ func configureEncoder() (yqlib.Encoder, error) {
184184

185185
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
186186
yqlib.ConfiguredXMLPreferences.Indent = indent
187+
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
187188

188189
switch format {
189190
case yqlib.JSONOutputFormat:
190191
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
191192
case yqlib.PropsOutputFormat:
192-
return yqlib.NewPropertiesEncoder(unwrapScalar, yqlib.ConfiguredPropertiesPreferences), nil
193+
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
193194
case yqlib.CSVOutputFormat:
194195
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
195196
case yqlib.TSVOutputFormat:

pkg/yqlib/encoder_properties.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ import (
1111
)
1212

1313
type propertiesEncoder struct {
14-
unwrapScalar bool
15-
prefs PropertiesPreferences
14+
prefs PropertiesPreferences
1615
}
1716

18-
func NewPropertiesEncoder(unwrapScalar bool, prefs PropertiesPreferences) Encoder {
17+
func NewPropertiesEncoder(prefs PropertiesPreferences) Encoder {
1918
return &propertiesEncoder{
20-
unwrapScalar: unwrapScalar,
21-
prefs: prefs,
19+
prefs: prefs,
2220
}
2321
}
2422

@@ -95,7 +93,7 @@ func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *CandidateN
9593
switch node.Kind {
9694
case ScalarNode:
9795
var nodeValue string
98-
if pe.unwrapScalar || !strings.Contains(node.Value, " ") {
96+
if pe.prefs.UnwrapScalar || !strings.Contains(node.Value, " ") {
9997
nodeValue = node.Value
10098
} else {
10199
nodeValue = fmt.Sprintf("%q", node.Value)

pkg/yqlib/encoder_properties_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func yamlToProps(sampleYaml string, unwrapScalar bool, separator string) string
5656
var output bytes.Buffer
5757
writer := bufio.NewWriter(&output)
5858

59-
var propsEncoder = NewPropertiesEncoder(unwrapScalar, PropertiesPreferences{KeyValueSeparator: separator})
59+
var propsEncoder = NewPropertiesEncoder(PropertiesPreferences{KeyValueSeparator: separator, UnwrapScalar: unwrapScalar})
6060
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
6161
if err != nil {
6262
panic(err)

pkg/yqlib/operator_encoder_decoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
1515
case JSONOutputFormat:
1616
return NewJSONEncoder(indent, false, false)
1717
case PropsOutputFormat:
18-
return NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)
18+
return NewPropertiesEncoder(ConfiguredPropertiesPreferences)
1919
case CSVOutputFormat:
2020
return NewCsvEncoder(ConfiguredCsvPreferences)
2121
case TSVOutputFormat:

pkg/yqlib/properties.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package yqlib
22

33
type PropertiesPreferences struct {
4+
UnwrapScalar bool
45
KeyValueSeparator string
56
UseArrayBrackets bool
67
}
78

89
func NewDefaultPropertiesPreferences() PropertiesPreferences {
910
return PropertiesPreferences{
11+
UnwrapScalar: true,
1012
KeyValueSeparator: " = ",
1113
UseArrayBrackets: false,
1214
}
1315
}
1416

17+
func (p *PropertiesPreferences) Copy() PropertiesPreferences {
18+
return PropertiesPreferences{
19+
UnwrapScalar: p.UnwrapScalar,
20+
KeyValueSeparator: p.KeyValueSeparator,
21+
UseArrayBrackets: p.UseArrayBrackets,
22+
}
23+
}
24+
1525
var ConfiguredPropertiesPreferences = NewDefaultPropertiesPreferences()

pkg/yqlib/properties_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,9 @@ func documentUnwrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario)
326326
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props%v%v sample.yml\n```\n", useArrayBracketsFlag, useCustomSeparatorFlag))
327327
}
328328
writeOrPanic(w, "will output\n")
329+
prefs.UnwrapScalar = true
329330

330-
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, prefs))))
331+
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs))))
331332
}
332333

333334
func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
@@ -351,8 +352,9 @@ func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
351352
writeOrPanic(w, "```bash\nyq -o=props --unwrapScalar=false sample.yml\n```\n")
352353
}
353354
writeOrPanic(w, "will output\n")
354-
355-
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(false, ConfiguredPropertiesPreferences))))
355+
prefs := ConfiguredPropertiesPreferences.Copy()
356+
prefs.UnwrapScalar = false
357+
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs))))
356358
}
357359

358360
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
@@ -402,7 +404,7 @@ func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
402404

403405
writeOrPanic(w, "will output\n")
404406

405-
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences))))
407+
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(ConfiguredPropertiesPreferences))))
406408
}
407409

408410
func documentPropertyScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
@@ -429,17 +431,24 @@ func TestPropertyScenarios(t *testing.T) {
429431
for _, s := range propertyScenarios {
430432
switch s.scenarioType {
431433
case "":
432-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)), s.description)
434+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description)
433435
case "decode":
434436
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
435437
case "encode-wrapped":
436-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(false, ConfiguredPropertiesPreferences)), s.description)
438+
prefs := ConfiguredPropertiesPreferences.Copy()
439+
prefs.UnwrapScalar = false
440+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
437441
case "encode-array-brackets":
438-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, PropertiesPreferences{KeyValueSeparator: " = ", UseArrayBrackets: true})), s.description)
442+
prefs := ConfiguredPropertiesPreferences.Copy()
443+
prefs.KeyValueSeparator = " = "
444+
prefs.UseArrayBrackets = true
445+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
439446
case "encode-custom-separator":
440-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(true, PropertiesPreferences{KeyValueSeparator: " :@ "})), s.description)
447+
prefs := ConfiguredPropertiesPreferences.Copy()
448+
prefs.KeyValueSeparator = " :@ "
449+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewPropertiesEncoder(prefs)), s.description)
441450
case "roundtrip":
442-
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true, ConfiguredPropertiesPreferences)), s.description)
451+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(ConfiguredPropertiesPreferences)), s.description)
443452

444453
default:
445454
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))

0 commit comments

Comments
 (0)