bricks

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: GPL-3.0 Imports: 11 Imported by: 2

README

Bricks Coverage Badge

Includes international data types and primitive data structures known for humans and required by developers.

Usage

go get github.com/janstoon/toolbox/bricks

Utils

Resolvers, Parsers

Types

Banking

International Bank Account Number
Primary Account Number
Bank Account
Money

Geography

Country

Internet

Email

Society

National Identity Number

Telecom

Phone Number
Network Operator

Time

Timespan
Jalali

Technical

Includes Abstract Data Types

Trie
Bag
Filter
Compensator

Wire

Portable data

Error
Number

Documentation

Index

Constants

View Source
const (
	ErrCodeCanceled = iota + 1
	ErrCodeUnknown
	ErrCodeInvalidArgument
	ErrCodeDeadlineExceeded
	ErrCodeNotFound
	ErrCodeAlreadyExists
	ErrCodePermissionDenied
	ErrCodeResourceExhausted
	ErrCodeFailedPrecondition
	ErrCodeAborted
	ErrCodeOutOfRange
	ErrCodeUnimplemented
	ErrCodeInternal
	ErrCodeUnavailable
	ErrCodeDataLoss
	ErrCodeUnauthenticated
)

Variables

View Source
var (
	ErrIbanIncorrectLength = errors.New("iban length incorrect")
	ErrIbanUnknownCountry  = errors.New("iban country unknown")
	ErrIbanCheckFailure    = errors.New("iban check failure")
	ErrIbanInvalidBban     = errors.New("iban invalid bban")

	ErrPanIncorrectLength = errors.New("pan length incorrect")
)
View Source
var (
	ErrPhoneNumberUnknownCountry = errors.New("phone number country unknown")
	ErrUnknownNetworkOperator    = errors.New("network operator unknown")
)
View Source
var (
	ErrRetryable = errors.New("temporary issue")

	ErrUnknown  = ErrorWithCode(ErrCodeUnknown, errors.New("unknown error"))
	ErrCanceled = errors.Join(ErrRetryable, ErrorWithCode(ErrCodeCanceled, errors.New("operation was canceled")))

	// ErrDeadlineExceeded indicates timeout has been reached regardless of operation result.
	//
	// May result in these http status codes:
	//   * 408
	//   * 504
	ErrDeadlineExceeded = errors.Join(ErrRetryable,
		ErrorWithCode(ErrCodeDeadlineExceeded, errors.New("operation expired before completion")))

	// ErrResourceExhausted indicates some resource has been exhausted,
	// perhaps a per-user quota (like rate limiting), or perhaps the entire file system is out of space.
	// Situations like out-of-memory and server overload, or when a message is larger than the configured maximum size.
	//
	// May result in these http status codes:
	//   * 429
	//   * 507
	ErrResourceExhausted = errors.Join(ErrRetryable,
		ErrorWithCode(ErrCodeResourceExhausted, errors.New("resource has been exhausted")))

	ErrCustomerSide    = errors.New("customer-side error")
	ErrInvalidArgument = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeInvalidArgument, errors.New("invalid argument")))
	ErrUnauthenticated = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeUnauthenticated, errors.New("caller not identified (unauthenticated)")))
	ErrPermissionDenied = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodePermissionDenied, errors.New("caller identified but permission denied")))
	ErrNotFound = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeNotFound, errors.New("requested entity was not found")))
	ErrAlreadyExists = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeAlreadyExists, errors.New("entity already exists")))
	ErrFailedPrecondition = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeFailedPrecondition, errors.New("failed precondition")))
	ErrOutOfRange = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeOutOfRange, errors.New("operation was attempted past the valid range")))
	ErrAborted  = errors.Join(ErrCustomerSide, ErrorWithCode(ErrCodeAborted, errors.New("operation was aborted")))
	ErrDataLoss = errors.Join(ErrCustomerSide,
		ErrorWithCode(ErrCodeDataLoss, errors.New("unrecoverable data loss or corruption")))

	ErrSupplierSide = errors.New("supplier-side error")
	ErrInternal     = errors.Join(ErrRetryable, ErrSupplierSide,
		ErrorWithCode(ErrCodeInternal, errors.New("some invariants expected by underlying system has been broken")))
	ErrUnimplemented = errors.Join(ErrSupplierSide,
		ErrorWithCode(ErrCodeUnimplemented,
			errors.New("operation is not implemented or not supported/enabled in this service")))
	ErrUnavailable = errors.Join(ErrRetryable, ErrSupplierSide,
		ErrorWithCode(ErrCodeUnavailable, errors.New("service is currently unavailable")))
)
View Source
var EmptyNationalIdentityNumber = NationalIdentityNumber{}
View Source
var EmptyPhoneNumber = PhoneNumber{}

