utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2017 License: AGPL-3.0 Imports: 19 Imported by: 43

Documentation

Index

Constants

View Source
const (
	NIL = iota
	STRING
	DECIMAL
	TIME
	BOOLEAN
	ERROR
	STRING_SLICE
	DECIMAL_SLICE
	TIME_SLICE
	BOOL_SLICE
	MAP
)
View Source
const (
	DateFormat_yyyy_MM_dd DateFormat = "yyyy-MM-dd"
	DateFormat_MM_dd_yyyy DateFormat = "MM-dd-yyyy"
	DateFormat_dd_MM_yyyy DateFormat = "dd-MM-yyyy"

	TimeFormat_HH_mm       TimeFormat = "hh:mm"
	TimeFormat_hh_mm_tt    TimeFormat = "hh:mm tt"
	TimeFormat_HH_mm_ss    TimeFormat = "HH:mm:ss"
	TimeFormat_hh_mm_ss_tt TimeFormat = "hh:mm:ss tt"
)

Variables

View Source
var EmptyJSONFragment = JSONFragment{nil}

EmptyJSONFragment is a fragment which has no values

View Source
var NilLanguage = Language("")

NilLanguage represents our nil, or unknown language

View Source
var Validator = validator.New()

Validator is our system validator, it can be shared across threads

View Source
var ZeroTime = time.Time{}

ZeroTime is our uninitialized time value

Functions

func Compare

func Compare(env Environment, arg1 interface{}, arg2 interface{}) (int, error)

Compare returns the difference between the two passed interfaces which must be of the same type

func DateFromString

func DateFromString(env Environment, str string) (time.Time, error)

DateFromString returns a date constructed from the passed in string, or an error if we are unable to extract one

func DateToISO

func DateToISO(date time.Time) string

DateToISO converts the passed in time.Time to a string in ISO8601 format

func DateToString

func DateToString(env Environment, date time.Time) string

DateToString converts the passed in time element to the right format based on the environment settings

func DaysBetween

func DaysBetween(date1 time.Time, date2 time.Time) int

DaysBetween returns the number of calendar days (an int) between the two dates. Note that if these are in different timezones then the local calendar day is used for each and the difference is calculated from that.

func IsMap

func IsMap(v interface{}) bool

func IsSlice

func IsSlice(v interface{}) bool

IsSlice returns whether the passed in interface is a slice

func LookupIndex

func LookupIndex(v interface{}, idx int) (interface{}, error)

LookupIndex tries to look up the interface at the passed in index for the passed in slice

func LookupKey

func LookupKey(v interface{}, key string) (value interface{}, err error)

LookupKey tries to look up the interface at the passed in key for the passed in slice

func MapLength

func MapLength(v interface{}) (int, error)

MapLength returns the length of the passed in map, it returns an error if the argument is not a map

func MonthsBetween

func MonthsBetween(date1 time.Time, date2 time.Time) int

MonthsBetween returns the number of calendar months (an int) between the two dates. Note that if these are in different timezones then the local calendar day is used for each and the difference is calculated from that.

func ResolveVariable

func ResolveVariable(env Environment, variable interface{}, key string) interface{}

ResolveVariable will resolve the passed in string variable given in dot notation and return the value as defined by the VariableResolver passed in.

Example syntaxes:

foo.bar.0  - 0th element of bar slice within foo, could also be "0" key in bar map within foo
foo.bar[0] - same as above

func SliceLength

func SliceLength(v interface{}) (int, error)

SliceLength returns the length of the passed in slice, it returns an error if the argument is not a slice

func Snakify

func Snakify(text string) string

Snakify turns the passed in string into a context reference. We replace all whitespace characters with _ and replace any duplicate underscores

func ToBool

func ToBool(env Environment, test interface{}) (bool, error)

ToBool tests whether the passed in item should be considered True false, 0, "" and nil are false, everything else is true

func ToDate

