Search Apps Documentation Source Content File Folder Download Copy Actions Download

id.gno

1.30 Kb · 58 lines
 1package boards
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/nt/seqid/v0"
 8)
 9
10const paddedStringLen = 10
11
12// ID defines a type for unique identifiers.
13type ID uint64
14
15// String returns the ID as a string.
16func (id ID) String() string {
17	return strconv.FormatUint(uint64(id), 10)
18}
19
20// PaddedString returns the ID as a 10 character string padded with zeroes.
21// This value can be used for indexing by ID.
22func (id ID) PaddedString() string {
23	s := id.String()
24	return strings.Repeat("0", paddedStringLen-len(s)) + s
25}
26
27// Key returns the ID as a string which can be used to index by ID.
28func (id ID) Key() string {
29	return seqid.ID(id).String()
30}
31
32// IdentifierGenerator defines an interface for sequential unique identifier generators.
33type IdentifierGenerator interface {
34	// Current returns the last generated ID.
35	Last() ID
36
37	// Next generates a new ID or panics if increasing ID overflows.
38	Next() ID
39}
40
41// NewIdentifierGenerator creates a new sequential unique identifier generator.
42func NewIdentifierGenerator() IdentifierGenerator {
43	return &idGenerator{}
44}
45
46type idGenerator struct {
47	last seqid.ID
48}
49
50// Current returns the last generated ID.
51func (g idGenerator) Last() ID {
52	return ID(g.last)
53}
54
55// Next generates a new ID or panics if increasing ID overflows.
56func (g *idGenerator) Next() ID {
57	return ID(g.last.Next())
58}