small RandomUtil prettification
This commit is contained in:
parent
204d718927
commit
91476a29b2
@ -16,54 +16,53 @@ const (
|
|||||||
CharsetNumeric
|
CharsetNumeric
|
||||||
)
|
)
|
||||||
|
|
||||||
type charsetPart struct {
|
type charsetBlock struct {
|
||||||
Offset int
|
Offset int
|
||||||
Size int
|
Size int
|
||||||
}
|
}
|
||||||
|
|
||||||
var charsets = map[Charset][]charsetPart{}
|
|
||||||
|
|
||||||
func NewRand() *RandomUtil {
|
func NewRand() *RandomUtil {
|
||||||
charsetLettersLower := charsetPart{ //CharsetLettersLower
|
charsetLettersLower := charsetBlock{
|
||||||
Offset: 0x41,
|
Offset: 0x41,
|
||||||
Size: 26,
|
Size: 26,
|
||||||
}
|
}
|
||||||
|
|
||||||
charsetLettersUpper := charsetPart{ //CharsetLettersUpper
|
charsetLettersUpper := charsetBlock{
|
||||||
Offset: 0x61,
|
Offset: 0x61,
|
||||||
Size: 26,
|
Size: 26,
|
||||||
}
|
}
|
||||||
|
|
||||||
charsetNumeric := charsetPart{ //CharsetLettersNumeric
|
charsetNumeric := charsetBlock{
|
||||||
Offset: 0x30,
|
Offset: 0x30,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
charsets = map[Charset][]charsetPart{
|
return &RandomUtil{
|
||||||
|
charsets: map[Charset][]charsetBlock{
|
||||||
CharsetNumeric: {charsetNumeric},
|
CharsetNumeric: {charsetNumeric},
|
||||||
CharsetLettersLower: {charsetLettersLower},
|
CharsetLettersLower: {charsetLettersLower},
|
||||||
CharsetLettersUpper: {charsetLettersUpper},
|
CharsetLettersUpper: {charsetLettersUpper},
|
||||||
CharsetLetters: {charsetLettersLower, charsetLettersUpper},
|
CharsetLetters: {charsetLettersLower, charsetLettersUpper},
|
||||||
CharsetAll: {charsetLettersLower, charsetLettersUpper, charsetNumeric},
|
CharsetAll: {charsetLettersLower, charsetLettersUpper, charsetNumeric},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RandomUtil{}
|
type RandomUtil struct {
|
||||||
|
charsets map[Charset][]charsetBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
type RandomUtil struct{}
|
|
||||||
|
|
||||||
func (r *RandomUtil) RandomID(outputLenght int, charset Charset) string {
|
func (r *RandomUtil) RandomID(outputLenght int, charset Charset) string {
|
||||||
src := rand.NewSource(time.Now().UnixMicro())
|
src := rand.NewSource(time.Now().UnixMicro())
|
||||||
randGen := rand.New(src)
|
randGen := rand.New(src)
|
||||||
|
|
||||||
charsetData := charsets[charset]
|
charsetBlocks := r.charsets[charset]
|
||||||
|
|
||||||
builder := strings.Builder{}
|
builder := strings.Builder{}
|
||||||
for i := 0; i < outputLenght; i++ {
|
for i := 0; i < outputLenght; i++ {
|
||||||
charsetIdx := randGen.Int() % len(charsetData)
|
charsetBlock := charsetBlocks[randGen.Int()%len(charsetBlocks)]
|
||||||
charsetPart := charsetData[charsetIdx]
|
|
||||||
|
|
||||||
byte := charsetPart.Offset + (randGen.Int() % charsetPart.Size)
|
byte := charsetBlock.Offset + (randGen.Int() % charsetBlock.Size)
|
||||||
builder.WriteRune(rune(byte))
|
builder.WriteRune(rune(byte))
|
||||||
}
|
}
|
||||||
return builder.String()
|
return builder.String()
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
|
// Modified recovery from gin, use own logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/src/logger"
|
"backend/src/logger"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user