mirror of https://github.com/docker/cli.git
Refactor substitute to reduce cyclo complexity
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
99ecf57c6c
commit
ce544823b6
|
@ -37,39 +37,41 @@ func Substitute(template string, mapping Mapping) (string, error) {
|
|||
var err error
|
||||
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
|
||||
matches := pattern.FindStringSubmatch(substring)
|
||||
groups := make(map[string]string)
|
||||
for i, name := range pattern.SubexpNames() {
|
||||
if i != 0 {
|
||||
groups[name] = matches[i]
|
||||
}
|
||||
groups := matchGroups(matches)
|
||||
if escaped := groups["escaped"]; escaped != "" {
|
||||
return escaped
|
||||
}
|
||||
|
||||
substitution := groups["named"]
|
||||
if substitution == "" {
|
||||
substitution = groups["braced"]
|
||||
}
|
||||
if substitution != "" {
|
||||
|
||||
switch {
|
||||
|
||||
case substitution == "":
|
||||
err = &InvalidTemplateError{Template: template}
|
||||
return ""
|
||||
|
||||
// Soft default (fall back if unset or empty)
|
||||
if strings.Contains(substitution, ":-") {
|
||||
case strings.Contains(substitution, ":-"):
|
||||
name, defaultValue := partition(substitution, ":-")
|
||||
value, ok := mapping(name)
|
||||
if !ok || value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// Hard default (fall back if-and-only-if empty)
|
||||
if strings.Contains(substitution, "-") {
|
||||
case strings.Contains(substitution, "-"):
|
||||
name, defaultValue := partition(substitution, "-")
|
||||
value, ok := mapping(name)
|
||||
if !ok {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
if strings.Contains(substitution, ":?") {
|
||||
case strings.Contains(substitution, ":?"):
|
||||
name, errorMessage := partition(substitution, ":?")
|
||||
value, ok := mapping(name)
|
||||
if !ok || value == "" {
|
||||
|
@ -77,9 +79,8 @@ func Substitute(template string, mapping Mapping) (string, error) {
|
|||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
if strings.Contains(substitution, "?") {
|
||||
case strings.Contains(substitution, "?"):
|
||||
name, errorMessage := partition(substitution, "?")
|
||||
value, ok := mapping(name)
|
||||
if !ok {
|
||||
|
@ -89,25 +90,21 @@ func Substitute(template string, mapping Mapping) (string, error) {
|
|||
return value
|
||||
}
|
||||
|
||||
// No default (fall back to empty string)
|
||||
value, ok := mapping(substitution)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
value, _ := mapping(substitution)
|
||||
return value
|
||||
}
|
||||
|
||||
if escaped := groups["escaped"]; escaped != "" {
|
||||
return escaped
|
||||
}
|
||||
|
||||
err = &InvalidTemplateError{Template: template}
|
||||
return ""
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func matchGroups(matches []string) map[string]string {
|
||||
groups := make(map[string]string)
|
||||
for i, name := range pattern.SubexpNames()[1:] {
|
||||
groups[name] = matches[i+1]
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
// Split the string at the first occurrence of sep, and return the part before the separator,
|
||||
// and the part after the separator.
|
||||
//
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var defaults = map[string]string{
|
||||
|
@ -22,6 +23,12 @@ func TestEscaped(t *testing.T) {
|
|||
assert.Equal(t, "${foo}", result)
|
||||
}
|
||||
|
||||
func TestSubstituteNoMatch(t *testing.T) {
|
||||
result, err := Substitute("foo", defaultMapping)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "foo", result)
|
||||
}
|
||||
|
||||
func TestInvalid(t *testing.T) {
|
||||
invalidTemplates := []string{
|
||||
"${",
|
||||
|
|
Loading…
Reference in New Issue