-
Notifications
You must be signed in to change notification settings - Fork 13
Consolidate duplicate variable expansion logic #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,20 @@ var varExprPattern = regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)\}`) | |
|
|
||
| var logValidation = logger.New("config:validation") | ||
|
|
||
| // expandVariables expands variable expressions in a string | ||
| // Returns the expanded string and error if any variable is undefined | ||
| func expandVariables(value, jsonPath string) (string, error) { | ||
| logValidation.Printf("Expanding variables: jsonPath=%s", jsonPath) | ||
| // expandVariablesCore is the shared implementation for variable expansion. | ||
| // It works with byte slices and handles the core expansion logic, tracking undefined variables. | ||
| // This eliminates code duplication between expandVariables and ExpandRawJSONVariables. | ||
| func expandVariablesCore(data []byte, contextDesc string) ([]byte, []string, error) { | ||
| logValidation.Printf("Expanding variables: context=%s", contextDesc) | ||
| var undefinedVars []string | ||
|
|
||
| result := varExprPattern.ReplaceAllStringFunc(value, func(match string) string { | ||
| result := varExprPattern.ReplaceAllFunc(data, func(match []byte) []byte { | ||
| // Extract variable name (remove ${ and }) | ||
| varName := match[2 : len(match)-1] | ||
| varName := string(match[2 : len(match)-1]) | ||
|
|
||
| if envValue, exists := os.LookupEnv(varName); exists { | ||
| logValidation.Printf("Expanded variable: %s (found in environment)", varName) | ||
| return envValue | ||
| return []byte(envValue) | ||
| } | ||
|
|
||
| // Track undefined variable | ||
|
|
@@ -38,43 +39,34 @@ func expandVariables(value, jsonPath string) (string, error) { | |
| return match // Keep original if undefined | ||
|
Comment on lines
31
to
39
|
||
| }) | ||
|
|
||
| logValidation.Printf("Variable expansion completed: context=%s, undefined_count=%d", contextDesc, len(undefinedVars)) | ||
| return result, undefinedVars, nil | ||
| } | ||
|
|
||
| // expandVariables expands variable expressions in a string | ||
| // Returns the expanded string and error if any variable is undefined | ||
| func expandVariables(value, jsonPath string) (string, error) { | ||
| result, undefinedVars, _ := expandVariablesCore([]byte(value), fmt.Sprintf("jsonPath=%s", jsonPath)) | ||
|
|
||
| if len(undefinedVars) > 0 { | ||
| logValidation.Printf("Variable expansion failed: undefined variables=%v", undefinedVars) | ||
| return "", rules.UndefinedVariable(undefinedVars[0], jsonPath) | ||
| } | ||
|
|
||
| logValidation.Print("Variable expansion completed successfully") | ||
| return result, nil | ||
| return string(result), nil | ||
| } | ||
|
|
||
| // ExpandRawJSONVariables expands all ${VAR} expressions in JSON data before schema validation. | ||
| // This ensures the schema validates the expanded values, not the variable syntax. | ||
| // It collects all undefined variables and reports them in a single error. | ||
| func ExpandRawJSONVariables(data []byte) ([]byte, error) { | ||
| logValidation.Print("Expanding variables in raw JSON data") | ||
| var undefinedVars []string | ||
|
|
||
| result := varExprPattern.ReplaceAllFunc(data, func(match []byte) []byte { | ||
| // Extract variable name (remove ${ and }) | ||
| varName := string(match[2 : len(match)-1]) | ||
|
|
||
| if envValue, exists := os.LookupEnv(varName); exists { | ||
| logValidation.Printf("Expanded variable in JSON: %s", varName) | ||
| return []byte(envValue) | ||
| } | ||
|
|
||
| // Track undefined variable | ||
| undefinedVars = append(undefinedVars, varName) | ||
| logValidation.Printf("Undefined variable in JSON: %s", varName) | ||
| return match // Keep original if undefined | ||
| }) | ||
| result, undefinedVars, _ := expandVariablesCore(data, "raw JSON data") | ||
|
|
||
| if len(undefinedVars) > 0 { | ||
| logValidation.Printf("Variable expansion failed: undefined variables=%v", undefinedVars) | ||
| return nil, rules.UndefinedVariable(undefinedVars[0], "configuration") | ||
| } | ||
|
Comment on lines
59
to
68
|
||
|
|
||
| logValidation.Print("Raw JSON variable expansion completed") | ||
| return result, nil | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expandVariablesCorereturns anerrorbut currently always returnsnil, and both callers explicitly ignore it (_, _ := ...). Consider removing theerrorreturn fromexpandVariablesCore(and adjusting callers), or if you anticipate future error paths, handle/propagate it now to avoid dead code and underscore-ignored values.