Functions

func CompensatorAsError added in v0.4.0

func CompensatorAsError(compensator Compensator, err error) error

func ErrorWithCode added in v0.7.1

func ErrorWithCode(code int, err error) error

func ParseError added in v0.7.0

func ParseError(err, unknown error) error

func RegisterBbanValidator

func RegisterBbanValidator(countryCode string, validator BbanValidator)

func RegisterCountry

func RegisterCountry(c Country)

func RegisterNetworkOperators

func RegisterNetworkOperators(countryIsoAlphaTwoCode string, nn ...NetworkOperator)

func RegisterPhoneNumberResolver

func RegisterPhoneNumberResolver(countryTelCode string, resolver PhoneNumberResolver)

Types

type Bag

type Bag[Value any] interface {
	// Pull takes out an item in case of existence. It should return ErrReachedEnd when nothing remained to return.
	Pull(ctx context.Context) (*Value, error)
}

Bag is a container which items are non-returnable once pulled. Implementation of Pull must be thread-safe.

type BankAccount

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

func NewBankAccount

func NewBankAccount(name string) BankAccount

func (BankAccount) BankName

func (ba BankAccount) BankName() string

type BbanValidator

type BbanValidator func(bban string) error

type ClosedInterval

type ClosedInterval[T cmp.Ordered] [2]T // [min, max]

func (ClosedInterval[T]) Contains

func (tt ClosedInterval[T]) Contains(e T) bool

type Coded added in v0.7.1

type Coded interface {
	Code() int
}

type Collection

type Collection[T any] interface {
	Contains(e T) bool
}

Collection is the set of objects

type Compensator added in v0.4.0

type Compensator interface {
	Compensate(ctx context.Context, err error) error
}

type CompensatorFunc added in v0.4.0

type CompensatorFunc func(ctx context.Context, err error) error

func (CompensatorFunc) Compensate added in v0.4.0

func (cf CompensatorFunc) Compensate(ctx context.Context, err error) error

type Country

type Country struct {
	EnglishName CountryName
	Codes       CountryCode
}

Country useful links: http://download.geonames.org/export/dump/countryInfo.txt, https://github.com/mledoze/countries/blob/master/countries.json

func LookupCountryByIsoAlphaTwoCode

func LookupCountryByIsoAlphaTwoCode(code string) *Country

func LookupCountryByTelephoneCode

func LookupCountryByTelephoneCode(code string) *Country

type CountryCode

type CountryCode struct {
	// IsoAlphaTwo contains ISO 3166-1 alpha-2 code
	// useful links: https://en.wikipedia.org/wiki/ISO_3166-1 and https://www.iban.com/country-codes
	IsoAlphaTwo string

	// IsoAlphaThree contains ISO 3166-1 alpha-3 code
	// useful links: https://en.wikipedia.org/wiki/ISO_3166-1 and https://www.iban.com/country-codes
	IsoAlphaThree string

	// IsoNumeric contains ISO 3166-1 numeric code
	// useful links: https://en.wikipedia.org/wiki/ISO_3166-1 and https://www.iban.com/country-codes
	IsoNumeric string

	// IocAlphaThree contains international olympic committee alphabetic country code
	// useful links: https://en.wikipedia.org/wiki/List_of_IOC_country_codes
	IocAlphaThree string

	// Telephone is the calling code
	// useful links: https://en.wikipedia.org/wiki/List_of_country_calling_codes
	Telephone string
}

