From d79e93d63717ab2eecde99ff7d492106264f24e2 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 5 Oct 2023 17:53:42 +1100 Subject: [PATCH] goccy wip --- examples/data1.yaml | 3 +- go.mod | 4 +- go.sum | 5 +++ pkg/yqlib/candidate_node_goccy_yaml.go | 55 ++++++++++++++++++++++++++ pkg/yqlib/decoder_goccy_yaml.go | 42 ++++++++++++++++++++ pkg/yqlib/goccy_yaml_test.go | 44 +++++++++++++++++++++ pkg/yqlib/operators_test.go | 2 +- 7 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 pkg/yqlib/candidate_node_goccy_yaml.go create mode 100644 pkg/yqlib/decoder_goccy_yaml.go create mode 100644 pkg/yqlib/goccy_yaml_test.go diff --git a/examples/data1.yaml b/examples/data1.yaml index f292424a..6667c208 100644 --- a/examples/data1.yaml +++ b/examples/data1.yaml @@ -1,2 +1 @@ -- cat -- dog \ No newline at end of file +!things 3.1 \ No newline at end of file diff --git a/go.mod b/go.mod index cd5a5ac2..ef5c6c94 100644 --- a/go.mod +++ b/go.mod @@ -25,9 +25,9 @@ require ( require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect golang.org/x/sys v0.12.0 // indirect - golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect + golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) go 1.20 diff --git a/go.sum b/go.sum index 061c4f2c..86a98261 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A= @@ -55,12 +57,15 @@ github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7 golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0= golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 h1:6D+BvnJ/j6e222UW8s2qTSe3wGBtvo0MbVQG/c5k8RE= diff --git a/pkg/yqlib/candidate_node_goccy_yaml.go b/pkg/yqlib/candidate_node_goccy_yaml.go new file mode 100644 index 00000000..28c93782 --- /dev/null +++ b/pkg/yqlib/candidate_node_goccy_yaml.go @@ -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 +} diff --git a/pkg/yqlib/decoder_goccy_yaml.go b/pkg/yqlib/decoder_goccy_yaml.go new file mode 100644 index 00000000..3f6b7a58 --- /dev/null +++ b/pkg/yqlib/decoder_goccy_yaml.go @@ -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 +} diff --git a/pkg/yqlib/goccy_yaml_test.go b/pkg/yqlib/goccy_yaml_test.go new file mode 100644 index 00000000..2655ec14 --- /dev/null +++ b/pkg/yqlib/goccy_yaml_test.go @@ -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) + } +} diff --git a/pkg/yqlib/operators_test.go b/pkg/yqlib/operators_test.go index 56340cbb..caeb33ac 100644 --- a/pkg/yqlib/operators_test.go +++ b/pkg/yqlib/operators_test.go @@ -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) }