Skip to content

Commit

Permalink
Change internal mold variable structure to slice
Browse files Browse the repository at this point in the history
  • Loading branch information
0verbyte committed Sep 12, 2023
1 parent b22c752 commit b937c0d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
18 changes: 8 additions & 10 deletions internal/mold/mold.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (m *MoldTemplateVariable) HasTag(tag string) bool {

// MoldTemplate data representation for the MoldTemplate.
type MoldTemplate struct {
variables map[string]MoldTemplateVariable
variables []MoldTemplateVariable

promptReader *bufio.Reader
}
Expand All @@ -70,7 +70,7 @@ func New(r io.Reader) (*MoldTemplate, error) {
return nil, err
}

moldTemplateVariables := make(map[string]MoldTemplateVariable)
moldTemplateVariables := []MoldTemplateVariable{}
for _, moldTemplateVariable := range moldTemplate {
if moldTemplateVariable.Name == "" {
return nil, ErrMissingVariableName
Expand All @@ -92,7 +92,7 @@ func New(r io.Reader) (*MoldTemplate, error) {
}
}

moldTemplateVariables[moldTemplateVariable.Name] = moldTemplateVariable
moldTemplateVariables = append(moldTemplateVariables, moldTemplateVariable)
}

return &MoldTemplate{
Expand Down Expand Up @@ -166,19 +166,17 @@ func (m *MoldTemplate) Generate() error {

// GetVariable gets a MoldTemplateVariable by key. If the key does not exist an error will be returned.
func (m *MoldTemplate) GetVariable(key string) (*MoldTemplateVariable, error) {
if v, ok := m.variables[key]; ok {
return &v, nil
for _, v := range m.variables {
if v.Name == key {
return &v, nil
}
}
return nil, ErrEnvironmentVariableDoesNotExist
}

// GetAllVariables returns all the variables in the mold.
func (m *MoldTemplate) GetAllVariables() []MoldTemplateVariable {
variables := []MoldTemplateVariable{}
for _, v := range m.variables {
variables = append(variables, v)
}
return variables
return m.variables
}

func (m *MoldTemplate) WriteEnvironment(w Writer) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/mold/std_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (

type StdoutWriter struct{}

func (w *StdoutWriter) Write(envVars map[string]MoldTemplateVariable) error {
func (w *StdoutWriter) Write(envVars []MoldTemplateVariable) error {
lines := []string{}
for _, v := range envVars {
valueFmt := "%s"
Expand Down
2 changes: 1 addition & 1 deletion internal/mold/writer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mold

type Writer interface {
Write(map[string]MoldTemplateVariable) error
Write([]MoldTemplateVariable) error
}

0 comments on commit b937c0d

Please sign in to comment.