aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/rand
diff options
context:
space:
mode:
authorvitalyisaev <vitalyisaev@ydb.tech>2023-12-12 21:55:07 +0300
committervitalyisaev <vitalyisaev@ydb.tech>2023-12-12 22:25:10 +0300
commit4967f99474a4040ba150eb04995de06342252718 (patch)
treec9c118836513a8fab6e9fcfb25be5d404338bca7 /vendor/github.com/aws/smithy-go/rand
parent2ce9cccb9b0bdd4cd7a3491dc5cbf8687cda51de (diff)
downloadydb-4967f99474a4040ba150eb04995de06342252718.tar.gz
YQ Connector: prepare code base for S3 integration
1. Кодовая база Коннектора переписана с помощью Go дженериков так, чтобы добавление нового источника данных (в частности S3 + csv) максимально переиспользовало имеющийся код (чтобы сохранялась логика нарезания на блоки данных, учёт трафика и пр.) 2. API Connector расширено для работы с S3, но ещё пока не протестировано.
Diffstat (limited to 'vendor/github.com/aws/smithy-go/rand')
-rw-r--r--vendor/github.com/aws/smithy-go/rand/doc.go3
-rw-r--r--vendor/github.com/aws/smithy-go/rand/gotest/ya.make5
-rw-r--r--vendor/github.com/aws/smithy-go/rand/rand.go31
-rw-r--r--vendor/github.com/aws/smithy-go/rand/uuid.go87
-rw-r--r--vendor/github.com/aws/smithy-go/rand/uuid_test.go47
-rw-r--r--vendor/github.com/aws/smithy-go/rand/ya.make17
6 files changed, 190 insertions, 0 deletions
diff --git a/vendor/github.com/aws/smithy-go/rand/doc.go b/vendor/github.com/aws/smithy-go/rand/doc.go
new file mode 100644
index 0000000000..f8b25d5625
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/doc.go
@@ -0,0 +1,3 @@
+// Package rand provides utilities for creating and working with random value
+// generators.
+package rand
diff --git a/vendor/github.com/aws/smithy-go/rand/gotest/ya.make b/vendor/github.com/aws/smithy-go/rand/gotest/ya.make
new file mode 100644
index 0000000000..e3a1376f42
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/gotest/ya.make
@@ -0,0 +1,5 @@
+GO_TEST_FOR(vendor/github.com/aws/smithy-go/rand)
+
+LICENSE(Apache-2.0)
+
+END()
diff --git a/vendor/github.com/aws/smithy-go/rand/rand.go b/vendor/github.com/aws/smithy-go/rand/rand.go
new file mode 100644
index 0000000000..9c479f62b5
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/rand.go
@@ -0,0 +1,31 @@
+package rand
+
+import (
+ "crypto/rand"
+ "fmt"
+ "io"
+ "math/big"
+)
+
+func init() {
+ Reader = rand.Reader
+}
+
+// Reader provides a random reader that can reset during testing.
+var Reader io.Reader
+
+// Int63n returns a int64 between zero and value of max, read from an io.Reader source.
+func Int63n(reader io.Reader, max int64) (int64, error) {
+ bi, err := rand.Int(reader, big.NewInt(max))
+ if err != nil {
+ return 0, fmt.Errorf("failed to read random value, %w", err)
+ }
+
+ return bi.Int64(), nil
+}
+
+// CryptoRandInt63n returns a random int64 between zero and value of max
+// obtained from the crypto rand source.
+func CryptoRandInt63n(max int64) (int64, error) {
+ return Int63n(Reader, max)
+}
diff --git a/vendor/github.com/aws/smithy-go/rand/uuid.go b/vendor/github.com/aws/smithy-go/rand/uuid.go
new file mode 100644
index 0000000000..dc81cbc68a
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/uuid.go
@@ -0,0 +1,87 @@
+package rand
+
+import (
+ "encoding/hex"
+ "io"
+)
+
+const dash byte = '-'
+
+// UUIDIdempotencyToken provides a utility to get idempotency tokens in the
+// UUID format.
+type UUIDIdempotencyToken struct {
+ uuid *UUID
+}
+
+// NewUUIDIdempotencyToken returns a idempotency token provider returning
+// tokens in the UUID random format using the reader provided.
+func NewUUIDIdempotencyToken(r io.Reader) *UUIDIdempotencyToken {
+ return &UUIDIdempotencyToken{uuid: NewUUID(r)}
+}
+
+// GetIdempotencyToken returns a random UUID value for Idempotency token.
+func (u UUIDIdempotencyToken) GetIdempotencyToken() (string, error) {
+ return u.uuid.GetUUID()
+}
+
+// UUID provides computing random UUID version 4 values from a random source
+// reader.
+type UUID struct {
+ randSrc io.Reader
+}
+
+// NewUUID returns an initialized UUID value that can be used to retrieve
+// random UUID version 4 values.
+func NewUUID(r io.Reader) *UUID {
+ return &UUID{randSrc: r}
+}
+
+// GetUUID returns a random UUID version 4 string representation sourced from the random reader the
+// UUID was created with. Returns an error if unable to compute the UUID.
+func (r *UUID) GetUUID() (string, error) {
+ var b [16]byte
+ if _, err := io.ReadFull(r.randSrc, b[:]); err != nil {
+ return "", err
+ }
+ r.makeUUIDv4(b[:])
+ return format(b), nil
+}
+
+// GetBytes returns a byte slice containing a random UUID version 4 sourced from the random reader the
+// UUID was created with. Returns an error if unable to compute the UUID.
+func (r *UUID) GetBytes() (u []byte, err error) {
+ u = make([]byte, 16)
+ if _, err = io.ReadFull(r.randSrc, u); err != nil {
+ return u, err
+ }
+ r.makeUUIDv4(u)
+ return u, nil
+}
+
+func (r *UUID) makeUUIDv4(u []byte) {
+ // 13th character is "4"
+ u[6] = (u[6] & 0x0f) | 0x40 // Version 4
+ // 17th character is "8", "9", "a", or "b"
+ u[8] = (u[8] & 0x3f) | 0x80 // Variant most significant bits are 10x where x can be either 1 or 0
+}
+
+// Format returns the canonical text representation of a UUID.
+// This implementation is optimized to not use fmt.
+// Example: 82e42f16-b6cc-4d5b-95f5-d403c4befd3d
+func format(u [16]byte) string {
+ // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
+
+ var scratch [36]byte
+
+ hex.Encode(scratch[:8], u[0:4])
+ scratch[8] = dash
+ hex.Encode(scratch[9:13], u[4:6])
+ scratch[13] = dash
+ hex.Encode(scratch[14:18], u[6:8])
+ scratch[18] = dash
+ hex.Encode(scratch[19:23], u[8:10])
+ scratch[23] = dash
+ hex.Encode(scratch[24:], u[10:])
+
+ return string(scratch[:])
+}
diff --git a/vendor/github.com/aws/smithy-go/rand/uuid_test.go b/vendor/github.com/aws/smithy-go/rand/uuid_test.go
new file mode 100644
index 0000000000..b22497757d
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/uuid_test.go
@@ -0,0 +1,47 @@
+package rand_test
+
+import (
+ "bytes"
+ mathrand "math/rand"
+ "testing"
+ "time"
+
+ "github.com/aws/smithy-go/rand"
+)
+
+func TestUUID(t *testing.T) {
+ randSrc := make([]byte, 32)
+ for i := 16; i < len(randSrc); i++ {
+ randSrc[i] = 1
+ }
+
+ uuid := rand.NewUUID(bytes.NewReader(randSrc))
+
+ v, err := uuid.GetUUID()
+ if err != nil {
+ t.Fatalf("expect no error getting zero UUID, got %v", err)
+ }
+ if e, a := `00000000-0000-4000-8000-000000000000`, v; e != a {
+ t.Errorf("expect %v, got %v", e, a)
+ }
+
+ v, err = uuid.GetUUID()
+ if err != nil {
+ t.Fatalf("expect no error getting ones UUID, got %v", err)
+ }
+ if e, a := `01010101-0101-4101-8101-010101010101`, v; e != a {
+ t.Errorf("expect %v, got %v", e, a)
+ }
+}
+
+func BenchmarkUUID_GetUUID(b *testing.B) {
+ src := mathrand.NewSource(time.Now().Unix())
+ uuid := rand.NewUUID(mathrand.New(src))
+
+ for i := 0; i < b.N; i++ {
+ _, err := uuid.GetUUID()
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}
diff --git a/vendor/github.com/aws/smithy-go/rand/ya.make b/vendor/github.com/aws/smithy-go/rand/ya.make
new file mode 100644
index 0000000000..96d268d8b8
--- /dev/null
+++ b/vendor/github.com/aws/smithy-go/rand/ya.make
@@ -0,0 +1,17 @@
+GO_LIBRARY()
+
+LICENSE(Apache-2.0)
+
+SRCS(
+ doc.go
+ rand.go
+ uuid.go
+)
+
+GO_XTEST_SRCS(uuid_test.go)
+
+END()
+
+RECURSE(
+ gotest
+)