cor

package
v0.0.0-...-9cd5b15 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: BSD-2-Clause Imports: 8 Imported by: 12

Documentation

Overview

Package cor has some utility function for working with numeric and character literal values.

The literal values of raw, uuid, time and span all have a formatter, string parser and a pointer parser function that returns nil on error.

The primitive go types bool, int64, float64 and string have a pointer helper that returns a pointer to argument value.

Index

Constants

This section is empty.

Variables

View Source
var ErrSpan = fmt.Errorf("invalid span format")

ErrSpan indicates an invalid input format when parsing a span.

View Source
var ErrUUID = fmt.Errorf("invalid uuid format")

ErrUUID indicates an invalid input format when parsing an uuid.

Functions

func Any

func Any(v interface{}) *interface{}

Any returns a pointer to v.

func Bool

func Bool(v bool) *bool

Bool returns a pointer to v.

func Cased

func Cased(s string) string

Cased returns n starting with uppercase letter. This function is especially used for go code gen.

func Ctrl

func Ctrl(r rune) int

func Digit

func Digit(r rune) bool

Digit tests whether r is an ascii digit.

func FormatHex

func FormatHex(v []byte) string

FormatHex returns v as string starting with '\x' and followed by the bytes as lower hex.

func FormatRaw

func FormatRaw(v []byte) string

FormatRaw returns v as string, plain texts are returned as is, otherwise FormatHex is used.

func FormatSpan

func FormatSpan(v time.Duration) string

FormatSpan returns v in the string format '-123:04:05.678'.

func FormatTime

func FormatTime(v time.Time) string

FormatTime returns v as string in the RFC3339 format with milliseconds.

func FormatUUID

func FormatUUID(v [16]byte) string

FormatUUID returns v as string in the canonical uuid format.

func Int

func Int(v int64) *int64

Int returns a pointer to v.

func IsCased

func IsCased(s string) bool

func IsKey

func IsKey(s string) bool

IsKey tests whether s is a valid key.

func IsName

func IsName(s string) bool

IsName tests whether s is a valid name.

func IsSym

func IsSym(s string) bool

IsSym tests whether s is a valid symbol.

func KeyPart

func KeyPart(r rune) bool

KeyPart tests whether r is ascii lowercase letter, digit, dot or underscore.

func KeyStart

func KeyStart(r rune) bool

KeyStart tests whether r is ascii lowercase letter or underscore.

func Keyed

func Keyed(s string) string

Keyed returns the s starting with the first name segment as key.

func Keyify

func Keyify(s string) string

func Letter

func Letter(r rune) bool

Letter tests whether r is an ascii letter.

func Milli

func Milli(v time.Duration) int64

Milli returns the milliseconds in v as integer.

func MilliSpan

func MilliSpan(n int64) time.Duration

MilliSpan returns milliseconds n as time duration.

func MustParseUUID

func MustParseUUID(s string) [16]byte

func NamePart

func NamePart(r rune) bool

NamePart tests whether r is ascii letter, digit or underscore.

func NameStart

func NameStart(r rune) bool

NameStart tests whether r is ascii letter or underscore.

func NewUUID

func NewUUID() (id [16]byte)

NewUUID returns 16 random bytes from crypto/rand.

func ParseRaw

func ParseRaw(s string) ([]byte, error)

ParseRaw parses s and returns a byte slice or an error.

func ParseSpan

func ParseSpan(s string) (time.Duration, error)

ParseSpan parses s and returns a time duration or an error. It accepts two formats '-123h4m5s678ms' and '-123:04:05.678'.

func ParseTime

func ParseTime(s string) (time.Time, error)

ParseTime parses s and return a time or error. It accepts variations of the RFC3339 format:

2006-01-02([T ]15:04(:05:999999999)?)?(Z|[+-]07([:]?00)?)?

The returned time will be parsed in the local timezone if none is specified.

func ParseTimeFormat

func ParseTimeFormat(s, fmt string) (time.Time, error)

ParseTimeFormat parses s with go time fmt in the local timezone and returns a time or error.

func ParseUUID

func ParseUUID(s string) ([16]byte, error)

ParseUUID parses s and return the uuid bytes or an error. It accepts 16 hex encoded bytes with up to four dashes in between.

func Punct

func Punct(r rune) bool

Punct tests whether r is one of the ascii punctuations allowed in symbols.

func Quote

func Quote(s string, q byte) (string, error)

Quote is a json compatible string quoter that works for single and double quotes and back ticks.

func Raw

func Raw(s string) *[]byte

Raw parses s and returns a pointer to a byte slice or nil on error.

func Real

func Real(v float64) *float64

Real returns a pointer to v.

func Space

func Space(r rune) bool

Space tests whether r is a space, tab or newline.

func Span

func Span(s string) *time.Duration

Span parses s and return a pointer to a time duration or nil on error.

func Str

func Str(v string) *string

Str returns a pointer to v.

func SymPart

func SymPart(r rune) bool

SymPart tests whether r is ascii letter, digit, underscore or punctuation.

func SymStart

func SymStart(r rune) bool

SymStart tests whether r is ascii letter, underscore or punctuation.

func Time

func Time(s string) *time.Time

Time parses s and returns a pointer to a time or nil on error

func UUID

func UUID(s string) *[16]byte

UUID parses s and returns a pointer to the uuid bytes or nil on error.

func UnixMilli

func UnixMilli(v time.Time) int64

UnixMilli returns a integer timestamp since unix epoch in milliseconds.

func UnixMilliTime

func UnixMilliTime(n int64) time.Time

UnixMilliTime constructs and returns a time from the millisecond timestamp since unix epoch.

func Unquote

func Unquote(s string) (string, error)

Unquote is a stripped version of strconv.Unquote. It does not complain about single-quoted string having length greater than 1.

func WriteQuote

func WriteQuote(b writer, s string, q byte) error

Types

type Path

type Path []Seg

Path consists of non-empty segments separated by dots '.' or slashes '/'. Segments starting with a digit or minus sign are idx segments that try to select into an idxr literal, otherwise the segment represents a key used to select into a keyr literal. Segments starting with a slash signify a selection from a idxr literal.

func FillPath

func FillPath(path string, vars ...string) (res Path, err error)

FillPath reads and returns the segments for the path filled with vars or an error.

func ParsePath

func ParsePath(path string) (res Path, err error)

ParsePath reads and returns the segments for path or an error.

func (Path) CountVars

func (p Path) CountVars() (n int)

func (Path) FillVars

func (p Path) FillVars(vars []string) error

func (Path) Fst

func (p Path) Fst() Seg

func (Path) Plain

func (p Path) Plain() string

func (Path) String

func (p Path) String() string

func (Path) Suffix

func (p Path) Suffix(suf string) string

type Seg

type Seg struct {
	Key string
	Idx int
	Sel byte
}

Seg is one segment of a path. It consists of a dot or slash, followed by a key or index.

func (Seg) Empty

func (s Seg) Empty() bool

func (Seg) IsIdx

func (s Seg) IsIdx() bool

func (Seg) Sep

func (s Seg) Sep() byte

func (Seg) String

func (s Seg) String() string

Jump to

Keyboard shortcuts

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