uuid25

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

Uuid25: 25-digit case-insensitive UUID encoding

GitHub tag License

Uuid25 is an alternative UUID representation that shortens a UUID string to just 25 digits using the case-insensitive Base36 encoding. This library provides functionality to convert from the conventional UUID formats to Uuid25 and vice versa.

import "github.com/uuid25/go-uuid25"

// convert from/to string
a, _ := uuid25.Parse("8da942a4-1fbe-4ca6-852c-95c473229c7d")
assert(a.String() == "8dx554y5rzerz1syhqsvsdw8t")
assert(a.ToHyphenated() == "8da942a4-1fbe-4ca6-852c-95c473229c7d")

// convert from/to 128-bit byte array
b := uuid25.FromBytes([]byte{
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
assert(b.String() == "f5lxx1zz5pnorynqglhzmsp33")
for _, x := range b.ToBytes() {
  assert(x == 0xff)
}

// convert from/to other popular textual representations
c := make([]uuid25.Uuid25, 4)
c[0], _ = uuid25.Parse("e7a1d63b711744238988afcf12161878")
c[1], _ = uuid25.Parse("e7a1d63b-7117-4423-8988-afcf12161878")
c[2], _ = uuid25.Parse("{e7a1d63b-7117-4423-8988-afcf12161878}")
c[3], _ = uuid25.Parse("urn:uuid:e7a1d63b-7117-4423-8988-afcf12161878")
for _, x := range c {
  assert(x.String() == "dpoadk8izg9y4tte7vy1xt94o")
}

d, _ := uuid25.Parse("dpoadk8izg9y4tte7vy1xt94o")
assert(d.ToHex() == "e7a1d63b711744238988afcf12161878")
assert(d.ToHyphenated() == "e7a1d63b-7117-4423-8988-afcf12161878")
assert(d.ToBraced() == "{e7a1d63b-7117-4423-8988-afcf12161878}")
assert(d.ToUrn() == "urn:uuid:e7a1d63b-7117-4423-8988-afcf12161878")

func assert(c bool) { if !c { panic("assertion failed") } }

License

Licensed under the Apache License, Version 2.0.

See also

Documentation

Overview

Uuid25: 25-digit case-insensitive UUID encoding

Uuid25 is an alternative UUID representation that shortens a UUID string to just 25 digits using the case-insensitive Base36 encoding. This library provides functionality to convert from the conventional UUID formats to Uuid25 and vice versa.

import "github.com/uuid25/go-uuid25"

// convert from/to string
a, _ := uuid25.Parse("8da942a4-1fbe-4ca6-852c-95c473229c7d")
assert(a.String() == "8dx554y5rzerz1syhqsvsdw8t")
assert(a.ToHyphenated() == "8da942a4-1fbe-4ca6-852c-95c473229c7d")

// convert from/to 128-bit byte array
b := uuid25.FromBytes([]byte{
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
assert(b.String() == "f5lxx1zz5pnorynqglhzmsp33")
for _, x := range b.ToBytes() {
  assert(x == 0xff)
}

// convert from/to other popular textual representations
c := make([]uuid25.Uuid25, 4)
c[0], _ = uuid25.Parse("e7a1d63b711744238988afcf12161878")
c[1], _ = uuid25.Parse("e7a1d63b-7117-4423-8988-afcf12161878")
c[2], _ = uuid25.Parse("{e7a1d63b-7117-4423-8988-afcf12161878}")
c[3], _ = uuid25.Parse("urn:uuid:e7a1d63b-7117-4423-8988-afcf12161878")
for _, x := range c {
  assert(x.String() == "dpoadk8izg9y4tte7vy1xt94o")
}

d, _ := uuid25.Parse("dpoadk8izg9y4tte7vy1xt94o")
assert(d.ToHex() == "e7a1d63b711744238988afcf12161878")
assert(d.ToHyphenated() == "e7a1d63b-7117-4423-8988-afcf12161878")
assert(d.ToBraced() == "{e7a1d63b-7117-4423-8988-afcf12161878}")
assert(d.ToUrn() == "urn:uuid:e7a1d63b-7117-4423-8988-afcf12161878")

func assert(c bool) { if !c { panic("assertion failed") } }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Uuid25

type Uuid25 string

Primary value type containing the Uuid25 representation of a UUID.

A valid value of this type must be constructed through FromBytes() or one of Parse*() functions.

func FromBytes

func FromBytes(uuidBytes []byte) Uuid25

Creates an instance from a 16-byte UUID binary representation.

func Parse

func Parse(uuidString string) (Uuid25, error)

Creates an instance from a UUID string representation.

This method accepts the following formats:

  • 25-digit Base36 Uuid25 format: `3ud3gtvgolimgu9lah6aie99o`
  • 32-digit hexadecimal format without hyphens: `40eb9860cf3e45e2a90eb82236ac806c`
  • 8-4-4-4-12 hyphenated format: `40eb9860-cf3e-45e2-a90e-b82236ac806c`
  • Hyphenated format with surrounding braces: `{40eb9860-cf3e-45e2-a90e-b82236ac806c}`
  • RFC 4122 URN format: `urn:uuid:40eb9860-cf3e-45e2-a90e-b82236ac806c`

func ParseBraced

func ParseBraced(uuidString string) (Uuid25, error)

Creates an instance from the hyphenated format with surrounding braces: `{40eb9860-cf3e-45e2-a90e-b82236ac806c}`.

func ParseHex

func ParseHex(uuidString string) (Uuid25, error)

Creates an instance from the 32-digit hexadecimal format without hyphens: `40eb9860cf3e45e2a90eb82236ac806c`.

func ParseHyphenated

func ParseHyphenated(uuidString string) (Uuid25, error)

Creates an instance from the 8-4-4-4-12 hyphenated format: `40eb9860-cf3e-45e2-a90e-b82236ac806c`.

func ParseUrn

func ParseUrn(uuidString string) (Uuid25, error)

Creates an instance from the RFC 4122 URN format: `urn:uuid:40eb9860-cf3e-45e2-a90e-b82236ac806c`.

func ParseUuid25

func ParseUuid25(uuidString string) (Uuid25, error)

Creates an instance from the 25-digit Base36 Uuid25 format: `3ud3gtvgolimgu9lah6aie99o`.

func (Uuid25) MarshalBinary

func (uuid25 Uuid25) MarshalBinary() (data []byte, err error)

Implements the encoding.BinaryMarshaler interface.

func (Uuid25) MarshalText

func (uuid25 Uuid25) MarshalText() (text []byte, err error)

Implements the encoding.TextMarshaler interface.

func (*Uuid25) Scan

func (uuid25 *Uuid25) Scan(src any) error

Implements the sql.Scanner interface.

func (Uuid25) String

func (uuid25 Uuid25) String() string

Returns the 25-digit Uuid25 representation of this type.

func (Uuid25) ToBraced

func (uuid25 Uuid25) ToBraced() string

Formats this type in the hyphenated format with surrounding braces: `{40eb9860-cf3e-45e2-a90e-b82236ac806c}`.

func (Uuid25) ToBytes

func (uuid25 Uuid25) ToBytes() [16]byte

Converts this type into the 16-byte binary representation of a UUID.

func (Uuid25) ToHex

func (uuid25 Uuid25) ToHex() string

Formats this type in the 32-digit hexadecimal format without hyphens: `40eb9860cf3e45e2a90eb82236ac806c`.

func (Uuid25) ToHyphenated

func (uuid25 Uuid25) ToHyphenated() string

Formats this type in the 8-4-4-4-12 hyphenated format: `40eb9860-cf3e-45e2-a90e-b82236ac806c`.

func (Uuid25) ToUrn

func (uuid25 Uuid25) ToUrn() string

Formats this type in the RFC 4122 URN format: `urn:uuid:40eb9860-cf3e-45e2-a90e-b82236ac806c`.

func (*Uuid25) UnmarshalBinary

func (uuid25 *Uuid25) UnmarshalBinary(data []byte) error

Implements the encoding.BinaryUnmarshaler interface.

func (*Uuid25) UnmarshalText

func (uuid25 *Uuid25) UnmarshalText(text []byte) error

Implements the encoding.TextUnmarshaler interface.

func (Uuid25) Value

func (uuid25 Uuid25) Value() (driver.Value, error)

Implements the driver.Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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