mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 14:25:38 +00:00
adding support for --wrapScalar=false in properties encoder
This commit is contained in:
parent
b669844ef7
commit
8543161161
@ -93,7 +93,7 @@ func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
|
|||||||
case yqlib.JSONOutputFormat:
|
case yqlib.JSONOutputFormat:
|
||||||
return yqlib.NewJONEncoder(indent, colorsEnabled)
|
return yqlib.NewJONEncoder(indent, colorsEnabled)
|
||||||
case yqlib.PropsOutputFormat:
|
case yqlib.PropsOutputFormat:
|
||||||
return yqlib.NewPropertiesEncoder()
|
return yqlib.NewPropertiesEncoder(unwrapScalar)
|
||||||
case yqlib.CSVOutputFormat:
|
case yqlib.CSVOutputFormat:
|
||||||
return yqlib.NewCsvEncoder(',')
|
return yqlib.NewCsvEncoder(',')
|
||||||
case yqlib.TSVOutputFormat:
|
case yqlib.TSVOutputFormat:
|
||||||
|
|||||||
@ -12,10 +12,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type propertiesEncoder struct {
|
type propertiesEncoder struct {
|
||||||
|
unwrapScalar bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPropertiesEncoder() Encoder {
|
func NewPropertiesEncoder(unwrapScalar bool) Encoder {
|
||||||
return &propertiesEncoder{}
|
return &propertiesEncoder{
|
||||||
|
unwrapScalar: unwrapScalar,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pe *propertiesEncoder) CanHandleAliases() bool {
|
func (pe *propertiesEncoder) CanHandleAliases() bool {
|
||||||
@ -75,7 +78,13 @@ func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yaml.Node,
|
|||||||
p.SetComment(path, headAndLineComment(node))
|
p.SetComment(path, headAndLineComment(node))
|
||||||
switch node.Kind {
|
switch node.Kind {
|
||||||
case yaml.ScalarNode:
|
case yaml.ScalarNode:
|
||||||
_, _, err := p.Set(path, node.Value)
|
var nodeValue string
|
||||||
|
if pe.unwrapScalar || !strings.Contains(node.Value, " ") {
|
||||||
|
nodeValue = node.Value
|
||||||
|
} else {
|
||||||
|
nodeValue = fmt.Sprintf("%q", node.Value)
|
||||||
|
}
|
||||||
|
_, _, err := p.Set(path, nodeValue)
|
||||||
return err
|
return err
|
||||||
case yaml.DocumentNode:
|
case yaml.DocumentNode:
|
||||||
return pe.doEncode(p, node.Content[0], path)
|
return pe.doEncode(p, node.Content[0], path)
|
||||||
|
|||||||
@ -9,11 +9,11 @@ import (
|
|||||||
"github.com/mikefarah/yq/v4/test"
|
"github.com/mikefarah/yq/v4/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func yamlToProps(sampleYaml string) string {
|
func yamlToProps(sampleYaml string, unwrapScalar bool) string {
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
writer := bufio.NewWriter(&output)
|
writer := bufio.NewWriter(&output)
|
||||||
|
|
||||||
var propsEncoder = NewPropertiesEncoder()
|
var propsEncoder = NewPropertiesEncoder(unwrapScalar)
|
||||||
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
|
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -28,51 +28,100 @@ func yamlToProps(sampleYaml string) string {
|
|||||||
return strings.TrimSuffix(output.String(), "\n")
|
return strings.TrimSuffix(output.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPropertiesEncoderSimple(t *testing.T) {
|
func TestPropertiesEncoderSimple_Unwrapped(t *testing.T) {
|
||||||
var sampleYaml = `a: 'bob cool'`
|
var sampleYaml = `a: 'bob cool'`
|
||||||
|
|
||||||
var expectedProps = `a = bob cool`
|
var expectedProps = `a = bob cool`
|
||||||
var actualProps = yamlToProps(sampleYaml)
|
var actualProps = yamlToProps(sampleYaml, true)
|
||||||
test.AssertResult(t, expectedProps, actualProps)
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPropertiesEncoderSimpleWithComments(t *testing.T) {
|
func TestPropertiesEncoderSimple_Wrapped(t *testing.T) {
|
||||||
|
var sampleYaml = `a: 'bob cool'`
|
||||||
|
|
||||||
|
var expectedProps = `a = "bob cool"`
|
||||||
|
var actualProps = yamlToProps(sampleYaml, false)
|
||||||
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesEncoderSimpleWithComments_Unwrapped(t *testing.T) {
|
||||||
var sampleYaml = `a: 'bob cool' # line`
|
var sampleYaml = `a: 'bob cool' # line`
|
||||||
|
|
||||||
var expectedProps = `# line
|
var expectedProps = `# line
|
||||||
a = bob cool`
|
a = bob cool`
|
||||||
var actualProps = yamlToProps(sampleYaml)
|
var actualProps = yamlToProps(sampleYaml, true)
|
||||||
test.AssertResult(t, expectedProps, actualProps)
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPropertiesEncoderDeep(t *testing.T) {
|
func TestPropertiesEncoderSimpleWithComments_Wrapped(t *testing.T) {
|
||||||
|
var sampleYaml = `a: 'bob cool' # line`
|
||||||
|
|
||||||
|
var expectedProps = `# line
|
||||||
|
a = "bob cool"`
|
||||||
|
var actualProps = yamlToProps(sampleYaml, false)
|
||||||
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesEncoderDeep_Unwrapped(t *testing.T) {
|
||||||
var sampleYaml = `a:
|
var sampleYaml = `a:
|
||||||
b: "bob cool"
|
b: "bob cool"
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedProps = `a.b = bob cool`
|
var expectedProps = `a.b = bob cool`
|
||||||
var actualProps = yamlToProps(sampleYaml)
|
var actualProps = yamlToProps(sampleYaml, true)
|
||||||
test.AssertResult(t, expectedProps, actualProps)
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPropertiesEncoderDeepWithComments(t *testing.T) {
|
func TestPropertiesEncoderDeep_Wrapped(t *testing.T) {
|
||||||
|
var sampleYaml = `a:
|
||||||
|
b: "bob cool"
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedProps = `a.b = "bob cool"`
|
||||||
|
var actualProps = yamlToProps(sampleYaml, false)
|
||||||
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesEncoderDeepWithComments_Unwrapped(t *testing.T) {
|
||||||
var sampleYaml = `a: # a thing
|
var sampleYaml = `a: # a thing
|
||||||
b: "bob cool" # b thing
|
b: "bob cool" # b thing
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedProps = `# b thing
|
var expectedProps = `# b thing
|
||||||
a.b = bob cool`
|
a.b = bob cool`
|
||||||
var actualProps = yamlToProps(sampleYaml)
|
var actualProps = yamlToProps(sampleYaml, true)
|
||||||
test.AssertResult(t, expectedProps, actualProps)
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPropertiesEncoderArray(t *testing.T) {
|
func TestPropertiesEncoderDeepWithComments_Wrapped(t *testing.T) {
|
||||||
|
var sampleYaml = `a: # a thing
|
||||||
|
b: "bob cool" # b thing
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedProps = `# b thing
|
||||||
|
a.b = "bob cool"`
|
||||||
|
var actualProps = yamlToProps(sampleYaml, false)
|
||||||
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesEncoderArray_Unwrapped(t *testing.T) {
|
||||||
var sampleYaml = `a:
|
var sampleYaml = `a:
|
||||||
b: [{c: dog}, {c: cat}]
|
b: [{c: dog}, {c: cat}]
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedProps = `a.b.0.c = dog
|
var expectedProps = `a.b.0.c = dog
|
||||||
a.b.1.c = cat`
|
a.b.1.c = cat`
|
||||||
var actualProps = yamlToProps(sampleYaml)
|
var actualProps = yamlToProps(sampleYaml, true)
|
||||||
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesEncoderArray_Wrapped(t *testing.T) {
|
||||||
|
var sampleYaml = `a:
|
||||||
|
b: [{c: dog named jim}, {c: cat named jam}]
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedProps = `a.b.0.c = "dog named jim"
|
||||||
|
a.b.1.c = "cat named jam"`
|
||||||
|
var actualProps = yamlToProps(sampleYaml, false)
|
||||||
test.AssertResult(t, expectedProps, actualProps)
|
test.AssertResult(t, expectedProps, actualProps)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
|
|||||||
case JSONOutputFormat:
|
case JSONOutputFormat:
|
||||||
return NewJONEncoder(indent, false)
|
return NewJONEncoder(indent, false)
|
||||||
case PropsOutputFormat:
|
case PropsOutputFormat:
|
||||||
return NewPropertiesEncoder()
|
return NewPropertiesEncoder(true)
|
||||||
case CSVOutputFormat:
|
case CSVOutputFormat:
|
||||||
return NewCsvEncoder(',')
|
return NewCsvEncoder(',')
|
||||||
case TSVOutputFormat:
|
case TSVOutputFormat:
|
||||||
|
|||||||
@ -128,7 +128,7 @@ func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
}
|
}
|
||||||
writeOrPanic(w, "will output\n")
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder())))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -178,7 +178,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", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder())))
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
@ -201,9 +201,9 @@ func TestPropertyScenarios(t *testing.T) {
|
|||||||
if s.scenarioType == "decode" {
|
if s.scenarioType == "decode" {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, true, true)), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, true, true)), s.description)
|
||||||
} else if s.scenarioType == "roundtrip" {
|
} else if s.scenarioType == "roundtrip" {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder()), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true)), s.description)
|
||||||
} else {
|
} else {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder()), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true)), s.description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
genericScenarios := make([]interface{}, len(propertyScenarios))
|
genericScenarios := make([]interface{}, len(propertyScenarios))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user