CountryCode contains country unique codes in different standards

type CountryName

type CountryName struct {
	Short string
	Full  string
}

type Email

type Email struct{}

type InternationalBankAccountNumber

type InternationalBankAccountNumber struct {
	Country *Country
	// contains filtered or unexported fields
}

func ParseInternationalBankAccountNumber

func ParseInternationalBankAccountNumber(iban string) (*InternationalBankAccountNumber, error)

func (InternationalBankAccountNumber) String

func (iban InternationalBankAccountNumber) String() string

type LeftOpenInterval

type LeftOpenInterval[T cmp.Ordered] [2]T // (min, max]

func (LeftOpenInterval[T]) Contains

func (tt LeftOpenInterval[T]) Contains(e T) bool

type MessageEnvelope added in v0.2.8

type MessageEnvelope struct {
	Id      string
	Retried uint
	Note    []byte
}

type Money

type Money any

type NationalIdentityNumber

type NationalIdentityNumber struct{}

func ParseNationalIdentityNumber

func ParseNationalIdentityNumber(number string) (*NationalIdentityNumber, error)

type NetworkOperator

type NetworkOperator struct {
	Name    string
	Virtual bool
}

func NetworkOperatorsByCountryCode

func NetworkOperatorsByCountryCode(code string) []NetworkOperator

NetworkOperatorsByCountryCode Lists registered NetworkOperator(s) of a country where code is iso alpha-2 code

type OpenInterval

type OpenInterval[T cmp.Ordered] [2]T // (min, max)

func (OpenInterval[T]) Contains

func (tt OpenInterval[T]) Contains(e T) bool

type PhoneNumber

type PhoneNumber struct {
	Country         *Country
	Mobile          bool
	Prepaid         bool
	DefaultOperator NetworkOperator
	// contains filtered or unexported fields
}

func MustParsePhoneNumber

func MustParsePhoneNumber(number string) PhoneNumber

func ParsePhoneNumber

func ParsePhoneNumber(number string) (*PhoneNumber, error)

func (PhoneNumber) String

func (pn PhoneNumber) String() string

type PhoneNumberMetadata

type PhoneNumberMetadata struct {
	Mobile   bool
	Prepaid  bool
	Operator NetworkOperator
}

type PhoneNumberResolver

type PhoneNumberResolver func(localNumber string) (*PhoneNumberMetadata, error)

type PrimaryAccountNumber

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

func ParsePrimaryAccountNumber

func ParsePrimaryAccountNumber(pan string) (*PrimaryAccountNumber, error)

func (PrimaryAccountNumber) String

func (pan PrimaryAccountNumber) String() string

type RightOpenInterval

type RightOpenInterval[T cmp.Ordered] [2]T // [min, max)

func (RightOpenInterval[T]) Contains

func (tt RightOpenInterval[T]) Contains(e T) bool

type Timespan

type Timespan struct {
	Start time.Time
	End   time.Time
}

func (Timespan) Contains

func (ts Timespan) Contains(e time.Time) bool

type TrieNode

type TrieNode[Key, KeyAtom comparable, Value any] struct {
	// contains filtered or unexported fields
}

func Trie

func Trie[Key, KeyAtom comparable, Value any](atomizer func(Key) []KeyAtom) *TrieNode[Key, KeyAtom, Value]

func (*TrieNode[Key, _, Value]) BestMatch

func (t *TrieNode[Key, _, Value]) BestMatch(key Key) *Value

func (*TrieNode[Key, _, _]) Delete

func (t *TrieNode[Key, _, _]) Delete(key Key)

func (*TrieNode[Key, _, Value]) Get

func (t *TrieNode[Key, _, Value]) Get(key Key) *Value

func (*TrieNode[Key, KeyAtom, Value]) Put

func (t *TrieNode[Key, KeyAtom, Value]) Put(key Key, val Value)

Directories

Path Synopsis
countries

Jump to

Keyboard shortcuts

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