diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2024-08-10 13:36:51 +1000 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-10 13:36:51 +1000 | 
| commit | 981f3fd6811b343893a80342085cf204396b95ae (patch) | |
| tree | 569e49c83988a0ca4daa2403697183bbc36815f7 | |
| parent | 5887cbba5730a5d8a468df78817261308e2378c1 (diff) | |
refactor: use unified string join utils (#7659)
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
| -rw-r--r-- | internal/configuration/deprecation.go | 71 | ||||
| -rw-r--r-- | internal/configuration/koanf_util.go | 2 | 
2 files changed, 10 insertions, 63 deletions
diff --git a/internal/configuration/deprecation.go b/internal/configuration/deprecation.go index 4de075190..71f813301 100644 --- a/internal/configuration/deprecation.go +++ b/internal/configuration/deprecation.go @@ -4,10 +4,10 @@ import (  	"fmt"  	"path"  	"strconv" -	"strings"  	"github.com/authelia/authelia/v4/internal/configuration/schema"  	"github.com/authelia/authelia/v4/internal/model" +	"github.com/authelia/authelia/v4/internal/utils"  )  // Deprecation represents a deprecated configuration key. @@ -340,14 +340,14 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  		MapFunc: func(d MultiKeyMappedDeprecation, keys map[string]any, val *schema.StructValidator) {  			host, port, err := getHostPort("notifier.smtp.host", "notifier.smtp.port", schema.DefaultSMTPNotifierConfiguration.Address.Host(), schema.DefaultSMTPNotifierConfiguration.Address.Port(), keys)  			if err != nil { -				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, strJoinAnd(d.Keys), d.NewKey, err)) +				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, utils.StringJoinAnd(d.Keys), d.NewKey, err))  				return  			}  			address := schema.NewSMTPAddress("", host, port) -			val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, strJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor())) +			val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, utils.StringJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor()))  			keys[d.NewKey] = address.String() @@ -363,7 +363,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  		MapFunc: func(d MultiKeyMappedDeprecation, keys map[string]any, val *schema.StructValidator) {  			host, port, err := getHostPort(keyStoragePostgresHost, keyStoragePostgresPort, schema.DefaultPostgreSQLStorageConfiguration.Address.Host(), schema.DefaultPostgreSQLStorageConfiguration.Address.Port(), keys)  			if err != nil { -				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, strJoinAnd(d.Keys), d.NewKey, err)) +				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, utils.StringJoinAnd(d.Keys), d.NewKey, err))  				return  			} @@ -373,7 +373,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  			} else {  				keys[d.NewKey] = address.String() -				val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, strJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor())) +				val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, utils.StringJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor()))  				for _, key := range d.Keys {  					delete(keys, key) @@ -388,7 +388,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  		MapFunc: func(d MultiKeyMappedDeprecation, keys map[string]any, val *schema.StructValidator) {  			host, port, err := getHostPort(keyStorageMySQLHost, keyStorageMySQLPort, schema.DefaultMySQLStorageConfiguration.Address.Host(), schema.DefaultMySQLStorageConfiguration.Address.Port(), keys)  			if err != nil { -				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, strJoinAnd(d.Keys), d.NewKey, err)) +				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, utils.StringJoinAnd(d.Keys), d.NewKey, err))  				return  			} @@ -398,7 +398,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  			} else {  				keys[d.NewKey] = address.String() -				val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, strJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor())) +				val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, utils.StringJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp://]<hostname>[:<port>]", address.String(), d.Version.NextMajor()))  				for _, key := range d.Keys {  					delete(keys, key) @@ -413,7 +413,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  		MapFunc: func(d MultiKeyMappedDeprecation, keys map[string]any, val *schema.StructValidator) {  			host, port, err := getHostPort(keyServerHost, keyServerPort, schema.DefaultServerConfiguration.Address.Hostname(), schema.DefaultServerConfiguration.Address.Port(), keys)  			if err != nil { -				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, strJoinAnd(d.Keys), d.NewKey, err)) +				val.Push(fmt.Errorf(errFmtMultiKeyMappingPortConvert, utils.StringJoinAnd(d.Keys), d.NewKey, err))  				return  			} @@ -439,7 +439,7 @@ var deprecationsMKM = []MultiKeyMappedDeprecation{  			address.SetPath(subpath) -			val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, strJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp[(4|6)]://]<hostname>[:<port>][/<path>]' or 'tcp[(4|6)://][hostname]:<port>[/<path>]", address.String(), d.Version.NextMajor())) +			val.PushWarning(fmt.Errorf(errFmtMultiRemappedKeys, utils.StringJoinAnd(d.Keys), d.Version, d.NewKey, "[tcp[(4|6)]://]<hostname>[:<port>][/<path>]' or 'tcp[(4|6)://][hostname]:<port>[/<path>]", address.String(), d.Version.NextMajor()))  			keys[d.NewKey] = address.String() @@ -482,59 +482,6 @@ func getHostPort(hostKey, portKey, hostFallback string, portFallback int, keys m  	return host, port, nil  } -func strJoinAnd(items []string) string { -	return strJoinComma("and", items) -} - -func strJoinComma(word string, items []string) string { -	if word == "" { -		return buildJoinedString(",", "", "'", items) -	} - -	return buildJoinedString(",", word, "'", items) -} - -func buildJoinedString(sep, sepFinal, quote string, items []string) string { -	n := len(items) - -	if n == 0 { -		return "" -	} - -	b := &strings.Builder{} - -	for i := 0; i < n; i++ { -		if quote != "" { -			b.WriteString(quote) -		} - -		b.WriteString(items[i]) - -		if quote != "" { -			b.WriteString(quote) -		} - -		if i == (n - 1) { -			continue -		} - -		if sep != "" { -			if sepFinal == "" || n != 2 { -				b.WriteString(sep) -			} - -			b.WriteString(" ") -		} - -		if sepFinal != "" && i == (n-2) { -			b.WriteString(strings.Trim(sepFinal, " ")) -			b.WriteString(" ") -		} -	} - -	return b.String() -} -  func GetMultiKeyMappedDeprecationKeys() (keys []string) {  	for _, mkm := range deprecationsMKM {  		keys = append(keys, mkm.Keys...) diff --git a/internal/configuration/koanf_util.go b/internal/configuration/koanf_util.go index 52e173565..d0a0b1b0a 100644 --- a/internal/configuration/koanf_util.go +++ b/internal/configuration/koanf_util.go @@ -185,7 +185,7 @@ func koanfRemapKeysMultiMapped(keys map[string]any, val *schema.StructValidator,  		}  		if v, ok := keys[dm.NewKey]; ok { -			val.Push(fmt.Errorf(errFmtMultiKeyMappingExists, strJoinAnd(dm.Keys), dm.NewKey, v)) +			val.Push(fmt.Errorf(errFmtMultiKeyMappingExists, utils.StringJoinAnd(dm.Keys), dm.NewKey, v))  			continue  		}  | 
