goccy wip

This commit is contained in:
Mike Farah
2023-10-05 17:53:42 +11:00
parent b0ff65d6b7
commit d79e93d637
7 changed files with 150 additions and 5 deletions
+55
View File
@@ -0,0 +1,55 @@
package yqlib
import "github.com/goccy/go-yaml/ast"
func (o *CandidateNode) goccyDecodeIntoChild(childNode ast.Node, anchorMap map[string]*CandidateNode) (*CandidateNode, error) {
newChild := o.CreateChild()
err := newChild.UnmarshalGoccyYAML(childNode, anchorMap)
return newChild, err
}
func (o *CandidateNode) UnmarshalGoccyYAML(node ast.Node, anchorMap map[string]*CandidateNode) error {
log.Debugf("UnmarshalYAML %v", node)
log.Debugf("UnmarshalYAML %v", node.Type().String())
o.Value = node.String()
switch node.Type() {
case ast.IntegerType:
o.Kind = ScalarNode
o.Tag = "!!int"
case ast.FloatType:
o.Kind = ScalarNode
o.Tag = "!!float"
case ast.StringType:
o.Kind = ScalarNode
o.Tag = "!!str"
case ast.TagType:
o.UnmarshalGoccyYAML(node.(*ast.TagNode).Value, anchorMap)
o.Tag = node.(*ast.TagNode).Start.Value
case ast.MappingValueType:
log.Debugf("UnmarshalYAML - a mapping node")
o.Kind = MappingNode
astMapIter := node.(ast.MapNode).MapRange()
for astMapIter.Next() {
log.Debug("UnmarshalYAML map entry %v", astMapIter.Key().String())
keyNode, err := o.goccyDecodeIntoChild(astMapIter.Key(), anchorMap)
if err != nil {
return err
}
keyNode.IsMapKey = true
log.Debug("UnmarshalYAML map value %v", astMapIter.Value().String())
valueNode, err := o.goccyDecodeIntoChild(astMapIter.Value(), anchorMap)
if err != nil {
return err
}
o.Content = append(o.Content, keyNode, valueNode)
}
default:
log.Debugf("UnmarshalYAML - node idea of the type!!")
}
log.Debugf("KIND: %v", o.Kind)
return nil
}
+42
View File
@@ -0,0 +1,42 @@
//go:build !yq_noyaml
package yqlib
import (
"io"
yaml "github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
)
type goccyYamlDecoder struct {
decoder yaml.Decoder
}
func NewGoccyYAMLDecoder() Decoder {
return &goccyYamlDecoder{}
}
func (dec *goccyYamlDecoder) Init(reader io.Reader) error {
dec.decoder = *yaml.NewDecoder(reader)
return nil
}
func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) {
var ast ast.Node
err := dec.decoder.Decode(&ast)
if err != nil {
log.Debug("badasda: %v", err)
return nil, err
}
log.Debug("ASTasdasdadasd: %v", ast.Type().String())
candidateNode := &CandidateNode{}
candidateNode.UnmarshalGoccyYAML(ast, nil)
return candidateNode, nil
}
+44
View File
@@ -0,0 +1,44 @@
package yqlib
import (
"testing"
"github.com/mikefarah/yq/v4/test"
)
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: !!cat 3",
expected: "mike: !!cat 3\n",
},
}
func testGoccyYamlScenario(t *testing.T, s formatScenario) {
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewGoccyYAMLDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
}
func TestGoccyYmlFormatScenarios(t *testing.T) {
for _, tt := range goccyYamlFormatScenarios {
testGoccyYamlScenario(t, tt)
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ type expressionScenario struct {
}
func TestMain(m *testing.M) {
logging.SetLevel(logging.ERROR, "")
logging.SetLevel(logging.DEBUG, "")
Now = func() time.Time {
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
}