diff --git a/pkg/parser/node/node.go b/pkg/parser/node/node.go index dad750e0..d290fb90 100644 --- a/pkg/parser/node/node.go +++ b/pkg/parser/node/node.go @@ -84,9 +84,42 @@ func (node *Node) Deduplicate() { node.Children = unique } +func (node *Node) MergeListOfMapsToSingleMap() { + _, ok := node.Value.(*ListValue) + if !ok { + return + } + + var virtualNode Node + + for _, child := range node.Children { + if _, ok := child.Value.(*MapValue); !ok { + return + } + + virtualNode.MergeFrom(child) + } + + node.Children = virtualNode.Children + + // Rewrite parents from virtualNode to node + for _, child := range virtualNode.Children { + child.Parent = node + } + + // This is now a map + node.Value = &MapValue{} +} + func (node *Node) MergeFrom(other *Node) { node.Name = other.Name + // Special treatment for environment variables since they can also be represented as a list of maps + if node.Name == "env" || node.Name == "environment" { + node.MergeListOfMapsToSingleMap() + other.MergeListOfMapsToSingleMap() + } + if reflect.TypeOf(node.Value) != reflect.TypeOf(other.Value) { node.Value = other.Value diff --git a/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.json b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.json new file mode 100644 index 00000000..62a69a39 --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.json @@ -0,0 +1,31 @@ +[ + { + "commands": [ + { + "cloneInstruction": {}, + "name": "clone" + } + ], + "environment": { + "A": "B", + "C": "D", + "CIRRUS_OS": "linux" + }, + "instance": { + "@type": "type.googleapis.com/org.cirruslabs.ci.services.cirruscigrpc.ContainerInstance", + "cpu": 2, + "image": "golang:latest", + "memory": 4096 + }, + "metadata": { + "properties": { + "allow_failures": "false", + "experimental": "false", + "indexWithinBuild": "0", + "timeout_in": "3600", + "trigger_type": "AUTOMATIC" + } + }, + "name": "main" + } +] diff --git a/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.yml b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.yml new file mode 100644 index 00000000..24b0941e --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-list.yml @@ -0,0 +1,9 @@ +container: + image: golang:latest + +env: + - A: B + +task: + env: + C: D diff --git a/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.json b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.json new file mode 100644 index 00000000..62a69a39 --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.json @@ -0,0 +1,31 @@ +[ + { + "commands": [ + { + "cloneInstruction": {}, + "name": "clone" + } + ], + "environment": { + "A": "B", + "C": "D", + "CIRRUS_OS": "linux" + }, + "instance": { + "@type": "type.googleapis.com/org.cirruslabs.ci.services.cirruscigrpc.ContainerInstance", + "cpu": 2, + "image": "golang:latest", + "memory": 4096 + }, + "metadata": { + "properties": { + "allow_failures": "false", + "experimental": "false", + "indexWithinBuild": "0", + "timeout_in": "3600", + "trigger_type": "AUTOMATIC" + } + }, + "name": "main" + } +] diff --git a/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.yml b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.yml new file mode 100644 index 00000000..7d1b97ea --- /dev/null +++ b/pkg/parser/testdata/via-rpc/new-collectible-env-merging-to-map.yml @@ -0,0 +1,9 @@ +container: + image: golang:latest + +env: + A: B + +task: + env: + - C: D