Skip to content

Commit d9aadbc

Browse files
mikefarahflintwinters
authored andcommitted
Fixed scalar encoding for HCL
1 parent 386280a commit d9aadbc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pkg/yqlib/encoder_hcl.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func (he *hclEncoder) PrintLeadingContent(_ io.Writer, _ string) error {
4343

4444
func (he *hclEncoder) Encode(writer io.Writer, node *CandidateNode) error {
4545
log.Debugf("I need to encode %v", NodeToString(node))
46+
if node.Kind == ScalarNode {
47+
return writeString(writer, node.Value+"\n")
48+
}
4649

4750
f := hclwrite.NewEmptyFile()
4851
body := f.Body()
@@ -490,7 +493,7 @@ func (he *hclEncoder) encodeBlockIfMapping(body *hclwrite.Body, key string, valu
490493
// encodeNode encodes a CandidateNode directly to HCL, preserving style information
491494
func (he *hclEncoder) encodeNode(body *hclwrite.Body, node *CandidateNode) error {
492495
if node.Kind != MappingNode {
493-
return fmt.Errorf("HCL encoder expects a mapping at the root level")
496+
return fmt.Errorf("HCL encoder expects a mapping at the root level, got %v", kindToString(node.Kind))
494497
}
495498

496499
for i := 0; i < len(node.Content); i += 2 {

pkg/yqlib/hcl_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ var hclFormatScenarios = []formatScenario{
325325
expected: "# Configuration\nport = 8080\n",
326326
scenarioType: "roundtrip",
327327
},
328+
{
329+
description: "Roundtrip: extraction",
330+
skipDoc: true,
331+
input: simpleSample,
332+
expression: ".shouty_message",
333+
expected: "upper(message)\n",
334+
scenarioType: "roundtrip",
335+
},
328336
{
329337
description: "Roundtrip: With templates, functions and arithmetic",
330338
input: simpleSample,

0 commit comments

Comments
 (0)