func ToDate(env Environment, val interface{}) (time.Time, error)

ToDate tries to convert the passed in interface to a time.Time returning an error if that isn't possible

func ToDecimal

func ToDecimal(env Environment, val interface{}) (decimal.Decimal, error)

ToDecimal tries to convert the passed in interface{} to a Decimal value, returning an error if that isn't possible

func ToGoDateFormat

func ToGoDateFormat(format string) (string, error)

ToGoDateFormat converts the passed in format to a GoLang format string.

Format strings we support:

d - day of month, 1-31 dd - day of month, zero padded 0-31 fff - thousandths of a second h - hour of the day 1-12 hh - hour of the day 01-12 H - hour of the day 1-23 HH - hour of the day 01-23 K - hour and minute offset from UTC, or Z fo UTC m - minute 0-59 mm - minute 00-59 M - month 1-12 MM - month 01-12 s - second 0-59 ss - second 00-59 TT - AM or PM tt - am or pm yy - last two digits of year 0-99 yyyy - four digits of your 0000-9999 zzz - hour and minute offset from UTC ignored chars: ' ', ':', ',', 'T', 'Z', '-', '_', '/'

func ToInt

func ToInt(env Environment, val interface{}) (int, error)

ToInt tries to convert the passed in interface{} to an integer value, returning an error if that isn't possible

func ToString

func ToString(env Environment, val interface{}) (string, error)

ToString tries to turn the passed in interface to a string

func ToStringArray

func ToStringArray(env Environment, v interface{}) ([]string, error)

ToStringArray tries to turn the passed in interface (which must be an underlying slice) to a string array

func TokenizeString

func TokenizeString(str string) []string

TokenizeString returns the words in the passed in string, split by non word characters including emojis

func ValidateAll

func ValidateAll(args ...interface{}) (err error)

ValidateAll validates all the passed in arguments, failing fast on an error

func ValidateAllUnlessErr

func ValidateAllUnlessErr(err error, args ...interface{}) error

ValidateAllUnlessErr validates all the passed in arguments, failing fast on any error including the passed in one

Types

type DateFormat

type DateFormat string

func (DateFormat) String

func (df DateFormat) String() string

type Environment

type Environment interface {
	DateFormat() DateFormat
	SetDateFormat(DateFormat)

	TimeFormat() TimeFormat
	SetTimeFormat(TimeFormat)

	Timezone() *time.Location
	SetTimezone(*time.Location)
}

Environment defines the Environment that the Excellent function is running in, this includes the timezone the user is in as well as the preferred date and time formats.

func NewDefaultEnvironment

func NewDefaultEnvironment() Environment

NewDefaultEnvironment creates a new Environment with our usual defaults in the UTC timezone

func NewEnvironment

func NewEnvironment(dateFormat DateFormat, timeFormat TimeFormat, timezone *time.Location) Environment

NewEnvironment creates a new Environment with the passed in date and time formats and timezone

type JSONFragment

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

JSONFragment is a thin wrapper around a byte array that takes care of allow key lookups into the json in that byte array

func NewJSONFragment

func NewJSONFragment(json []byte) JSONFragment

NewJSONFragment creates a new json fragment for the passed in byte array

func ToJSON

func ToJSON(env Environment, val interface{}) (JSONFragment, error)

ToJSON tries to turn the passed in interface to a JSON fragment

func (JSONFragment) Default

func (j JSONFragment) Default() interface{}

Default returns the default value for this JSON, which is the JSON itself

func (*JSONFragment) MarshalJSON

func (j *JSONFragment) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON representation of our fragment, which is just our internal byte array

func (JSONFragment) Resolve

func (j JSONFragment) Resolve(key string) interface{}

Resolve resolves the passed in key, which is expected to be either an integer in the case that our JSON is an array or a key name if it is a map

func (JSONFragment) String

func (j JSONFragment) String() string

String returns the string representation of this JSON, which is just the JSON itself

func (*JSONFragment) UnmarshalJSON

func (j *JSONFragment) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a new JSONFragment from the passed in byte stream. We validate it looks like valid JSON then set our internal byte structure

type Language

type Language string

Language is our internal representation of a language

func ParseLanguage

func ParseLanguage(lang string) (Language, error)

ParseLanguage returns a new Language for the passed in language string, or an error if not found

type RequestResponse

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

RequestResponse represents both the outgoing request and response for a particular URL/method/body

func MakeHTTPRequest

func MakeHTTPRequest(req *http.Request) (*RequestResponse, error)

MakeHTTPRequest fires the passed in http request, returning any errors encountered. RequestResponse is always set regardless of any errors being set

func (*RequestResponse) Body

func (r *RequestResponse) Body() string

func (*RequestResponse) Default

func (r *RequestResponse) Default() interface{}

func (*RequestResponse) JSON

func (r *RequestResponse) JSON() JSONFragment

func (*RequestResponse) MarshalJSON

func (r *RequestResponse) MarshalJSON() ([]byte, error)

func (*RequestResponse) Request

func (r *RequestResponse) Request() string

func (*RequestResponse) Resolve

func (r *RequestResponse) Resolve(key string) interface{}

func (*RequestResponse) Response

func (r *RequestResponse) Response() string

func (*RequestResponse) Status

func (*RequestResponse) StatusCode

func (r *RequestResponse) StatusCode() int

func (*RequestResponse) String

func (r *RequestResponse) String() string

func (*RequestResponse) URL

func (r *RequestResponse) URL() string

func (*RequestResponse) UnmarshalJSON

func (r *RequestResponse) UnmarshalJSON(data []byte) error

type RequestResponseStatus

type RequestResponseStatus string

RequestResponseStatus represents the status of a WebhookRequeset

const (
	// RRSuccess represents that the webhook was successful
	RRSuccess RequestResponseStatus = "S"

	// RRConnectionFailure represents that the webhook had a connection failure
	RRConnectionFailure RequestResponseStatus = "F"

	// RRStatusFailure represents that the webhook had a non 2xx status code
	RRStatusFailure RequestResponseStatus = "E"
)

func (RequestResponseStatus) String

func (r RequestResponseStatus) String() string

type TimeFormat

type TimeFormat string

func (TimeFormat) String

func (tf TimeFormat) String() string

type Typed

type Typed interface {
	Type() string
}

type TypedEnvelope

type TypedEnvelope struct {
	Type string `json:"type"`
	Data []byte `json:"-"`
}

TypedEnvelope represents a json blob with a type property

func EnvelopeFromTyped

func EnvelopeFromTyped(typed Typed) (*TypedEnvelope, error)

func (*TypedEnvelope) MarshalJSON

func (e *TypedEnvelope) MarshalJSON() ([]byte, error)

func (*TypedEnvelope) UnmarshalJSON

func (e *TypedEnvelope) UnmarshalJSON(b []byte) (err error)

type ValidationError

type ValidationError []error

ValidationError is our error type for validation errors

func NewValidationError

func NewValidationError(err string) ValidationError

NewValidationError creates a new ValidationError with the single passed in error messages

func (ValidationError) Error

func (e ValidationError) Error() string

Error returns a string representation of these validation errors

type VariableResolver

type VariableResolver interface {
	Resolve(key string) interface{}
	Default() interface{}
	String() string
}

VariableResolver defines the interface used by Excellent objects that can be indexed into

func NewMapResolver

func NewMapResolver(values map[string]interface{}) VariableResolver

NewMapResolver returns a simple resolver that resolves variables according to the values passed in

type XType

type XType int

XType is an an enumeration of the possible types we can deal with

func ToXAtom

func ToXAtom(env Environment, val interface{}) (interface{}, XType, error)

ToXAtom figures out the raw type of the passed in interface, returning that type

Jump to

Keyboard shortcuts

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