chore(settings): use github.com/qdm12/gosettings

This commit is contained in:
Quentin McGaw
2023-05-25 12:08:43 +00:00
parent 1827a03afd
commit a43973c093
31 changed files with 461 additions and 496 deletions

View File

@@ -1,12 +1,6 @@
package helpers
import (
"errors"
"fmt"
"strings"
)
func IsOneOf(value string, choices ...string) (ok bool) {
func IsOneOf[T comparable](value T, choices ...T) (ok bool) {
for _, choice := range choices {
if value == choice {
return true
@@ -14,39 +8,3 @@ func IsOneOf(value string, choices ...string) (ok bool) {
}
return false
}
var (
ErrNoChoice = errors.New("one or more values is set but there is no possible value available")
ErrValueNotOneOf = errors.New("value is not one of the possible choices")
)
func AreAllOneOf(values, choices []string) (err error) {
if len(values) > 0 && len(choices) == 0 {
return fmt.Errorf("%w", ErrNoChoice)
}
set := make(map[string]struct{}, len(choices))
for _, choice := range choices {
choice = strings.ToLower(choice)
set[choice] = struct{}{}
}
for _, value := range values {
_, ok := set[value]
if !ok {
return fmt.Errorf("%w: value %q, choices available are %s",
ErrValueNotOneOf, value, strings.Join(choices, ", "))
}
}
return nil
}
func Uint16IsOneOf(port uint16, choices []uint16) (ok bool) {
for _, choice := range choices {
if port == choice {
return true
}
}
return false
}

View File

@@ -30,7 +30,7 @@ func DefaultNumber[T Number](existing T, defaultValue T) ( //nolint:ireturn
return defaultValue
}
func DefaultIP(existing netip.Addr, defaultValue netip.Addr) (
func DefaultValidator(existing netip.Addr, defaultValue netip.Addr) (
result netip.Addr) {
if existing.IsValid() {
return existing

View File

@@ -30,7 +30,7 @@ func MergeWithNumber[T Number](existing, other T) (result T) { //nolint:ireturn
return other
}
func MergeWithIP(existing, other netip.Addr) (result netip.Addr) {
func MergeWithValidator(existing, other netip.Addr) (result netip.Addr) {
if existing.IsValid() {
return existing
}
@@ -44,7 +44,7 @@ func MergeWithHTTPHandler(existing, other http.Handler) (result http.Handler) {
return other
}
func MergeSlices[T comparable](a, b []T) (result []T) {
func MergeWithSlice[T comparable](a, b []T) (result []T) {
if a == nil && b == nil {
return nil
}

View File

@@ -28,7 +28,7 @@ func OverrideWithNumber[T Number](existing, other T) (result T) { //nolint:iretu
return other
}
func OverrideWithIP(existing, other netip.Addr) (result netip.Addr) {
func OverrideWithValidator(existing, other netip.Addr) (result netip.Addr) {
if !other.IsValid() {
return existing
}