fix the error message in Substitute function

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
Arash Deshmeh 2018-02-26 15:23:52 -05:00
parent e33bc48752
commit 5d8ce59a25
2 changed files with 13 additions and 9 deletions

View File

@ -7,7 +7,7 @@ import (
) )
var delimiter = "\\$" var delimiter = "\\$"
var substitution = "[_a-z][_a-z0-9]*(?::?[-?][^}]+)?" var substitution = "[_a-z][_a-z0-9]*(?::?[-?][^}]*)?"
var patternString = fmt.Sprintf( var patternString = fmt.Sprintf(
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?P<braced>%s)}|(?P<invalid>))", "%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?P<braced>%s)}|(?P<invalid>))",
@ -75,7 +75,9 @@ func Substitute(template string, mapping Mapping) (string, error) {
name, errorMessage := partition(substitution, ":?") name, errorMessage := partition(substitution, ":?")
value, ok := mapping(name) value, ok := mapping(name)
if !ok || value == "" { if !ok || value == "" {
err = &InvalidTemplateError{Template: errorMessage} err = &InvalidTemplateError{
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
}
return "" return ""
} }
return value return value
@ -84,7 +86,9 @@ func Substitute(template string, mapping Mapping) (string, error) {
name, errorMessage := partition(substitution, "?") name, errorMessage := partition(substitution, "?")
value, ok := mapping(name) value, ok := mapping(name)
if !ok { if !ok {
err = &InvalidTemplateError{Template: errorMessage} err = &InvalidTemplateError{
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
}
return "" return ""
} }
return value return value

View File

@ -97,23 +97,23 @@ func TestMandatoryVariableErrors(t *testing.T) {
}{ }{
{ {
template: "not ok ${UNSET_VAR:?Mandatory Variable Unset}", template: "not ok ${UNSET_VAR:?Mandatory Variable Unset}",
expectedError: "Mandatory Variable Unset", expectedError: "required variable UNSET_VAR is missing a value: Mandatory Variable Unset",
}, },
{ {
template: "not ok ${BAR:?Mandatory Variable Empty}", template: "not ok ${BAR:?Mandatory Variable Empty}",
expectedError: "Mandatory Variable Empty", expectedError: "required variable BAR is missing a value: Mandatory Variable Empty",
}, },
{ {
template: "not ok ${UNSET_VAR:?}", template: "not ok ${UNSET_VAR:?}",
expectedError: "", expectedError: "required variable UNSET_VAR is missing a value",
}, },
{ {
template: "not ok ${UNSET_VAR?Mandatory Variable Unset", template: "not ok ${UNSET_VAR?Mandatory Variable Unset}",
expectedError: "Mandatory Variable Unset", expectedError: "required variable UNSET_VAR is missing a value: Mandatory Variable Unset",
}, },
{ {
template: "not ok ${UNSET_VAR?}", template: "not ok ${UNSET_VAR?}",
expectedError: "", expectedError: "required variable UNSET_VAR is missing a value",
}, },
} }