From af6e7ccb39c748d0b612c4734530c2e53b5f11aa Mon Sep 17 00:00:00 2001 From: Maarten de Kruijf Date: Mon, 11 Mar 2024 11:58:33 +0100 Subject: [PATCH] Made variables not copy on usage and cleaned up merge --- models/cacao/variables.go | 60 ++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/models/cacao/variables.go b/models/cacao/variables.go index 1bdcfafe..780d029f 100644 --- a/models/cacao/variables.go +++ b/models/cacao/variables.go @@ -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) } } @@ -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 { @@ -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 }