Select Git revision
-
Christel Loftus authoredChristel Loftus authored
utils.go 8.08 KiB
package utils
import (
"bytes"
emailverifier "github.com/AfterShip/email-verifier"
normalizer "github.com/dimuska139/go-email-normalizer/v2"
"github.com/jinzhu/now"
"github.com/mohae/deepcopy"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
"math"
"net"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"time"
)
// GetEnv is a helper function for getting environment variables with a default
func GetEnv(name string, def string) (env string) {
// If variable not set, provide default
if env = os.Getenv(name); env == "" {
env = def
}
return
}
// CorsHeaders returns a map to allow Cors
func CorsHeaders() map[string]string {
return map[string]string{
"Access-Control-Allow-Origin": "*",
// do not wildcard: https://stackoverflow.com/questions/13146892/cors-access-control-allow-headers-wildcard-being-ignored
"Access-Control-Allow-Headers": "authorization, content-type, referer, sec-ch-ua, sec-ch-ua-mobile, sec-ch-ua-platform, user-agent, x-amz-date, x-amz-security-token",
"Access-Control-Allow-Methods": "OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Credentials": "true",
}
}
func DeepCopy(fromValue interface{}) (toValue interface{}) {
return deepcopy.Copy(fromValue)
}
func Clone[T any](fromValue T) (toValue T) {
return deepcopy.Copy(fromValue).(T)
}
func ValueToPointer[V any](value V) *V {
return &value
}
func PointerToValue[V any](value *V) V {
if value != nil {
return *value
}
return *new(V) // zero value of V
}
func ValidateEmailAddress(email string, options ...map[string]string) (string, error) {
// To lower
cleanedEmail := strings.ToLower(strings.TrimSpace(email))
// Remove all whitespaces
cleanedEmail = string_utils.RemoveAllWhiteSpaces(cleanedEmail)
// Also remove unofficial whitespaces
for _, char := range string_utils.NonOfficialWhitespaceChars {