Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor nanoid package #1608

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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) {

Check warning on line 31 in pkg/tools/nanoid/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/tools/nanoid/utils.go#L31

Added line #L31 was not covered by tests
ifIDLen := kernelmech.LinuxIfMaxLength - len(ifPrefix)
id, err := RandomString(ifIDLen)
id, err := GenerateString(ifIDLen)

Check warning on line 33 in pkg/tools/nanoid/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/tools/nanoid/utils.go#L33

Added line #L33 was not covered by tests
if err != nil {
return "", err
}
Expand Down
Loading