From e4894528a3651f933bc20ecf1e6e09a8aa865585 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 10 Oct 2023 14:28:53 +1100 Subject: [PATCH] goccy wip --- pkg/yqlib/candidate_node_goccy_yaml.go | 7 ++++ pkg/yqlib/decoder_goccy_yaml.go | 2 +- pkg/yqlib/goccy_yaml_test.go | 54 +++++++++++++++++++++----- pkg/yqlib/yaml_test.go | 14 +++---- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/pkg/yqlib/candidate_node_goccy_yaml.go b/pkg/yqlib/candidate_node_goccy_yaml.go index ea14494f..bed150b5 100644 --- a/pkg/yqlib/candidate_node_goccy_yaml.go +++ b/pkg/yqlib/candidate_node_goccy_yaml.go @@ -58,6 +58,13 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, cm yaml.CommentMap) er case ast.FloatType: o.Kind = ScalarNode o.Tag = "!!float" + case ast.BoolType: + o.Kind = ScalarNode + o.Tag = "!!bool" + case ast.NullType: + o.Kind = ScalarNode + o.Tag = "!!null" + o.Value = node.GetToken().Value case ast.StringType: o.Kind = ScalarNode o.Tag = "!!str" diff --git a/pkg/yqlib/decoder_goccy_yaml.go b/pkg/yqlib/decoder_goccy_yaml.go index 5b7aec1b..c775a9f3 100644 --- a/pkg/yqlib/decoder_goccy_yaml.go +++ b/pkg/yqlib/decoder_goccy_yaml.go @@ -20,7 +20,7 @@ func NewGoccyYAMLDecoder() Decoder { func (dec *goccyYamlDecoder) Init(reader io.Reader) error { dec.cm = yaml.CommentMap{} - dec.decoder = *yaml.NewDecoder(reader, yaml.CommentToMap(dec.cm)) + dec.decoder = *yaml.NewDecoder(reader, yaml.CommentToMap(dec.cm), yaml.UseOrderedMap()) return nil } diff --git a/pkg/yqlib/goccy_yaml_test.go b/pkg/yqlib/goccy_yaml_test.go index 1410c435..ecdd526c 100644 --- a/pkg/yqlib/goccy_yaml_test.go +++ b/pkg/yqlib/goccy_yaml_test.go @@ -109,18 +109,54 @@ var goccyYamlFormatScenarios = []formatScenario{ // input: "# head comment\na: #line comment\n meow\n", // expected: "# head comment\na: meow #line comment\n", // go-yaml does this // }, + // { + // description: "basic - foot comment", + // skipDoc: true, + // input: "a: meow\n# foot comment\n", + // expected: "a: meow\n# foot comment\n", + // }, + // { + // description: "basic - foot comment", + // skipDoc: true, + // input: "a: meow\nb: woof\n# foot comment\n", + // expected: "a: meow\nb: woof\n# foot comment\n", + // }, + // { + // description: "basic - boolean", + // skipDoc: true, + // input: "true\n", + // expected: "true\n", + // }, + // { + // description: "basic - null", + // skipDoc: true, + // input: "a: null\n", + // expected: "a: null\n", + // }, + // { + // description: "basic - ~", + // skipDoc: true, + // input: "a: ~\n", + // expected: "a: ~\n", + // }, + // { + // description: "basic - ~", + // skipDoc: true, + // input: "null\n", + // expected: "null\n", + // }, { - description: "basic - foot comment", skipDoc: true, - input: "a: meow\n# foot comment\n", - expected: "a: meow\n# foot comment\n", - }, - { - description: "basic - foot comment", - skipDoc: true, - input: "a: meow\nb: woof\n# foot comment\n", - expected: "a: meow\nb: woof\n# foot comment\n", + description: "trailing comment", + input: "test:", + expected: "test:", }, + // { + // skipDoc: true, + // description: "trailing comment", + // input: "test:\n# this comment will be removed", + // expected: "test:\n# this comment will be removed", + // }, } func testGoccyYamlScenario(t *testing.T, s formatScenario) { diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index d58f0e68..6b51de58 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -7,12 +7,12 @@ import ( ) var yamlFormatScenarios = []formatScenario{ - { - description: "basic - null", - skipDoc: true, - input: "null", - expected: "null\n", - }, + // { + // description: "basic - null", + // skipDoc: true, + // input: "null", + // expected: "null\n", + // }, // { // description: "basic - ~", // skipDoc: true, @@ -86,7 +86,7 @@ var yamlParseScenarios = []expressionScenario{ } func testYamlScenario(t *testing.T, s formatScenario) { - test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) + test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewGoccyYAMLDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description) } func TestYamlParseScenarios(t *testing.T) {