Working around goccy

This commit is contained in:
Mike Farah 2023-11-18 15:22:27 +11:00
parent 554eb998f6
commit ed5685319d
2 changed files with 28 additions and 5 deletions

View File

@ -3,15 +3,20 @@
package yqlib
import (
"bytes"
"errors"
"io"
"regexp"
yaml "github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
)
type goccyYamlDecoder struct {
decoder yaml.Decoder
cm yaml.CommentMap
decoder yaml.Decoder
cm yaml.CommentMap
bufferRead bytes.Buffer
readAnything bool
}
func NewGoccyYAMLDecoder() Decoder {
@ -20,16 +25,34 @@ func NewGoccyYAMLDecoder() Decoder {
func (dec *goccyYamlDecoder) Init(reader io.Reader) error {
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
}
func (dec *goccyYamlDecoder) Decode() (*CandidateNode, error) {
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
var ast ast.Node
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
}

View File

@ -106,7 +106,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) {