This commit is contained in:
Mike Farah 2023-04-13 14:53:14 +10:00
parent 27fa4e10b8
commit 2de69a19f5
2 changed files with 13 additions and 9 deletions

View File

@ -29,6 +29,7 @@ func (o *CandidateNode) MarshalJSON() ([]byte, error) {
return buf.Bytes(), err
}
err = enc.Encode(value)
log.Debugf("cool I handled that")
return buf.Bytes(), err
case MappingNode:
log.Debugf("MarshalJSON MappingNode")
@ -46,11 +47,13 @@ func (o *CandidateNode) MarshalJSON() ([]byte, error) {
}
}
buf.WriteByte('}')
return buf.Bytes(), nil
case SequenceNode:
log.Debugf("MarshalJSON SequenceNode")
err := enc.Encode(o.Content)
return buf.Bytes(), err
default:
err := enc.Encode(nil)
return buf.Bytes(), err
}
log.Debug("none of those things?")
return buf.Bytes(), nil
}

View File

@ -11,7 +11,8 @@ import (
"github.com/mikefarah/yq/v4/test"
)
func yamlToJSON(sampleYaml string, indent int) string {
func yamlToJSON(t *testing.T, sampleYaml string, indent int) string {
t.Helper()
var output bytes.Buffer
writer := bufio.NewWriter(&output)
@ -22,7 +23,7 @@ func yamlToJSON(sampleYaml string, indent int) string {
}
node := inputs.Front().Value.(*CandidateNode)
log.Debugf("%v", NodeToString(node))
log.Debugf("Content[0] %v", NodeToString(node.Content[0]))
// log.Debugf("Content[0] %v", NodeToString(node.Content[0]))
err = jsonEncoder.Encode(writer, node)
if err != nil {
@ -49,31 +50,31 @@ banana:
}
]
}`
var actualJSON = yamlToJSON(sampleYaml, 2)
var actualJSON = yamlToJSON(t, sampleYaml, 2)
test.AssertResult(t, expectedJSON, actualJSON)
}
func TestJsonNullInArray(t *testing.T) {
var sampleYaml = `[null]`
var actualJSON = yamlToJSON(sampleYaml, 0)
var actualJSON = yamlToJSON(t, sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJSON)
}
func TestJsonNull(t *testing.T) {
var sampleYaml = `null`
var actualJSON = yamlToJSON(sampleYaml, 0)
var actualJSON = yamlToJSON(t, sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJSON)
}
func TestJsonNullInObject(t *testing.T) {
var sampleYaml = `{x: null}`
var actualJSON = yamlToJSON(sampleYaml, 0)
var actualJSON = yamlToJSON(t, sampleYaml, 0)
test.AssertResult(t, `{"x":null}`, actualJSON)
}
func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) {
var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"`
var expectedJSON = `{"build":"( ./lint && ./format && ./compile ) < src.code"}`
var actualJSON = yamlToJSON(sampleYaml, 0)
var actualJSON = yamlToJSON(t, sampleYaml, 0)
test.AssertResult(t, expectedJSON, actualJSON)
}