migrate orion repo into monorepo structure
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/mail"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NormalizeEmail(email string) (string, error) {
|
||||
if email == "" {
|
||||
return "", errors.New("email empty")
|
||||
}
|
||||
|
||||
lower := strings.ToLower(email)
|
||||
lower = strings.TrimSpace(lower)
|
||||
|
||||
parsed, err := mail.ParseAddress(lower)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return parsed.Address, nil
|
||||
}
|
||||
|
||||
func IsValidEmail(email string) bool {
|
||||
_, err := mail.ParseAddress(email)
|
||||
return err == nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// enum of environment variables
|
||||
type EnvVar string
|
||||
|
||||
const ()
|
||||
|
||||
// MustGetEnv returns the value of the environment variable with the given key.
|
||||
// panics if the variable is not set.
|
||||
func MustGetEnv[T string | EnvVar](key T) string {
|
||||
keyString := string(key)
|
||||
value := os.Getenv(keyString)
|
||||
if value == "" {
|
||||
logrus.Errorf("Missing required environment variable %s", key)
|
||||
panic("Missing required environment variable")
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// GetEnv returns the value of the environment variable with the given key.
|
||||
// returns an empty string if the variable is not set.
|
||||
func GetEnv(key string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
logrus.Warnf("Missing optional environment variable %s", key)
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func OptionalBool(input *bool) bool {
|
||||
if input == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return *input
|
||||
}
|
||||
|
||||
func CreateOptionalBool(input bool) *bool {
|
||||
if input == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &input
|
||||
}
|
||||
|
||||
// OptionalString converts a non-nil *string to the respective string or returns "".
|
||||
func OptionalString(input *string) string {
|
||||
if input == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return *input
|
||||
}
|
||||
|
||||
// OptionalInt converts a non-nil *int to the respective int, otherwise returns 0.
|
||||
func OptionalInt(input *int) int {
|
||||
if input == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *input
|
||||
}
|
||||
|
||||
// CreateOptionalInt when given a zero value int (0), it returns a nil *int.
|
||||
// Otherwise, it gives a proper *int with valid value.
|
||||
func CreateOptionalInt(input int) *int {
|
||||
if input == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &input
|
||||
}
|
||||
|
||||
// CreateOptionalString when given an empty string, it returns a nil *string.
|
||||
// Otherwise, it gives a proper *string with valid value.
|
||||
func CreateOptionalString(input string) *string {
|
||||
if input == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &input
|
||||
}
|
||||
|
||||
// GetNumberFromString converts a string to a number.
|
||||
// Returns error if the query is not a number.
|
||||
func GetNumberFromString(input string) (int, error) {
|
||||
for _, c := range input {
|
||||
if c < '0' || c > '9' {
|
||||
return 0, errors.New("invalid input")
|
||||
}
|
||||
}
|
||||
|
||||
idAsInt, err := strconv.Atoi(input)
|
||||
if err != nil || idAsInt <= 0 {
|
||||
return 0, errors.New("invalid input")
|
||||
}
|
||||
|
||||
return idAsInt, nil
|
||||
}
|
||||
|
||||
type Number interface {
|
||||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
|
||||
}
|
||||
|
||||
// OptionalNumber converts a non-nil *NUMBER to the respective number value or returns 0.
|
||||
func OptionalNumber[T Number](input *T) T {
|
||||
if input == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *input
|
||||
}
|
||||
|
||||
// CreateOptionalNumber when given an zero value NUMBER (0), it returns a nil *NUMBER, otherwise, it gives a proper *NUMBER with valid value.
|
||||
func CreateOptionalNumber[T Number](input T) *T {
|
||||
if input == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &input
|
||||
}
|
||||
|
||||
func IntToInt64Pointer(input *int) *int64 {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
val := int64(*input)
|
||||
return &val
|
||||
}
|
||||
|
||||
func NumberToNumberPointer[T Number, Y Number](input *T) *Y {
|
||||
if input == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
val := Y(*input)
|
||||
return &val
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
charsetNumbers = "0123456789"
|
||||
)
|
||||
|
||||
// RandomString generates a random string of length n based on self defined charset
|
||||
func RandomString(length int) string {
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(length)
|
||||
for i := 0; i < length; i++ {
|
||||
sb.WriteByte(charset[rand.Intn(len(charset))])
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// RandomStringNumbers
|
||||
func RandomStringNumbers(length int) string {
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(length)
|
||||
for range length {
|
||||
sb.WriteByte(charsetNumbers[rand.Intn(len(charsetNumbers))])
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
func Unique(slice []string) []string {
|
||||
keys := make(map[string]bool)
|
||||
list := []string{}
|
||||
|
||||
for _, entry := range slice {
|
||||
if _, value := keys[entry]; !value {
|
||||
keys[entry] = true
|
||||
list = append(list, entry)
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package utils
|
||||
|
||||
func HoursToMicroseconds[K uint32 | uint64](hours K) int64 {
|
||||
return int64(hours) * 60 * 60 * 1000000
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import "net/url"
|
||||
|
||||
func IsValidURL(urlString string) bool {
|
||||
_, err := url.ParseRequestURI(urlString)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(urlString)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeEmail(t *testing.T) {
|
||||
email := "[email protected]"
|
||||
normalizedEmail, err := NormalizeEmail(email)
|
||||
if err != nil {
|
||||
t.Error("invalid email parse", err)
|
||||
}
|
||||
|
||||
email = " [email protected]"
|
||||
normalizedEmail, err = NormalizeEmail(email)
|
||||
if err != nil {
|
||||
t.Error("invalid email parse", err)
|
||||
}
|
||||
if normalizedEmail != "[email protected]" {
|
||||
t.Errorf("invalid email parse: %s", normalizedEmail)
|
||||
}
|
||||
|
||||
email = "[email protected]"
|
||||
normalizedEmail, err = NormalizeEmail(email)
|
||||
if err != nil {
|
||||
t.Error("invalid email parse", err)
|
||||
}
|
||||
if normalizedEmail != "[email protected]" {
|
||||
t.Errorf("invalid email parse: %s", normalizedEmail)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user