From edee168c48df9d6933255c71ccecffd98f498c82 Mon Sep 17 00:00:00 2001 From: Denis Tingaikin Date: Fri, 5 Apr 2024 13:31:06 +0300 Subject: [PATCH] refactor nanoid package (#1608) Signed-off-by: denis-tingaikin --- pkg/networkservice/common/mechanisms/kernel/client.go | 2 +- pkg/networkservice/common/mechanisms/kernel/server.go | 2 +- pkg/tools/nanoid/generator.go | 11 ++--------- pkg/tools/nanoid/generator_test.go | 6 +++--- pkg/tools/nanoid/utils.go | 6 +++--- 5 files changed, 10 insertions(+), 17 deletions(-) diff --git a/pkg/networkservice/common/mechanisms/kernel/client.go b/pkg/networkservice/common/mechanisms/kernel/client.go index 36f8200b1..60aac1093 100644 --- a/pkg/networkservice/common/mechanisms/kernel/client.go +++ b/pkg/networkservice/common/mechanisms/kernel/client.go @@ -42,7 +42,7 @@ type kernelMechanismClient struct { // NewClient - returns client that sets kernel preferred mechanism func NewClient(opts ...Option) networkservice.NetworkServiceClient { o := &options{ - interfaceNameGenerator: nanoid.LinuxInterfaceNameGenerator, + interfaceNameGenerator: nanoid.GenerateLinuxInterfaceName, } for _, opt := range opts { opt(o) diff --git a/pkg/networkservice/common/mechanisms/kernel/server.go b/pkg/networkservice/common/mechanisms/kernel/server.go index d86609c63..4f66c9584 100644 --- a/pkg/networkservice/common/mechanisms/kernel/server.go +++ b/pkg/networkservice/common/mechanisms/kernel/server.go @@ -40,7 +40,7 @@ type kernelMechanismServer struct { // NewServer - creates a NetworkServiceServer that requests a kernel interface and populates the netns inode func NewServer(opts ...Option) networkservice.NetworkServiceServer { o := &options{ - interfaceNameGenerator: nanoid.LinuxInterfaceNameGenerator, + interfaceNameGenerator: nanoid.GenerateLinuxInterfaceName, } for _, opt := range opts { opt(o) diff --git a/pkg/tools/nanoid/generator.go b/pkg/tools/nanoid/generator.go index 8ff4e7055..4e6f43e6d 100644 --- a/pkg/tools/nanoid/generator.go +++ b/pkg/tools/nanoid/generator.go @@ -59,16 +59,9 @@ func generateRandomBuffer(generator io.Reader, step int) ([]byte, error) { return buffer, nil } -// New creates a new nanoid generator with specified options -func New(opt ...Option) func(int) (string, error) { - return func(size int) (string, error) { - return RandomString(size, opt...) - } -} - -// RandomString generates a random string based on size. +// GenerateString generates a random string based on size. // Original JavaScript implementation: https://github.com/ai/nanoid/blob/main/README.md -func RandomString(size int, opt ...Option) (string, error) { +func GenerateString(size int, opt ...Option) (string, error) { opts := &generatorOpts{ alphabet: DefaultAlphabet, randomByteGenerator: rand.Reader, diff --git a/pkg/tools/nanoid/generator_test.go b/pkg/tools/nanoid/generator_test.go index 9c814d1df..bc7d70dea 100644 --- a/pkg/tools/nanoid/generator_test.go +++ b/pkg/tools/nanoid/generator_test.go @@ -32,7 +32,7 @@ const ( func TestNoCollisions(t *testing.T) { used := make(map[string]bool) for i := 0; i < 50*1000; i++ { - id, err := nanoid.RandomString(idLen) + id, err := nanoid.GenerateString(idLen) require.NoError(t, err) _, ok := used[id] require.False(t, ok, "Collision detected for id: %s", id) @@ -45,7 +45,7 @@ func TestFlatDistribution(t *testing.T) { chars := make(map[rune]int) for i := 0; i < count; i++ { - id, err := nanoid.RandomString(idLen) + id, err := nanoid.GenerateString(idLen) require.NoError(t, err, "Error generating nanoid") for _, char := range id { chars[char]++ @@ -71,7 +71,7 @@ func TestFlatDistribution(t *testing.T) { func TestCustomAlphabet(t *testing.T) { alphabet := "abcd" - id, err := nanoid.RandomString(idLen, nanoid.WithAlphabet(alphabet)) + id, err := nanoid.GenerateString(idLen, nanoid.WithAlphabet(alphabet)) require.NoError(t, err) for _, c := range id { diff --git a/pkg/tools/nanoid/utils.go b/pkg/tools/nanoid/utils.go index cfcf0ace5..c685b29c2 100644 --- a/pkg/tools/nanoid/utils.go +++ b/pkg/tools/nanoid/utils.go @@ -26,11 +26,11 @@ const ( ifPrefix = "nsm" ) -// LinuxInterfaceNameGenerator - returns a random interface name with "nsm" prefix +// GenerateLinuxInterfaceName - returns a random interface name with "nsm" prefix // to achieve a 1% chance of name collision, you need to generate approximately 68 billon names -func LinuxInterfaceNameGenerator() (string, error) { +func GenerateLinuxInterfaceName() (string, error) { ifIDLen := kernelmech.LinuxIfMaxLength - len(ifPrefix) - id, err := RandomString(ifIDLen) + id, err := GenerateString(ifIDLen) if err != nil { return "", err }