Skip to content

Commit

Permalink
Add tags to mold template variable
Browse files Browse the repository at this point in the history
  • Loading branch information
0verbyte committed Sep 12, 2023
1 parent ef7d008 commit b22c752
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/mold/mold.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,26 @@ type MoldTemplateVariable struct {
Value interface{} `yaml:"value"`
Type string `yaml:"type"`
Required bool `yaml:"required"`
Tags []string `yaml:"tags"`
}

func (m *MoldTemplateVariable) String() string {
return fmt.Sprintf("%s = %v (type=%s, required=%t)", m.Name, m.Value, m.Type, m.Required)
}

func (m *MoldTemplateVariable) AllTags() []string {
return m.Tags
}

func (m *MoldTemplateVariable) HasTag(tag string) bool {
for _, v := range m.Tags {
if v == tag {
return true
}
}
return false
}

// MoldTemplate data representation for the MoldTemplate.
type MoldTemplate struct {
variables map[string]MoldTemplateVariable
Expand Down
95 changes: 95 additions & 0 deletions internal/mold/mold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,98 @@ data`
}
}
}

func TestHasTag(t *testing.T) {
var moldTemplate = `
- name: foo
value: "bar"
type: string
required: true
tags: ["local", "dev"]
- name: debug
value: true
type: boolean
required: false
`

m, err := New(strings.NewReader(moldTemplate))
if err != nil {
t.Errorf("Failed to create new mold: %v", err)
return
}

specs := []struct {
envVar string
input string
output bool
}{
{envVar: "foo", input: "local", output: true},
{envVar: "foo", input: "production", output: false},
}

for _, spec := range specs {
envVar, err := m.GetVariable(spec.envVar)
if err != nil {
t.Errorf("Got error: %v", err)
return
}
got := envVar.HasTag(spec.input)
if got != spec.output {
t.Errorf("Failed tag lookup for %s. Expected %t, got %t", spec.input, spec.output, got)
}
}
}

func TestTagsAll(t *testing.T) {
var moldTemplate = `
- name: foo
value: "bar"
type: string
required: true
tags: ["local", "dev"]
- name: debug
value: true
type: boolean
required: false
`

m, err := New(strings.NewReader(moldTemplate))
if err != nil {
t.Errorf("Failed to create new mold: %v", err)
return
}

specs := []struct {
envVar string
output []string
}{
{envVar: "foo", output: []string{"local", "dev"}},
{envVar: "debug", output: []string{}},
}

contains := func(items []string, key string) bool {
for _, item := range items {
if key == item {
return true
}
}
return false
}

for _, spec := range specs {
envVar, err := m.GetVariable(spec.envVar)
if err != nil {
t.Errorf("Got error: %v", err)
return
}

gotTags := envVar.AllTags()
for _, expected := range spec.output {
if !contains(gotTags, expected) {
t.Errorf("Expected %s in tags, got tags %s", expected, strings.Join(gotTags, ", "))
}
}
}
}

0 comments on commit b22c752

Please sign in to comment.