mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-28 01:15:35 +00:00
Working around goccy
This commit is contained in:
parent
554eb998f6
commit
ed5685319d
@ -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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user