Skip to content
Snippets Groups Projects
Select Git revision
  • 5c977d6520a48da94640316bdaff16bbac6ca521
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

utils.go

Blame
  • 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 {