From d0f668263ade6ba5796143d3aee9546a28939660 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Wed, 10 Jan 2024 22:10:26 +0800 Subject: [PATCH 1/5] update rand code --- randx/rand_code.go | 61 ++++++++++++++++++++++++++++------------- randx/rand_code_test.go | 27 ++++++++---------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/randx/rand_code.go b/randx/rand_code.go index 776d055..bc429a7 100644 --- a/randx/rand_code.go +++ b/randx/rand_code.go @@ -19,34 +19,57 @@ import ( "math/rand" ) -var ERRTYPENOTSUPPORTTED = errors.New("ekit:不支持的类型") +var ( + ErrTypeNotSupported = errors.New("ekit:不支持的类型") + // deprecated + ERRTYPENOTSUPPORTTED = ErrTypeNotSupported +) type TYPE int const ( - TYPE_DEFAULT TYPE = 0 //默认类型 - TYPE_DIGIT TYPE = 1 //数字// - TYPE_LETTER TYPE = 2 //小写字母 - TYPE_CAPITAL TYPE = 3 //大写字母 - TYPE_MIXED TYPE = 4 //数字+字母混合 + // 数字 + TYPE_DIGIT TYPE = 1 + // 小写字母 + TYPE_LOWER TYPE = 1 << 1 + TYPE_LETTER TYPE = TYPE_LOWER + // 大写字母 + TYPE_UPPER TYPE = 1 << 2 + TYPE_CAPITAL TYPE = TYPE_UPPER + // 混合类型 + TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPER | TYPE_LOWER) + + // 数字字符组 + CHARSET_DIGIT = "0123456789" + // 小写字母字符组 + CHARSET_LOWER = "abcdefghijklmnopqrstuvwxyz" + CHARSET_LETTER = CHARSET_LOWER + // 大写字母字符组 + CHARSET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + CHARSET_CAPITAL = CHARSET_UPPER ) // RandCode 根据传入的长度和类型生成随机字符串,这个方法目前可以生成数字、字母、数字+字母的随机字符串 func RandCode(length int, typ TYPE) (string, error) { - switch typ { - case TYPE_DEFAULT: - fallthrough - case TYPE_DIGIT: - return generate("0123456789", length, 4), nil - case TYPE_LETTER: - return generate("abcdefghijklmnopqrstuvwxyz", length, 5), nil - case TYPE_CAPITAL: - return generate("ABCDEFGHIJKLMNOPQRSTUVWXYZ", length, 5), nil - case TYPE_MIXED: - return generate("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length, 7), nil - default: - return "", ERRTYPENOTSUPPORTTED + charset := "" + appendIfHas := func(typBase TYPE, baseCharset string) { + if (typ & typBase) == typBase { + charset += baseCharset + } + } + appendIfHas(TYPE_DIGIT, CHARSET_DIGIT) + appendIfHas(TYPE_UPPER, CHARSET_UPPER) + appendIfHas(TYPE_LOWER, CHARSET_LOWER) + + charsetSize := len(charset) + if charsetSize == 0 { + return "", ErrTypeNotSupported + } + bits := 1 + for charsetSize > ((1 << bits) - 1) { + bits++ } + return generate(charset, length, bits), nil } // generate 根据传入的随机源和长度生成随机字符串,一次随机,多次使用 diff --git a/randx/rand_code_test.go b/randx/rand_code_test.go index ee962ee..6350625 100644 --- a/randx/rand_code_test.go +++ b/randx/rand_code_test.go @@ -12,65 +12,60 @@ // See the License for the specific language governing permissions and // limitations under the License. -package randx +package randx_test import ( "errors" "regexp" "testing" + + "github.com/ecodeclub/ekit/randx" ) func TestRandCode(t *testing.T) { testCases := []struct { name string length int - typ TYPE + typ randx.TYPE wantMatch string wantErr error }{ - { - name: "默认类型", - length: 8, - typ: TYPE_DEFAULT, - wantMatch: "^[0-9]+$", - wantErr: nil, - }, { name: "数字验证码", length: 8, - typ: TYPE_DIGIT, + typ: randx.TYPE_DIGIT, wantMatch: "^[0-9]+$", wantErr: nil, }, { name: "小写字母验证码", length: 8, - typ: TYPE_LETTER, + typ: randx.TYPE_LETTER, wantMatch: "^[a-z]+$", wantErr: nil, }, { name: "大写字母验证码", length: 8, - typ: TYPE_CAPITAL, + typ: randx.TYPE_CAPITAL, wantMatch: "^[A-Z]+$", wantErr: nil, }, { name: "混合验证码", length: 8, - typ: TYPE_MIXED, + typ: randx.TYPE_MIXED, wantMatch: "^[0-9a-zA-Z]+$", wantErr: nil, }, { name: "未定义类型", length: 8, - typ: 9, + typ: 0, wantMatch: "", - wantErr: ERRTYPENOTSUPPORTTED, + wantErr: randx.ErrTypeNotSupported, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - code, err := RandCode(tc.length, tc.typ) + code, err := randx.RandCode(tc.length, tc.typ) if err != nil { if !errors.Is(err, tc.wantErr) { t.Errorf("unexpected error: %v", err) From f5039fe40caf1e21ab78727fff5a0727c7070578 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Fri, 12 Jan 2024 22:41:43 +0800 Subject: [PATCH 2/5] update rand code --- randx/rand_code.go | 50 +++++++++--- randx/rand_code_test.go | 164 ++++++++++++++++++++++++++++++++++------ 2 files changed, 178 insertions(+), 36 deletions(-) diff --git a/randx/rand_code.go b/randx/rand_code.go index bc429a7..17fb906 100644 --- a/randx/rand_code.go +++ b/randx/rand_code.go @@ -17,10 +17,13 @@ package randx import ( "errors" "math/rand" + + "github.com/ecodeclub/ekit/tuple/pair" ) var ( - ErrTypeNotSupported = errors.New("ekit:不支持的类型") + ErrTypeNotSupported = errors.New("ekit:不支持的类型") + ErrLengthLessThanZero = errors.New("ekit:长度必须大于0") // deprecated ERRTYPENOTSUPPORTTED = ErrTypeNotSupported ) @@ -36,8 +39,10 @@ const ( // 大写字母 TYPE_UPPER TYPE = 1 << 2 TYPE_CAPITAL TYPE = TYPE_UPPER + // 特殊符号 + TYPE_SPECIAL TYPE = 1 << 3 // 混合类型 - TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPER | TYPE_LOWER) + TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPER | TYPE_LOWER | TYPE_SPECIAL) // 数字字符组 CHARSET_DIGIT = "0123456789" @@ -47,29 +52,52 @@ const ( // 大写字母字符组 CHARSET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" CHARSET_CAPITAL = CHARSET_UPPER + // 特殊字符数组 + CHARSET_SPECIAL = " ~!@#$%^&*()_+-=[]{};'\\:\"|,./<>?" +) + +var ( + // 只限于randx包内部使用 + typeCharSetPair = []pair.Pair[TYPE, string]{ + pair.NewPair(TYPE_DIGIT, CHARSET_DIGIT), + pair.NewPair(TYPE_LOWER, CHARSET_LOWER), + pair.NewPair(TYPE_UPPER, CHARSET_UPPER), + pair.NewPair(TYPE_SPECIAL, CHARSET_SPECIAL), + } ) -// RandCode 根据传入的长度和类型生成随机字符串,这个方法目前可以生成数字、字母、数字+字母的随机字符串 +// RandCode 根据传入的长度和类型生成随机字符串 func RandCode(length int, typ TYPE) (string, error) { + if typ > TYPE_MIXED { + return "", ErrTypeNotSupported + } charset := "" - appendIfHas := func(typBase TYPE, baseCharset string) { - if (typ & typBase) == typBase { - charset += baseCharset + for _, p := range typeCharSetPair { + if (typ & p.Key) == p.Key { + charset += p.Value } } - appendIfHas(TYPE_DIGIT, CHARSET_DIGIT) - appendIfHas(TYPE_UPPER, CHARSET_UPPER) - appendIfHas(TYPE_LOWER, CHARSET_LOWER) + return RandStrByCharset(length, charset) +} +// 根据传入的长度和字符集生成随机字符串 +func RandStrByCharset(length int, charset string) (string, error) { + if length < 0 { + return "", ErrLengthLessThanZero + } charsetSize := len(charset) if charsetSize == 0 { return "", ErrTypeNotSupported } - bits := 1 + return generate(charset, length, getFirstMask(charsetSize)), nil +} + +func getFirstMask(charsetSize int) int { + bits := 0 for charsetSize > ((1 << bits) - 1) { bits++ } - return generate(charset, length, bits), nil + return bits } // generate 根据传入的随机源和长度生成随机字符串,一次随机,多次使用 diff --git a/randx/rand_code_test.go b/randx/rand_code_test.go index 6350625..83d5186 100644 --- a/randx/rand_code_test.go +++ b/randx/rand_code_test.go @@ -15,11 +15,12 @@ package randx_test import ( - "errors" "regexp" + "strings" "testing" "github.com/ecodeclub/ekit/randx" + "github.com/stretchr/testify/assert" ) func TestRandCode(t *testing.T) { @@ -32,56 +33,169 @@ func TestRandCode(t *testing.T) { }{ { name: "数字验证码", - length: 8, + length: 100, typ: randx.TYPE_DIGIT, wantMatch: "^[0-9]+$", wantErr: nil, - }, { + }, + { name: "小写字母验证码", - length: 8, + length: 100, typ: randx.TYPE_LETTER, wantMatch: "^[a-z]+$", wantErr: nil, - }, { + }, + { + name: "数字+小写字母验证码", + length: 100, + typ: randx.TYPE_DIGIT | randx.TYPE_LOWER, + wantMatch: "^[a-z0-9]+$", + wantErr: nil, + }, + { + name: "数字+大写字母验证码", + length: 100, + typ: randx.TYPE_DIGIT | randx.TYPE_UPPER, + wantMatch: "^[A-Z0-9]+$", + wantErr: nil, + }, + { name: "大写字母验证码", - length: 8, + length: 100, typ: randx.TYPE_CAPITAL, wantMatch: "^[A-Z]+$", wantErr: nil, - }, { - name: "混合验证码", - length: 8, - typ: randx.TYPE_MIXED, + }, + { + name: "大小写字母验证码", + length: 100, + typ: randx.TYPE_UPPER | randx.TYPE_LOWER, + wantMatch: "^[a-zA-Z]+$", + wantErr: nil, + }, + { + name: "数字+大小写字母验证码", + length: 100, + typ: randx.TYPE_DIGIT | randx.TYPE_UPPER | randx.TYPE_LOWER, wantMatch: "^[0-9a-zA-Z]+$", wantErr: nil, - }, { - name: "未定义类型", - length: 8, + }, + { + name: "所有类型验证", + length: 100, + typ: randx.TYPE_MIXED, + wantMatch: "^[\\S\\s]*$", + wantErr: nil, + }, + { + name: "未定义类型(超过范围)", + length: 100, + typ: randx.TYPE_MIXED + 1, + wantMatch: "", + wantErr: randx.ErrTypeNotSupported, + }, + { + name: "未定义类型(0)", + length: 100, typ: 0, wantMatch: "", wantErr: randx.ErrTypeNotSupported, }, + { + name: "长度小于0", + length: -1, + typ: 0, + wantMatch: "", + wantErr: randx.ErrLengthLessThanZero, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { code, err := randx.RandCode(tc.length, tc.typ) if err != nil { - if !errors.Is(err, tc.wantErr) { - t.Errorf("unexpected error: %v", err) - } + assert.Equal(t, tc.wantErr, err) + } else { + assert.Lenf( + t, + code, + tc.length, + "expected length: %d but got length:%d", + tc.length, len(code)) + + matched, err := regexp.MatchString(tc.wantMatch, code) + assert.Nil(t, err) + assert.Truef(t, matched, "expected %s but got %s", tc.wantMatch, code) + } + }) + } +} + +func TestRandStrByCharset(t *testing.T) { + matchFunc := func(str, charset string) bool { + for _, c := range str { + if !strings.Contains(charset, string(c)) { + return false + } + } + return true + } + testCases := []struct { + name string + length int + charset string + wantErr error + }{ + { + name: "长度小于0", + length: -1, + charset: "123", + wantErr: randx.ErrLengthLessThanZero, + }, + { + name: "随机字符串测试", + length: 100, + charset: "2rg248ry227t@@", + wantErr: nil, + }, + { + name: "随机字符串测试", + length: 100, + charset: "2rg248ry227t@&*($.!", + wantErr: nil, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + code, err := randx.RandStrByCharset(tc.length, tc.charset) + if err != nil { + assert.Equal(t, tc.wantErr, err) } else { - //长度检验 - if len(code) != tc.length { - t.Errorf("expected length: %d but got length:%d ", tc.length, len(code)) - } - //模式检验 - matched, _ := regexp.MatchString(tc.wantMatch, code) - if !matched { - t.Errorf("expected %s but got %s", tc.wantMatch, code) - } + assert.Lenf( + t, + code, + tc.length, + "expected length: %d but got length:%d", + tc.length, len(code)) + assert.True(t, matchFunc(code, tc.charset)) } }) } +} +// goos: linux +// goarch: amd64 +// pkg: github.com/ecodeclub/ekit/randx +// cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz +// BenchmarkRandCode_MIXED/length=1000000-8 1000000000 0.004584 ns/op 0 B/op 0 allocs/op +func BenchmarkRandCode_MIXED(b *testing.B) { + b.Run("length=1000000", func(b *testing.B) { + n := 1000000 + b.StartTimer() + res, err := randx.RandCode(n, randx.TYPE_MIXED) + b.StopTimer() + assert.Nil(b, err) + assert.Len(b, res, n) + }) } From ca5e0ca768819b6fc65e991edd92f52683d28116 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Sat, 13 Jan 2024 16:40:57 +0800 Subject: [PATCH 3/5] update comments --- randx/rand_code.go | 42 ++++++++++++++------- randx/rand_code_test.go | 81 ++++++++++++++++++++++++++++++----------- 2 files changed, 87 insertions(+), 36 deletions(-) diff --git a/randx/rand_code.go b/randx/rand_code.go index 17fb906..03474a9 100644 --- a/randx/rand_code.go +++ b/randx/rand_code.go @@ -23,7 +23,7 @@ import ( var ( ErrTypeNotSupported = errors.New("ekit:不支持的类型") - ErrLengthLessThanZero = errors.New("ekit:长度必须大于0") + ErrLengthLessThanZero = errors.New("ekit:长度必须大于等于0") // deprecated ERRTYPENOTSUPPORTTED = ErrTypeNotSupported ) @@ -34,45 +34,53 @@ const ( // 数字 TYPE_DIGIT TYPE = 1 // 小写字母 - TYPE_LOWER TYPE = 1 << 1 - TYPE_LETTER TYPE = TYPE_LOWER + TYPE_LOWERCASE TYPE = 1 << 1 + TYPE_LETTER TYPE = TYPE_LOWERCASE // 大写字母 - TYPE_UPPER TYPE = 1 << 2 - TYPE_CAPITAL TYPE = TYPE_UPPER + TYPE_UPPERCASE TYPE = 1 << 2 + TYPE_CAPITAL TYPE = TYPE_UPPERCASE // 特殊符号 TYPE_SPECIAL TYPE = 1 << 3 // 混合类型 - TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPER | TYPE_LOWER | TYPE_SPECIAL) + TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPERCASE | TYPE_LOWERCASE | TYPE_SPECIAL) // 数字字符组 CHARSET_DIGIT = "0123456789" // 小写字母字符组 - CHARSET_LOWER = "abcdefghijklmnopqrstuvwxyz" - CHARSET_LETTER = CHARSET_LOWER + CHARSET_LOWERCASE = "abcdefghijklmnopqrstuvwxyz" + CHARSET_LETTER = CHARSET_LOWERCASE // 大写字母字符组 - CHARSET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - CHARSET_CAPITAL = CHARSET_UPPER + CHARSET_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + CHARSET_CAPITAL = CHARSET_UPPERCASE // 特殊字符数组 CHARSET_SPECIAL = " ~!@#$%^&*()_+-=[]{};'\\:\"|,./<>?" ) var ( // 只限于randx包内部使用 - typeCharSetPair = []pair.Pair[TYPE, string]{ + typeCharsetPairs = []pair.Pair[TYPE, string]{ pair.NewPair(TYPE_DIGIT, CHARSET_DIGIT), - pair.NewPair(TYPE_LOWER, CHARSET_LOWER), - pair.NewPair(TYPE_UPPER, CHARSET_UPPER), + pair.NewPair(TYPE_LOWERCASE, CHARSET_LOWERCASE), + pair.NewPair(TYPE_UPPERCASE, CHARSET_UPPERCASE), pair.NewPair(TYPE_SPECIAL, CHARSET_SPECIAL), } ) // RandCode 根据传入的长度和类型生成随机字符串 +// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero +// 请保证输入的 typ 的取值范围在 (0, type.MIXED] 内,否则会返回 ErrTypeNotSupported func RandCode(length int, typ TYPE) (string, error) { + if length < 0 { + return "", ErrLengthLessThanZero + } + if length == 0 { + return "", nil + } if typ > TYPE_MIXED { return "", ErrTypeNotSupported } charset := "" - for _, p := range typeCharSetPair { + for _, p := range typeCharsetPairs { if (typ & p.Key) == p.Key { charset += p.Value } @@ -81,10 +89,16 @@ func RandCode(length int, typ TYPE) (string, error) { } // 根据传入的长度和字符集生成随机字符串 +// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero +// 请保证输入的字符集不为空字符串,否则会返回 ErrTypeNotSupported +// 字符集内部字符可以无序或重复 func RandStrByCharset(length int, charset string) (string, error) { if length < 0 { return "", ErrLengthLessThanZero } + if length == 0 { + return "", nil + } charsetSize := len(charset) if charsetSize == 0 { return "", ErrTypeNotSupported diff --git a/randx/rand_code_test.go b/randx/rand_code_test.go index 83d5186..5a221d2 100644 --- a/randx/rand_code_test.go +++ b/randx/rand_code_test.go @@ -48,14 +48,14 @@ func TestRandCode(t *testing.T) { { name: "数字+小写字母验证码", length: 100, - typ: randx.TYPE_DIGIT | randx.TYPE_LOWER, + typ: randx.TYPE_DIGIT | randx.TYPE_LOWERCASE, wantMatch: "^[a-z0-9]+$", wantErr: nil, }, { name: "数字+大写字母验证码", length: 100, - typ: randx.TYPE_DIGIT | randx.TYPE_UPPER, + typ: randx.TYPE_DIGIT | randx.TYPE_UPPERCASE, wantMatch: "^[A-Z0-9]+$", wantErr: nil, }, @@ -66,17 +66,38 @@ func TestRandCode(t *testing.T) { wantMatch: "^[A-Z]+$", wantErr: nil, }, + { + name: "大写字母验证码(兼容旧版本)", + length: 100, + typ: randx.TYPE_CAPITAL, + wantMatch: "^[A-Z]+$", + wantErr: nil, + }, { name: "大小写字母验证码", length: 100, - typ: randx.TYPE_UPPER | randx.TYPE_LOWER, + typ: randx.TYPE_UPPERCASE | randx.TYPE_LOWERCASE, + wantMatch: "^[a-zA-Z]+$", + wantErr: nil, + }, + { + name: "大小写字母验证码(兼容旧版本)", + length: 100, + typ: randx.TYPE_CAPITAL | randx.TYPE_LETTER, wantMatch: "^[a-zA-Z]+$", wantErr: nil, }, { name: "数字+大小写字母验证码", length: 100, - typ: randx.TYPE_DIGIT | randx.TYPE_UPPER | randx.TYPE_LOWER, + typ: randx.TYPE_DIGIT | randx.TYPE_UPPERCASE | randx.TYPE_LOWERCASE, + wantMatch: "^[0-9a-zA-Z]+$", + wantErr: nil, + }, + { + name: "数字+大小写字母验证码(兼容旧版本)", + length: 100, + typ: randx.TYPE_DIGIT | randx.TYPE_LETTER | randx.TYPE_CAPITAL, wantMatch: "^[0-9a-zA-Z]+$", wantErr: nil, }, @@ -84,7 +105,14 @@ func TestRandCode(t *testing.T) { name: "所有类型验证", length: 100, typ: randx.TYPE_MIXED, - wantMatch: "^[\\S\\s]*$", + wantMatch: "^[\\S\\s]+$", + wantErr: nil, + }, + { + name: "特殊字符类型验证", + length: 100, + typ: randx.TYPE_SPECIAL, + wantMatch: "^[^0-9a-zA-Z]+$", wantErr: nil, }, { @@ -108,25 +136,29 @@ func TestRandCode(t *testing.T) { wantMatch: "", wantErr: randx.ErrLengthLessThanZero, }, + { + name: "长度等于0", + length: 0, + typ: randx.TYPE_MIXED, + wantMatch: "", + wantErr: nil, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { code, err := randx.RandCode(tc.length, tc.typ) - if err != nil { + if tc.wantErr != nil { assert.Equal(t, tc.wantErr, err) - } else { - assert.Lenf( - t, - code, - tc.length, - "expected length: %d but got length:%d", - tc.length, len(code)) - + return + } + assert.Len(t, code, tc.length) + if tc.length > 0 { matched, err := regexp.MatchString(tc.wantMatch, code) assert.Nil(t, err) assert.Truef(t, matched, "expected %s but got %s", tc.wantMatch, code) } + }) } } @@ -152,6 +184,12 @@ func TestRandStrByCharset(t *testing.T) { charset: "123", wantErr: randx.ErrLengthLessThanZero, }, + { + name: "长度等于0", + length: 0, + charset: "123", + wantErr: nil, + }, { name: "随机字符串测试", length: 100, @@ -169,17 +207,16 @@ func TestRandStrByCharset(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { code, err := randx.RandStrByCharset(tc.length, tc.charset) - if err != nil { + if tc.wantErr != nil { assert.Equal(t, tc.wantErr, err) - } else { - assert.Lenf( - t, - code, - tc.length, - "expected length: %d but got length:%d", - tc.length, len(code)) + return + } + + assert.Len(t, code, tc.length) + if tc.length > 0 { assert.True(t, matchFunc(code, tc.charset)) } + }) } } From 466806bc41e1de3966368d881b92fb9ff3eb88a3 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Sat, 13 Jan 2024 16:58:57 +0800 Subject: [PATCH 4/5] update changelog --- .CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.CHANGELOG.md b/.CHANGELOG.md index 5436d92..2e16860 100644 --- a/.CHANGELOG.md +++ b/.CHANGELOG.md @@ -1,6 +1,8 @@ # 开发中 - [syncx: 支持分key加锁](https://github.com/ecodeclub/ekit/pull/224) - [syncx: 添加具有最大申请次数限制的LimitPool](https://github.com/ecodeclub/ekit/pull/233) +- [tuple: 增加Pair的实现](https://github.com/ecodeclub/ekit/pull/237) +- [randx: 重构randx.RandCode的代码,增加对特殊字符的支持](https://github.com/ecodeclub/ekit/pull/241) # v0.0.8 - [atomicx: 泛型封装 atomic.Value](https://github.com/gotomicro/ekit/pull/101) From 4b42722147812421fc2d8e8cdcf6f2e2febd6075 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Sat, 13 Jan 2024 17:09:35 +0800 Subject: [PATCH 5/5] update error --- randx/rand_code.go | 22 ++++++++++------------ randx/rand_code_test.go | 14 ++++++++++---- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/randx/rand_code.go b/randx/rand_code.go index 03474a9..248a020 100644 --- a/randx/rand_code.go +++ b/randx/rand_code.go @@ -22,10 +22,8 @@ import ( ) var ( - ErrTypeNotSupported = errors.New("ekit:不支持的类型") - ErrLengthLessThanZero = errors.New("ekit:长度必须大于等于0") - // deprecated - ERRTYPENOTSUPPORTTED = ErrTypeNotSupported + errTypeNotSupported = errors.New("ekit:不支持的类型") + errLengthLessThanZero = errors.New("ekit:长度必须大于等于0") ) type TYPE int @@ -67,17 +65,17 @@ var ( ) // RandCode 根据传入的长度和类型生成随机字符串 -// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero -// 请保证输入的 typ 的取值范围在 (0, type.MIXED] 内,否则会返回 ErrTypeNotSupported +// 请保证输入的 length >= 0,否则会返回 errLengthLessThanZero +// 请保证输入的 typ 的取值范围在 (0, type.MIXED] 内,否则会返回 errTypeNotSupported func RandCode(length int, typ TYPE) (string, error) { if length < 0 { - return "", ErrLengthLessThanZero + return "", errLengthLessThanZero } if length == 0 { return "", nil } if typ > TYPE_MIXED { - return "", ErrTypeNotSupported + return "", errTypeNotSupported } charset := "" for _, p := range typeCharsetPairs { @@ -89,19 +87,19 @@ func RandCode(length int, typ TYPE) (string, error) { } // 根据传入的长度和字符集生成随机字符串 -// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero -// 请保证输入的字符集不为空字符串,否则会返回 ErrTypeNotSupported +// 请保证输入的 length >= 0,否则会返回 errLengthLessThanZero +// 请保证输入的字符集不为空字符串,否则会返回 errTypeNotSupported // 字符集内部字符可以无序或重复 func RandStrByCharset(length int, charset string) (string, error) { if length < 0 { - return "", ErrLengthLessThanZero + return "", errLengthLessThanZero } if length == 0 { return "", nil } charsetSize := len(charset) if charsetSize == 0 { - return "", ErrTypeNotSupported + return "", errTypeNotSupported } return generate(charset, length, getFirstMask(charsetSize)), nil } diff --git a/randx/rand_code_test.go b/randx/rand_code_test.go index 5a221d2..255162f 100644 --- a/randx/rand_code_test.go +++ b/randx/rand_code_test.go @@ -15,6 +15,7 @@ package randx_test import ( + "errors" "regexp" "strings" "testing" @@ -23,6 +24,11 @@ import ( "github.com/stretchr/testify/assert" ) +var ( + errTypeNotSupported = errors.New("ekit:不支持的类型") + errLengthLessThanZero = errors.New("ekit:长度必须大于等于0") +) + func TestRandCode(t *testing.T) { testCases := []struct { name string @@ -120,21 +126,21 @@ func TestRandCode(t *testing.T) { length: 100, typ: randx.TYPE_MIXED + 1, wantMatch: "", - wantErr: randx.ErrTypeNotSupported, + wantErr: errTypeNotSupported, }, { name: "未定义类型(0)", length: 100, typ: 0, wantMatch: "", - wantErr: randx.ErrTypeNotSupported, + wantErr: errTypeNotSupported, }, { name: "长度小于0", length: -1, typ: 0, wantMatch: "", - wantErr: randx.ErrLengthLessThanZero, + wantErr: errLengthLessThanZero, }, { name: "长度等于0", @@ -182,7 +188,7 @@ func TestRandStrByCharset(t *testing.T) { name: "长度小于0", length: -1, charset: "123", - wantErr: randx.ErrLengthLessThanZero, + wantErr: errLengthLessThanZero, }, { name: "长度等于0",