goid

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: MIT Imports: 6 Imported by: 0

README

Go Report Card Go Docs

goid

A package to manage ID

Documentattion

Installation
go get github.com/bearchit/goid
Usage
package main

import (
	"fmt"
	"github.com/bearchit/goid"
)

func main() {
	// Create a new ID generator
	// Using dash character with UUIDv4 string
	g := goid.NewUuidV4Generator(true)

	// Generate ID
	id := g.Generate()

	fmt.Println(id.Nil())    // Returns boolean whether id is nil or not
	fmt.Println(id.String()) // Converts ID to string
	fmt.Println(id)

	target := g.Generate()
	ids := goid.NewIDs(
		target,
		g.Generate(),
		g.Generate(),
	)
	fmt.Println(ids)
	fmt.Println(ids.Contains(target))

	// Create a UUIDv4 generator without dash
	g2 := goid.NewUuidV4Generator(false)
	fmt.Println(g2.Generate())
}
Namespace Generator
Default delimiter
g := goid.NewNamespaceGenerator("com")
id := g.Generate("github", "bearchit", "goid")
// id = com.github.bearchit.goid
Custom delimiter
g := goid.NewNamespaceGenerator("com", goid.WithDelimiter("-"))
id := g.Generate("github", "bearchit", "goid")
// id = com-github.bearchit-goid

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultGenerator     = NewUuidV4Generator(true)
	MockedID             = ID("mocked-id")
	DefaultMockGenerator = MockGenerator{/* contains filtered or unexported fields */}
)

Functions

func NewGlobalGenerator added in v0.5.0

func NewGlobalGenerator(
	opts ...func(*globalGenerator),
) *globalGenerator

func NewNamespaceGenerator added in v0.4.0

func NewNamespaceGenerator(
	namespace string,
	opts ...func(*namespaceGenerator),
) *namespaceGenerator

func WithDelimiter added in v0.4.0

func WithDelimiter(delimiter string) func(opt *namespaceGenerator)

func WithGenerator added in v0.6.0

func WithGenerator(generator Generator) func(*globalGenerator)

Types

type Generator

type Generator interface {
	Generate() ID
}

func NewMock

func NewMock(id ID) Generator

func NewUuidV4Generator

func NewUuidV4Generator(dash bool) Generator

type GlobalID added in v0.6.0

type GlobalID struct {
	Prefix    string
	Delimiter string
	ID        ID
}

func ParseGlobalID added in v0.6.0

func ParseGlobalID(id ID) (*GlobalID, error)

type ID

type ID string
const NilID ID = ""

func FromString added in v0.2.0

func FromString(id string) ID

func Generate

func Generate() ID

func (ID) Nil added in v0.3.0

func (id ID) Nil() bool

func (ID) String

func (id ID) String() string

type IDs added in v0.3.0

type IDs []ID

func NewIDs added in v0.3.0

func NewIDs(id ...ID) IDs

func (*IDs) Add added in v0.6.0

func (ids *IDs) Add(others ...ID)

func (IDs) Contains added in v0.3.0

func (ids IDs) Contains(others ...ID) bool

type MockGenerator

type MockGenerator struct {
	// contains filtered or unexported fields
}

func (MockGenerator) Generate

func (g MockGenerator) Generate() ID

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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