mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 14:25:38 +00:00
Working around goccy
This commit is contained in:
parent
554eb998f6
commit
ed5685319d
@ -3,15 +3,20 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
yaml "github.com/goccy/go-yaml"
|
yaml "github.com/goccy/go-yaml"
|
||||||
"github.com/goccy/go-yaml/ast"
|
"github.com/goccy/go-yaml/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
type goccyYamlDecoder struct {
|
type goccyYamlDecoder struct {
|
||||||
decoder yaml.Decoder
|
decoder yaml.Decoder
|
||||||
cm yaml.CommentMap
|
cm yaml.CommentMap
|
||||||
|
bufferRead bytes.Buffer
|
||||||
|
readAnything bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGoccyYAMLDecoder() Decoder {
|
func NewGoccyYAMLDecoder() Decoder {
|
||||||
@ -20,16 +25,34 @@ func NewGoccyYAMLDecoder() Decoder {
|
|||||||
|
|
||||||
func (dec *goccyYamlDecoder) Init(reader io.Reader) error {
|
func (dec *goccyYamlDecoder) Init(reader io.Reader) error {
|
||||||
dec.cm = yaml.CommentMap{}
|
dec.cm = yaml.CommentMap{}
|
||||||
dec.decoder = *yaml.NewDecoder(reader, yaml.CommentToMap(dec.cm), yaml.UseOrderedMap())
|
dec.readAnything = false
|
||||||
|
readerToUse := io.TeeReader(reader, &dec.bufferRead)
|
||||||
|
dec.decoder = *yaml.NewDecoder(readerToUse, yaml.CommentToMap(dec.cm), yaml.UseOrderedMap())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) {
|
func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) {
|
||||||
|
|
||||||
|
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
||||||
|
|
||||||
var ast ast.Node
|
var ast ast.Node
|
||||||
|
|
||||||
err := dec.decoder.Decode(&ast)
|
err := dec.decoder.Decode(&ast)
|
||||||
if err != nil {
|
if errors.Is(err, io.EOF) && !dec.readAnything {
|
||||||
|
|
||||||
|
content := dec.bufferRead.String()
|
||||||
|
// only null fix
|
||||||
|
if content == "null" || content == "~" {
|
||||||
|
dec.readAnything = true
|
||||||
|
return createScalarNode(nil, content), nil
|
||||||
|
} else if commentLineRegEx.MatchString(content) {
|
||||||
|
dec.readAnything = true
|
||||||
|
node := createScalarNode(nil, "")
|
||||||
|
node.LeadingContent = content
|
||||||
|
return node, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -106,7 +106,7 @@ var yamlParseScenarios = []expressionScenario{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testYamlScenario(t *testing.T, s formatScenario) {
|
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) {
|
func TestYamlParseScenarios(t *testing.T) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user