Skip to content

Commit

Permalink
Made variables not copy on usage and cleaned up merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartendeKruijf committed Mar 11, 2024
1 parent 33c5fb8 commit af6e7cc
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions models/cacao/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,36 @@ import (
// Allows passing in multiple variables at once
func NewVariables(new ...Variable) Variables {
variables := make(Variables)
variables.InsertRange(new...)
for _, variable := range new {
variables.Insert(variable)
}
return variables
}

// Insert a variable
//
// Returns true if the variable was inserted
func (variables Variables) Insert(new Variable) bool {
if _, found := variables[new.Name]; found {
func (variables *Variables) Insert(new Variable) bool {
if _, found := (*variables)[new.Name]; found {
return false
}
variables[new.Name] = new
(*variables)[new.Name] = new
return true
}

// Insert or replace a variable
//
// Returns true if the variable was replaced
func (variables Variables) InsertOrReplace(new Variable) bool {
_, found := variables[new.Name]
variables[new.Name] = new
func (variables *Variables) InsertOrReplace(new Variable) bool {
_, found := (*variables)[new.Name]
(*variables)[new.Name] = new
return found
}

// Insert multiple variables at once
func (variables Variables) InsertRange(new ...Variable) {
for _, newVar := range new {
variables.Insert(newVar)
// Insert variables map at once into the base and keep base variables in favor of source duplicates
func (variables *Variables) InsertRange(source Variables) {
for _, variable := range source {
variables.Insert(variable)
}
}

Expand All @@ -49,29 +51,22 @@ func (variables Variables) Find(key string) (Variable, bool) {
return val, ok
}

// Construct a Replacer for string interpolation
// Interpolate variable references into a target string
//
// Variable substitution is performed according to the CACAO spec,
// which states `__var__:value` is replaced with the value of `__var__`.
func (variables Variables) Replacer() *strings.Replacer {
// Returns the Interpolated string with variables values available in the map
func (variables *Variables) Interpolate(input string) string {
replacements := make([]string, 0)
for key, value := range variables {
replacementKey := fmt.Sprintf("%s:value", key)
for key, value := range *variables {
replacementKey := fmt.Sprint(key, ":value")
replacements = append(replacements, replacementKey, value.Value)
}
return strings.NewReplacer(replacements...)
}

// Interpolate variable references in a string
func (variables Variables) Interpolate(s string) string {
replacer := variables.Replacer()
return replacer.Replace(s)
return strings.NewReplacer(replacements...).Replace(input)
}

// Select a subset of variables from the map
//
// Unknown keys are ignored.
func (variables Variables) Select(keys []string) Variables {
func (variables *Variables) Select(keys []string) Variables {
newVariables := NewVariables()

for _, key := range keys {
Expand All @@ -83,16 +78,9 @@ func (variables Variables) Select(keys []string) Variables {
return newVariables
}

// Merge two maps of Cacao variables
func (variables Variables) Merge(new Variables) Variables {
combined := NewVariables()
for _, value := range variables {
combined.Insert(value)
// Merge two maps of Cacao variables and replace the base with the source if exists
func (variables *Variables) Merge(source Variables) {
for _, variable := range source {
variables.InsertOrReplace(variable)
}

for _, newValue := range new {
combined.InsertOrReplace(newValue)
}

return combined
}

0 comments on commit af6e7cc

Please sign in to comment.