chore: tweak utils

This commit is contained in:
Steven 2024-08-20 23:45:55 +08:00
parent fc20673706
commit 340025002b
4 changed files with 224 additions and 47 deletions

1
go.mod
View File

@ -71,6 +71,7 @@ require (
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9 github.com/lib/pq v1.10.9
github.com/mssola/useragent v1.0.0 github.com/mssola/useragent v1.0.0
github.com/nyaruka/phonenumbers v1.4.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/posthog/posthog-go v1.2.18 github.com/posthog/posthog-go v1.2.18
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa

2
go.sum
View File

@ -258,6 +258,8 @@ github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/nyaruka/phonenumbers v1.4.0 h1:ddhWiHnHCIX3n6ETDA58Zq5dkxkjlvgrDWM2OHHPCzU=
github.com/nyaruka/phonenumbers v1.4.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=

View File

@ -2,20 +2,25 @@ package util
import ( import (
"crypto/rand" "crypto/rand"
"fmt"
"math/big" "math/big"
"net/mail" "net/mail"
"net/url"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8"
"github.com/google/uuid"
"github.com/nyaruka/phonenumbers"
"github.com/pkg/errors"
) )
// ConvertStringToInt32 converts a string to int32. // ConvertStringToInt32 converts a string to int32.
func ConvertStringToInt32(src string) (int32, error) { func ConvertStringToInt32(src string) (int32, error) {
i, err := strconv.Atoi(src) parsed, err := strconv.ParseInt(src, 10, 32)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return int32(i), nil return int32(parsed), nil
} }
// HasPrefixes returns true if the string s has any of the given prefixes. // HasPrefixes returns true if the string s has any of the given prefixes.
@ -36,6 +41,10 @@ func ValidateEmail(email string) bool {
return true return true
} }
func GenUUID() string {
return uuid.New().String()
}
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// RandomString returns a random string with length n. // RandomString returns a random string with length n.
@ -57,8 +66,91 @@ func RandomString(n int) (string, error) {
return sb.String(), nil return sb.String(), nil
} }
// ValidateURI validates the URI. // ValidatePhone validates the phone number.
func ValidateURI(uri string) bool { func ValidatePhone(phone string) error {
u, err := url.Parse(uri) phoneNumber, err := phonenumbers.Parse(phone, "")
return err == nil && u.Scheme != "" && u.Host != "" if err != nil {
return err
}
if !phonenumbers.IsValidNumber(phoneNumber) {
return errors.New("invalid phone number")
}
return nil
}
// SanitizeUTF8String returns a copy of the string s with each run of invalid or unprintable UTF-8 byte sequences
// replaced by its hexadecimal representation string.
func SanitizeUTF8String(s string) string {
var b strings.Builder
for i, c := range s {
if c != utf8.RuneError {
continue
}
_, wid := utf8.DecodeRuneInString(s[i:])
if wid == 1 {
b.Grow(len(s))
_, _ = b.WriteString(s[:i])
s = s[i:]
break
}
}
// Fast path for unchanged input
if b.Cap() == 0 { // didn't call b.Grow above
return s
}
for i := 0; i < len(s); {
c := s[i]
// U+0000-U+0019 are control characters
if 0x20 <= c && c < utf8.RuneSelf {
i++
_ = b.WriteByte(c)
continue
}
_, wid := utf8.DecodeRuneInString(s[i:])
if wid == 1 {
i++
_, _ = b.WriteString(fmt.Sprintf("\\x%02x", c))
continue
}
_, _ = b.WriteString(s[i : i+wid])
i += wid
}
return b.String()
}
// ReplaceString replaces all occurrences of old in slice with new.
func ReplaceString(slice []string, old, new string) []string {
for i, s := range slice {
if s == old {
slice[i] = new
}
}
return slice
}
// TruncateString truncates the string to have a maximum length of `limit` characters.
func TruncateString(str string, limit int) (string, bool) {
chars := 0
// The string may contain unicode characters, so we iterate here.
for i := range str {
if chars >= limit {
return str[:i], true
}
chars++
}
return str, false
}
// TruncateStringWithDescription tries to truncate the string and append "... (view details in Bytebase)" if truncated.
func TruncateStringWithDescription(str string) string {
const limit = 450
if truncatedStr, truncated := TruncateString(str, limit); truncated {
return fmt.Sprintf("%s... (view details in Bytebase)", truncatedStr)
}
return str
} }

