namespace

package
v0.3.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package namespace implements a namespace to solve name collision.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NumberSuffix

func NumberSuffix(name string, cnt int) string

NumberSuffix renames names by adding a numeric suffix.

func UnderscoreSuffix

func UnderscoreSuffix(name string, cnt int) string

UnderscoreSuffix renames names by adding underscores as suffix.

Types

type Namespace

type Namespace interface {
	// Add tries to create a mapping from ID to name. If name
	// is used by other ID, then a renamed one will be returned.
	Add(name, id string) (result string)

	// Get returns the Added name of id. If none found, the result will
	// be an empty string.
	Get(id string) (name string)

	// ID is the reverse operation of Get. It returns the ID that maps
	// to the given name.
	ID(name string) (id string)

	// Reserve tries to create a mapping from ID to name without any
	// renaming. The result indicates whether it succeeds.
	Reserve(name, id string) (ok bool)

	// MustReserve tries to create a mapping from ID to name without any
	// renaming. If the name is occupied, this function will panic.
	MustReserve(name, id string)

	// Iterate passes each key and value in the namespace to f.
	// If f returns false, Iterate stops the iteration.
	Iterate(v func(name, id string) (shouldContinue bool))
}

Namespace is a container that maps IDs to names (both strings) while avoids collision between names by renaming.

func NewNamespace

func NewNamespace(rename func(string, int) string) Namespace

NewNamespace creates a namespace with the given rename function. The first parameter for the rename function is the preferred name which is not usable due to collision. The second parameter indicates how many different IDs has claimed the same name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL