common

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: Apache-2.0 Imports: 14 Imported by: 5

Documentation

Index

Constants

View Source
const (
	AcceptLanguage     = "Accept-Language"
	Authorization      = "Authorization"
	ContentType        = "Content-Type"
	UserAgent          = "User-Agent"
	APIKey             = "ApiKey"
	BearerPrefix       = "Bearer "
	ApplicationJSON    = "application/json"
	TextXML            = "text/xml"
	RequestIDHeader    = "X-Request-ID"
	ClientCodeParam    = "clientCode"
	MaxRequestIDLength = 64
	AESKeyLength       = 32
	BINLength          = 6
)

common constants

View Source
const (
	DateFormat      = "02 Jan 2006"
	DayOfWeekFormat = "Mon"
	DateTimeFormat  = "02 Jan 2006, 03:04 PM MST"
	YearFormat      = "2006"
)

date time string formats

Variables

This section is empty.

Functions

func BoolRef

func BoolRef(v bool) *bool

BoolRef returns a reference to a bool value

func CurrentLocalTime

func CurrentLocalTime() time.Time

CurrentLocalTime returns current date time in local timezone

func CurrentUTCTime

func CurrentUTCTime() time.Time

CurrentUTCTime returns current date time in UTC

func CurrentUTCTimeRef

func CurrentUTCTimeRef() *time.Time

CurrentUTCTimeRef returns current date time reference in UTC

func CurrentUnixTimestamp

func CurrentUnixTimestamp() int64

CurrentUnixTimestamp returns current Unix timestamp (seconds)

func Decrypt

func Decrypt(ciphertext string, key string) (plaintext string, err error)

Decrypt decrypts data using 256-bit AES-GCM, key must have length 32 or more

func Encrypt

func Encrypt(plaintext string, key string) (ciphertext string, err error)

Encrypt encrypts data using 256-bit AES-GCM, key must have length 32 or more

func GetBasic added in v0.1.3

func GetBasic(ctx context.Context, key string) (value interface{})

GetBasic gets basic from the context with key

func GetExtra added in v0.1.6

func GetExtra(ctx context.Context, key string) (value interface{})

GetExtra gets Extra from the context with key

func GetStatsD

func GetStatsD(ctx context.Context) (statsD *statsd.Client)

GetStatsD gets statsD client from the context if any

func GetString

func GetString(ctx context.Context, key ContextKey) (value string)

GetString gets string from a context by ContextKey

func IDToRef

func IDToRef(id uint, prefix string, length int, refChars, refPaddingChars string) (string, error)

IDToRef convert defined ID to reference

func Int32Ref

func Int32Ref(v int32) *int32

Int32Ref returns a reference to a int32 value

func Int64Ref

func Int64Ref(v int64) *int64

Int64Ref returns a reference to a int64 value

func RefToID

func RefToID(ref, prefix string, refChars, refPaddingChars string) (uint64, error)

RefToID convert defined reference to id

func SetBasic added in v0.1.4

func SetBasic(parent context.Context, key string, value interface{}) context.Context

SetBasic returns a copy of parent context with basic key value added into it

func SetBasics added in v0.1.3

func SetBasics(parent context.Context, basics Basics) context.Context

SetBasics returns a copy of parent context with basics added into it

func SetExtra added in v0.1.6

func SetExtra(parent context.Context, key string, value interface{}) context.Context

SetExtra returns a copy of parent context with Extra key value added into it

func SetExtras

func SetExtras(parent context.Context, extras Extras) context.Context

SetExtras returns a copy of parent context with extras added into it

func SetStatsD

func SetStatsD(parent context.Context, statsD *statsd.Client) context.Context

SetStatsD returns a copy of parent context with statsD client added into it

func StrRef

func StrRef(v string) *string

StrRef returns a reference to a string value

func String2Uint added in v0.1.2

func String2Uint(s string, chars, paddingChars string) (n uint64, err error)

String2Uint is the revert of Uint2String

It returns negative result in 2 cases:
- s contains character not exist in chars
- the result is bigger than max int64 (integer overflow happen)

func TimeChanged

func TimeChanged(new *time.Time, base *time.Time) (bool, *time.Time)

TimeChanged check if new time changed from the base, return the changed flag and time value

func TimeRef

func TimeRef(v time.Time) *time.Time

TimeRef return a reference to time value

func Uint2String added in v0.1.2

func Uint2String(number uint64, chars, paddingChars string, length int) (s string, err error)

Uint2String converts a number to a string using characters from chars.

Validation:

  • `length` must > 0
  • length of `chars` must >= 10
  • `paddingChars` must not be empty
  • `chars` & `paddingChars` must not overlap

If length of the result < required length, the result will be left padded with random characters from `paddingChars`.

func Uint32Ref

func Uint32Ref(v uint32) *uint32

Uint32Ref returns a reference to a uint32 value

func UintRef

func UintRef(v uint) *uint

UintRef returns a reference to a uint value

func ValidateCardNumber

func ValidateCardNumber(number string) bool

ValidateCardNumber will check the credit card's number against the Luhn algorithm

Types

type Basics added in v0.1.3

type Basics map[string]interface{}

Basics are basics information to be added into context

func GetBasics added in v0.1.3

func GetBasics(ctx context.Context) (value Basics)

GetBasics gets basics from the context if any

type ContextKey

type ContextKey string

ContextKey is key to be used to store values to context

type DMutex

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

DMutex a simple distributed mutex for lock with keys

func (*DMutex) Lock

func (dm *DMutex) Lock(key string) (UnLocker, error)

Lock the specific key

type Date

type Date time.Time

Date : custom date in format yyyy-MM-dd

func (Date) Before

func (d Date) Before(other Date) bool

Before checks if the date is before the other date

func (Date) Equal

func (d Date) Equal(other time.Time) bool

Equal check if date is present the same time instant with other

func (Date) IsZero

func (d Date) IsZero() bool

IsZero check if date is present zero time instance

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON marshall Date into JSON

func (Date) String

func (d Date) String() string

String returns the time in the custom format

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON Parses the json string in the Date

type DistributedLocker

type DistributedLocker interface {
	// Lock the specific key
	Lock(key string) (UnLocker, error)
}

DistributedLocker a distributed lock type

type Extras

type Extras map[string]interface{}

Extras are extra information to be added into context

func GetExtras

func GetExtras(ctx context.Context) (value Extras)

GetExtras gets extras from the context if any

type UnLocker

type UnLocker func()

UnLocker unlock function

Jump to

Keyboard shortcuts

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