Skip to content

Commit

Permalink
Merge pull request #72 from lukasjarosch/feat/undefined-variable-hand…
Browse files Browse the repository at this point in the history
…ling

feat: ignore variables with targetValue `nil`
  • Loading branch information
lukasjarosch committed Aug 15, 2023
2 parents 2950ba6 + d6a1e00 commit 9b5321a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func ReplaceVariables(data Data, classFiles []*Class, predefinedVariables map[st
return false
}

// variables can be ignored, they are stored here :)
ignoredVariables := []Variable{}

// TODO: gosh, make this a standalone function already
replaceVariable := func(variable Variable) error {
var targetValue interface{}
Expand Down Expand Up @@ -150,6 +153,15 @@ func ReplaceVariables(data Data, classFiles []*Class, predefinedVariables map[st
return variable.FullName() != sourceValue
}

// At this point, the variable might still point to something unknown (targetValue == nil).
// This case might occur if one defines an inventory value which uses bash / environment variables, which
// look like skipper variables, but actually aren't.
// In this case, we ignore this variable.
if targetValue == nil {
ignoredVariables = append(ignoredVariables, variable)
return nil
}

// if the variable is not 'inline', we are going to 'attach' whatever the variable points to
// with the variable. This allows you to import a list from a different class for example.
// class-file:
Expand Down Expand Up @@ -188,6 +200,17 @@ func ReplaceVariables(data Data, classFiles []*Class, predefinedVariables map[st
return err
}

// remove the ignored variables from the found variables
// otherwise we would end up in an endless loop.
for _, ignored := range ignoredVariables {
for i, variable := range variables {
if variable.Name == ignored.Name {
variables[i] = variables[len(variables)-1]
variables = variables[:len(variables)-1]
}
}
}

if len(variables) == 0 {
break
}
Expand Down

0 comments on commit 9b5321a

Please sign in to comment.