Skip to content

Commit

Permalink
refactor nanoid package (#1608)
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin authored Apr 5, 2024
1 parent 77fc153 commit edee168
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/kernel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/mechanisms/kernel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions pkg/tools/nanoid/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/nanoid/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]++
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/tools/nanoid/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit edee168

Please sign in to comment.