Skip to content
Snippets Groups Projects
Commit 6290fb568e09 authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

'variables' can now be written as a mapping instead of a list

parent 4e528e4f9fce
No related branches found
No related tags found
1 merge request!3Topic/default/variables as mapping
......@@ -16,12 +16,6 @@
"gopkg.in/yaml.v3"
)
// Variable ...
type Variable struct {
Name string
Value interface{}
}
type Sha struct {
Key string
Resource string
......@@ -65,7 +59,7 @@
Inherit string
NameSpace string
Inherits []string `yaml:",flow"`
Variables []Variable `yaml:",flow"`
Variables Variables `yaml:",flow"`
Sha []Sha `yaml:",flow"`
Charts map[string]Chart `yaml:",flow"`
Creates []Create `yaml:"create,flow"`
......@@ -320,7 +314,7 @@
}
type CmdSpec struct {
Variables []Variable
Variables Variables
Shas []*CmdSha
Charts CmdCharts
Ytt Ytt
......
......@@ -19,3 +19,10 @@
args:
- flag: --from-file
value: pipelines
vars:
db_type: postgres
dbs:
- db1
- db2
secret:
name: postgres
package runner
import (
"fmt"
"gopkg.in/yaml.v3"
)
// Variable ...
type Variable struct {
Name string
Value interface{}
}
type Variables []Variable
func (v *Variables) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode((*[]Variable)(v)); err == nil {
return nil
}
if node.Kind != yaml.MappingNode {
return fmt.Errorf("expects a mapping, got: %d", node.Kind)
}
*v = make(Variables, 0, len(node.Content)/2)
var next Variable
for i, content := range node.Content {
if i%2 == 0 {
content.Decode(&next.Name)
} else {
content.Decode(&next.Value)
*v = append(*v, next)
}
}
return nil
}
package runner
import (
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestVariablesUnmarshal(t *testing.T) {
for _, tt := range []struct {
name string
yaml string
expected Variables
err string
}{
{"legacy", `- name: v1
value: value1
- name: v2
value: 54.3
- name: nested
value:
- attr1: attr1value
attr2: 2
- attr1: otherattr1value
attr2: 4
`,
Variables{
{Name: "v1", Value: "value1"},
{Name: "v2", Value: 54.3},
{Name: "nested", Value: []interface{}{
map[string]interface{}{
"attr1": "attr1value",
"attr2": 2,
},
map[string]interface{}{
"attr1": "otherattr1value",
"attr2": 4,
},
}},
},
"",
},
{"dict", `v1: value1
v2: 54.3
nested:
- attr1: attr1value
attr2: 2
- attr1: otherattr1value
attr2: 4
`,
Variables{
{Name: "v1", Value: "value1"},
{Name: "v2", Value: 54.3},
{Name: "nested", Value: []interface{}{
map[string]interface{}{
"attr1": "attr1value",
"attr2": 2,
},
map[string]interface{}{
"attr1": "otherattr1value",
"attr2": 4,
},
}},
},
"",
},
} {
t.Run(tt.name, func(t *testing.T) {
var actual Variables
err := yaml.Unmarshal([]byte(tt.yaml), &actual)
if tt.err != "" {
require.EqualError(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, actual)
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment