> be fist commit

This commit is contained in:
2020-04-13 23:38:41 +02:00
commit 4cce632708
69 changed files with 7852 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
package generate
import (
"crypto/rand"
"encoding/binary"
mathRand "math/rand"
"strconv"
)
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
func generateWeak(length int, prefix string) string {
result := prefix
for len(result) < length {
result += string(alphabet[mathRand.Intn(len(alphabet))])
}
return result
}
// Generate generates an ID either strongly or weakly depending on the system.
func Generate(length int, prefix string) string {
var buffer [32]byte
result := prefix
offset := 0
_, err := rand.Read(buffer[:32])
if err != nil {
return generateWeak(length, prefix)
}
for len(result) < length {
result += strconv.FormatUint(binary.LittleEndian.Uint64(buffer[offset:]), 36)
offset += 8
if offset >= 32 {
_, err = rand.Read(buffer[:32])
if err != nil {
return generateWeak(length, prefix)
}
offset = 0
}
}
return result[:length]
}
+9
View File
@@ -0,0 +1,9 @@
package generate
func ItemID() string {
return Generate(32, "I")
}
func SessionID() string {
return Generate(32, "S")
}
+18
View File
@@ -0,0 +1,18 @@
package xlerrors
type notFoundError struct {
Subject string
}
func (err *notFoundError) Error() string {
return err.Subject + " not found"
}
func NotFound(subject string) error {
return &notFoundError{Subject: subject}
}
func IsNotFound(err error) bool {
_, ok := err.(*notFoundError)
return ok
}