Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate go yaml #198

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ require (
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand All @@ -45,6 +47,8 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -66,10 +70,11 @@ require (
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapw
github.com/emicklei/go-restful/v3 v3.12.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
Expand All @@ -41,6 +43,8 @@ github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
Expand Down Expand Up @@ -76,6 +80,11 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
Expand Down Expand Up @@ -185,8 +194,12 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -205,6 +218,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
Expand Down
169 changes: 126 additions & 43 deletions internal/yaml/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import (
"fmt"

"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/token"
"strings"
)

const BoolTag token.ReservedTagKeyword = "!!bool"

Check failure on line 11 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

exported: exported const BoolTag should have comment or be unexported (revive)

// New creates a new YAML parser.
func New() Parser {
return &parser{}
Expand All @@ -16,8 +20,9 @@
return &node{
typeID: TypeIDString,
tag: "!!null",
contents: nil,
value: "",
contents: nil,
nodeMap: nil,
}
}

Expand Down Expand Up @@ -63,15 +68,16 @@
tag string
contents []Node
value string
nodeMap map[string]Node
}

func (n node) MapKeys() []string {
if n.typeID != TypeIDMap {
panic(fmt.Errorf("node is not a map, cannot call MapKeys"))
}
result := make([]string, len(n.contents)/2)
for i := 0; i < len(n.contents); i += 2 {
result[i/2] = n.contents[i].Value()
result := make([]string, 0)
for k := range n.nodeMap {
result = append(result, k)
}
return result
}
Expand All @@ -80,10 +86,8 @@
if n.typeID != TypeIDMap {
panic(fmt.Errorf("node is not a map, cannot call MapKey"))
}
for i := 0; i < len(n.contents); i += 2 {
if key == n.contents[i].Raw() {
return n.contents[i+1], true
}
if value, ok := n.nodeMap[key]; ok {
return value, true
}
return nil, false
}
Expand All @@ -93,11 +97,9 @@
case TypeIDString:
return n.value
case TypeIDMap:
result := make(map[string]any, len(n.contents)/2)
for i := 0; i < len(n.contents); i += 2 {
key := n.contents[i].Raw().(string)
value := n.contents[i+1].Raw()
result[key] = value
result := make(map[string]any, len(n.nodeMap))
for key, value := range n.nodeMap {
result[key] = value.Raw()
}
return result
case TypeIDSequence:
Expand Down Expand Up @@ -130,45 +132,126 @@
type parser struct {
}

func removeLeftLF(src string) string {

Check failure on line 135 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

func `removeLeftLF` is unused (unused)
return strings.TrimLeft(strings.TrimLeft(strings.TrimLeft(src, "\r"), "\n"), "\r\n")
}

func RemoveAllLineEndSpaces(src string) string {

Check failure on line 139 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

exported: exported function RemoveAllLineEndSpaces should have comment or be unexported (revive)
var strBuilder strings.Builder
for _, k := range strings.Split(src, "\n") {
s := strings.TrimRight(k, "\r ")
strBuilder.WriteString(s + "\n")
}
return strBuilder.String()
}

func (p parser) Parse(data []byte) (Node, error) {
var n yaml.Node
// goccy's yaml decoder has a bug involving trailing spaces before
// the line end character
out := RemoveAllLineEndSpaces(fmt.Sprintf("msg1: abc '\n'\nmsg2: def\nmsg3: ghi"))
fmt.Printf("%v\n", out)

//trimmedData := RemoveAllLineEndSpaces(string(data))

Check failure on line 154 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
var n ast.Node
if err := yaml.Unmarshal(data, &n); err != nil {
return nil, err
}
if n == nil {
return node{}, fmt.Errorf("empty YAML file given")
}
return p.transform(&n)
}

func (p parser) transform(n *yaml.Node) (Node, error) {
var t TypeID
switch n.Kind {
case 0:
return nil, fmt.Errorf("empty YAML file given")
case yaml.MappingNode:
t = TypeIDMap
case yaml.SequenceNode:
t = TypeIDSequence
case yaml.ScalarNode:
t = TypeIDString
case yaml.DocumentNode:
return p.transform(n.Content[0])
// transform converts an instance of goccy's yaml ast to the arcaflow engine's
// yaml ast

Check failure on line 166 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

Comment should end in a period (godot)
func (p parser) transform(n *ast.Node) (node, error) {
arcaNode := node{contents: make([]Node, 0), nodeMap: make(map[string]Node)}
var childArcaNode node
var err error
switch (*n).Type() {
case ast.TagType:
// tag nodes do not exist in the engine's yaml ast, so they need to be
// replaced by their child value node
tagNode := (*n).(*ast.TagNode)
tagExplicit := string(token.ReservedTagKeyword(tagNode.GetToken().Value))
childArcaNode, err = p.transform(&tagNode.Value)
if err == nil {
childArcaNode.tag = tagExplicit
arcaNode = childArcaNode
}
case ast.DocumentType:
docNode := (*n).(*ast.DocumentNode)
// recursively transform nodes in non-empty container nodes
arcaNode, err = p.transform(&docNode.Body)
case ast.MappingType:
arcaNode.typeID = TypeIDMap
arcaNode.tag = string(token.MappingTag)
mappingNode := (*n).(*ast.MappingNode)
// recursively transform nodes in non-empty container nodes
arcaNode, err = p.fillNodeMap(mappingNode.MapRange(), arcaNode)
case ast.MappingValueType:
arcaNode.typeID = TypeIDMap
arcaNode.tag = string(token.MappingTag)
mappingValueNode := (*n).(*ast.MappingValueNode)
// recursively transform nodes in non-empty container nodes
arcaNode, err = p.fillNodeMap(mappingValueNode.MapRange(), arcaNode)
case ast.SequenceType:
arcaNode.typeID = TypeIDSequence
arcaNode.tag = string(token.SequenceTag)
sequenceNode := (*n).(*ast.SequenceNode)
// recursively transform nodes in non-empty container nodes
for _, subNode := range sequenceNode.Values {
childArcaNode, err = p.transform(&subNode)

Check failure on line 204 in internal/yaml/parser.go

View workflow job for this annotation

GitHub Actions / lint and test / golangci-lint

G601: Implicit memory aliasing in for loop. (gosec)
if err != nil {
return node{}, err
}
arcaNode.contents = append(arcaNode.contents, childArcaNode)
}
case ast.LiteralType:
arcaNode.tag = string(token.StringTag)
arcaNode.typeID = TypeIDString
literalNode := (*n).(*ast.LiteralNode)
scalarNode := any(literalNode.Value).(ast.ScalarNode)
arcaNode.value = fmt.Sprintf("%v", scalarNode.GetValue())
case ast.NullType:
arcaNode.tag = string(token.NullTag)
arcaNode.value = (*n).GetToken().Value
case ast.FloatType:
arcaNode.tag = string(token.FloatTag)
arcaNode.typeID = TypeIDString
// must use token to get expected significant figures for numbers with
// trailing zeros after decimal
arcaNode.value = (*n).GetToken().Value
case ast.BoolType:
arcaNode.tag = string(BoolTag)
arcaNode.typeID = TypeIDString
scalarNode := (*n).(ast.ScalarNode)
arcaNode.value = fmt.Sprintf("%v", scalarNode.GetValue())
case ast.IntegerType:
arcaNode.tag = string(token.IntegerTag)
arcaNode.typeID = TypeIDString
scalarNode := (*n).(ast.ScalarNode)
arcaNode.value = fmt.Sprintf("%v", scalarNode.GetValue())
case ast.StringType:
arcaNode.tag = string(token.StringTag)
arcaNode.typeID = TypeIDString
scalarNode := (*n).(ast.ScalarNode)
arcaNode.value = fmt.Sprintf("%v", scalarNode.GetValue())
default:
return nil, fmt.Errorf("unsupported node type: %d", n.Kind)
return node{}, fmt.Errorf("unsupported node type: %s", (*n).Type())
}
return arcaNode, nil
}

contents := make([]Node, len(n.Content))
for i, subNode := range n.Content {
subContent, err := p.transform(subNode)
func (p parser) fillNodeMap(mapIter *ast.MapNodeIter, arcaNode node) (node, error) {
for mapIter.Next() {
subNodeKey := mapIter.Key().String()
subNode := mapIter.Value()
subContent, err := p.transform(&subNode)
if err != nil {
return nil, err
return node{}, err
}
contents[i] = subContent
arcaNode.nodeMap[subNodeKey] = subContent
}

return &node{
t,
n.Tag,
contents,
n.Value,
}, nil

return arcaNode, nil
}
Loading
Loading