From 0fceef741b3804e00aa3025e5f227f5fe574af7f Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 5 Oct 2023 20:22:53 +1100 Subject: [PATCH] goccy wip --- pkg/yqlib/candidate_node_goccy_yaml.go | 21 ++++ pkg/yqlib/goccy_yaml_test.go | 132 ++++++++++++++++--------- 2 files changed, 108 insertions(+), 45 deletions(-) diff --git a/pkg/yqlib/candidate_node_goccy_yaml.go b/pkg/yqlib/candidate_node_goccy_yaml.go index 7e8abc93..c3b45536 100644 --- a/pkg/yqlib/candidate_node_goccy_yaml.go +++ b/pkg/yqlib/candidate_node_goccy_yaml.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/goccy/go-yaml/ast" + goccyToken "github.com/goccy/go-yaml/token" ) func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, anchorMap map[string]*CandidateNode) (*CandidateNode, error) { @@ -16,6 +17,7 @@ func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, anchorMap map[s func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, anchorMap map[string]*CandidateNode) error { log.Debugf("UnmarshalYAML %v", node) log.Debugf("UnmarshalYAML %v", node.Type().String()) + log.Debugf("UnmarshalYAML Value: %v", node.String()) o.Value = node.String() switch node.Type() { @@ -28,6 +30,25 @@ func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, anchorMap map[string]* case ast.StringType: o.Kind = ScalarNode o.Tag = "!!str" + switch node.GetToken().Type { + case goccyToken.SingleQuoteType: + o.Style = SingleQuotedStyle + case goccyToken.DoubleQuoteType: + o.Style = DoubleQuotedStyle + } + o.Value = node.(*ast.StringNode).Value + log.Debugf("string value %v", node.(*ast.StringNode).Value) + case ast.LiteralType: + o.Kind = ScalarNode + o.Tag = "!!str" + o.Style = LiteralStyle + astLiteral := node.(*ast.LiteralNode) + if astLiteral.Start.Type == goccyToken.FoldedType { + o.Style = FoldedStyle + } + log.Debug("startvalue: %v ", node.(*ast.LiteralNode).Start.Value) + log.Debug("startvalue: %v ", node.(*ast.LiteralNode).Start.Type) + o.Value = astLiteral.Value.Value case ast.TagType: o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, anchorMap) o.Tag = node.(*ast.TagNode).Start.Value diff --git a/pkg/yqlib/goccy_yaml_test.go b/pkg/yqlib/goccy_yaml_test.go index fc9d65c4..327032cb 100644 --- a/pkg/yqlib/goccy_yaml_test.go +++ b/pkg/yqlib/goccy_yaml_test.go @@ -7,53 +7,95 @@ import ( ) var goccyYamlFormatScenarios = []formatScenario{ + // { + // description: "basic - 3", + // skipDoc: true, + // input: "3", + // expected: "3\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "3.1", + // expected: "3.1\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "mike: 3", + // expected: "mike: 3\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "{mike: 3}", + // expected: "{mike: 3}\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "{\nmike: 3\n}", + // expected: "{mike: 3}\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "mike: !!cat 3", + // expected: "mike: !!cat 3\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "- 3", + // expected: "- 3\n", + // }, + // { + // description: "basic - 3.1", + // skipDoc: true, + // input: "[3]", + // expected: "[3]\n", + // }, + // { + // description: "basic - plain string", + // skipDoc: true, + // input: `a: meow`, + // expected: "a: meow\n", + // }, + // { + // description: "basic - double quoted string", + // skipDoc: true, + // input: `a: "meow"`, + // expected: "a: \"meow\"\n", + // }, + // { + // description: "basic - single quoted string", + // skipDoc: true, + // input: `a: 'meow'`, + // expected: "a: 'meow'\n", + // }, + // { + // description: "basic - string block", + // skipDoc: true, + // input: "a: |\n meow\n", + // expected: "a: |\n meow\n", + // }, + // { + // description: "basic - long string", + // skipDoc: true, + // input: "a: the cute cat wrote a long sentence that wasn't wrapped at all.\n", + // expected: "a: the cute cat wrote a long sentence that wasn't wrapped at all.\n", + // }, + // { + // description: "basic - string block", + // skipDoc: true, + // input: "a: |-\n meow\n", + // expected: "a: |-\n meow\n", + // }, { - description: "basic - 3", + description: "basic - string block", skipDoc: true, - input: "3", - expected: "3\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "3.1", - expected: "3.1\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "mike: 3", - expected: "mike: 3\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "{mike: 3}", - expected: "{mike: 3}\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "{\nmike: 3\n}", - expected: "{mike: 3}\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "mike: !!cat 3", - expected: "mike: !!cat 3\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "- 3", - expected: "- 3\n", - }, - { - description: "basic - 3.1", - skipDoc: true, - input: "[3]", - expected: "[3]\n", + input: "a: >\n meow\n", + expected: "a: >\n meow\n", }, }