View File

@ -2,60 +2,142 @@ package util
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestValidateEmail(t *testing.T) { func TestHasPrefixes(t *testing.T) {
tests := []struct { type args struct {
email string src string
want bool prefixes []string
}{
{
email: "t@gmail.com",
want: true,
},
{
email: "@yourselfhosted.com",
want: false,
},
{
email: "1@gmail",
want: true,
},
} }
for _, test := range tests {
result := ValidateEmail(test.email)
if result != test.want {
t.Errorf("Validate Email %s: got result %v, want %v.", test.email, result, test.want)
}
}
}
func TestValidateURI(t *testing.T) {
tests := []struct { tests := []struct {
uri string name string
args args
want bool want bool
}{ }{
{ {
uri: "https://localhsot:3000", name: "has prefixes",
args: args{
src: "abc",
prefixes: []string{"a", "b", "c"},
},
want: true, want: true,
}, },
{ {
uri: "https://yourselfhosted.com", name: "has no matching prefix",
want: true, args: args{
}, src: "this is a sentence",
{ prefixes: []string{"that", "x", "y"},
uri: "google.com", },
want: false,
},
{
uri: "i don't know",
want: false, want: false,
}, },
} }
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
got := HasPrefixes(tt.args.src, tt.args.prefixes...)
assert.Equal(t, got, tt.want)
})
}
}
func TestTruncateString(t *testing.T) {
tests := []struct {
name string
str string
limit int
want string
truncated bool
}{
{
name: "simple truncate 0",
str: "0123",
limit: 0,
want: "",
truncated: true,
},
{
name: "simple truncate 2",
str: "0123",
limit: 2,
want: "01",
truncated: true,
},
{
name: "simple truncate 3",
str: "0123",
limit: 3,
want: "012",
truncated: true,
},
{
name: "simple truncate 4",
str: "0123",
limit: 4,
want: "0123",
truncated: false,
},
{
name: "simple truncate 20",
str: "0123",
limit: 20,
want: "0123",
truncated: false,
},
{
name: "unicode truncate 5",
str: "H㐀〾▓朗퐭텟şüöžåйкл¤",
limit: 5,
want: "H㐀〾▓朗",
truncated: true,
},
{
name: "unicode truncate 10",
str: "H㐀〾▓朗퐭텟şüöžåйкл¤",
limit: 10,
want: "H㐀〾▓朗퐭텟şüö",
truncated: true,
},
{
name: "unicode fit",
str: "H㐀〾▓朗퐭텟şüöžåйкл¤",
limit: 16,
want: "H㐀〾▓朗퐭텟şüöžåйкл¤",
truncated: false,
},
}
a := assert.New(t)
for i := range tests {
test := tests[i]
t.Run(test.name, func(_ *testing.T) {
got, truncated := TruncateString(test.str, test.limit)
a.Equal(test.want, got)
a.Equal(test.truncated, truncated)
})
}
}
func TestValidatePhone(t *testing.T) {
tests := []struct {
phone string
want bool
}{
{
phone: "1234567890",
want: false,
},
{
phone: "+8615655556666",
want: true,
},
}
for _, test := range tests { for _, test := range tests {
result := ValidateURI(test.uri) got := ValidatePhone(test.phone)
if result != test.want { isValid := got == nil
t.Errorf("Validate URI %s: got result %v, want %v.", test.uri, result, test.want) if isValid != test.want {
t.Errorf("validatePhone %s, err %v", test.phone, got)
} }
} }
} }