uniform

package module
v0.0.0-...-49370fd Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2023 License: MIT Imports: 15 Imported by: 1

README

Uniform

Dependencies

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/uniform-nats.key -out /etc/ssl/certs/uniform-nats.crt
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/uniform-amqps.key -out /etc/ssl/certs/uniform-amqps.crt
sudo chmod +r /etc/ssl/private/uniform-nats.key
sudo chmod +r /etc/ssl/private/uniform-amqps.key

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCantReply = errors.New("uniform: no reply channel available")
	ErrTimeout   = errors.New("uniform: timeout")
)

A package level reusable error for chain timeouts

View Source
var Contains = func(haystack []string, needle string, caseSensitive bool) bool {
	return IndexOf(haystack, needle, caseSensitive) != -1
}

Contains checks if a string array contains a given string

View Source
var Filter = func(items, filters []string, caseSensitive bool) []string {
	if filters == nil || len(filters) <= 0 {
		return items
	}

	filteredItems := make([]string, 0)
	for _, item := range items {
		if Contains(filters, item, caseSensitive) {
			continue
		}
		filteredItems = append(filteredItems, item)
	}

	return filteredItems
}

Filter trims the filterItems from the items array

View Source
var Hash = func(value interface{}, salt string) string {
	concatenatedData := []byte(fmt.Sprintf(`%s%v`, salt, value))

	encoder := sha512.New()
	_, err := encoder.Write(concatenatedData)
	if err != nil {
		panic(err)
	}
	hashedData := encoder.Sum(nil)

	base64EncodedString := base64.StdEncoding.EncodeToString(hashedData)
	return base64EncodedString
}

Hash generates a sha512 hash for the given value/salt combo

View Source
var IndexOf = func(haystack []string, needle string, caseSensitive bool) int {
	if caseSensitive {
		for i, item := range haystack {
			if item == needle {
				return i
			}
		}
	} else {
		lowNeedle := strings.ToLower(needle)
		for i, item := range haystack {
			if strings.ToLower(item) == lowNeedle {
				return i
			}
		}
	}
	return -1
}

IndexOf Gets the index of a string in an array of strings

View Source
var IsEmpty = func(value interface{}) bool {
	if value == nil || value == "" {
		return true
	}

	stringValue := strings.TrimSpace(fmt.Sprint(value))
	if stringValue == "" || stringValue == "nil" || stringValue == "<nil>" {
		return true
	}

	if strings.HasPrefix(stringValue, "0001-01-01") {
		return true
	}

	return isEmpty(value)
}

IsEmpty determines if a value is empty or not

View Source
var ParseRequest = func(data []byte) (Request, error) {
	var request Request

	if err := bson.Unmarshal(data, &request); err != nil {
		return request, err
	}

	switch value := request.Model.(type) {
	case primitive.Binary:
		request.Model = value.Data
		break
	}

	return request, nil
}

ParseRequest Gets the index of a string in an array of strings

View Source
var ValidateDate = func(american bool, value interface{}) (bool, []string, time.Time) {
	panic("not yet implemented")
}
View Source
var ValidateDateTime = func(american bool, value interface{}) (bool, []string, time.Time) {
	panic("not yet implemented")
}
View Source
var ValidateEmail = func(value interface{}) (bool, []string, interface{}) {
	panic("not yet implemented")
}
View Source
var ValidateIdentityNumber = func(country string, value interface{}) (bool, []string, interface{}) {
	panic("not yet implemented")
}
View Source
var ValidateMaximumFloat = func(maximum float64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateMaximumInt = func(maximum int64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateMinimumFloat = func(minimum float64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateMinimumInt = func(minimum int64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateMobile = func(country string, value interface{}) (bool, []string, interface{}) {
	panic("not yet implemented")
}
View Source
var ValidatePassportNumber = func(country string, value interface{}) (bool, []string, interface{}) {
	panic("not yet implemented")
}
View Source
var ValidateRangeFloat = func(minimum, maximum float64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateRangeInt = func(minimum, maximum int64, value interface{}) (bool, []string) {
	panic("not yet implemented")
}
View Source
var ValidateRequired = func(value interface{}) (bool, []string) {
	if IsEmpty(value) {
		return false, []string{"May not be empty"}
	}
	return true, nil
}

Functions

func Alert

func Alert(code int, message string)

Trigger a panic in a specific format that will tell the api layer to respond with a specific error code

func MapKeysToSlice

func MapKeysToSlice[K comparable, V any](m map[K]V) []K

MapKeysToSlice extract the keys of map as an array

func RequestValidator

func RequestValidator(request M, fields ...string) (IValidator, M)

Types

type DateTime

type DateTime struct {
	UtcTime time.Time

	Timezone           string
	TimezoneAdjustment time.Duration
	TimezoneTime       time.Time

	DaylightSavings           bool
	DaylightSavingsStart      time.Time
	DaylightSavingsEnd        time.Time
	DaylightSavingsAdjustment time.Duration
	LocalTime                 time.Time
}

type EmailAttachment

type EmailAttachment struct {
	ContentType string `json:"content-type"`
	Filename    string `json:"filename"`
	Data        string `json:"data"`
}

type IConn

type IConn interface {
	Request(page diary.IPage, subj string, timeout time.Duration, request Request, scope S) error
	Publish(page diary.IPage, subj string, request Request) error

	ChainRequest(page diary.IPage, subj string, original IRequest, request Request, scope S) error
	ChainPublish(page diary.IPage, subj string, original IRequest, request Request) error

	Subscribe(rate time.Duration, subj string, scope S) (ISubscription, error)
	QueueSubscribe(rate time.Duration, subj, queue string, scope S) (ISubscription, error)

	GeneratePdf(p diary.IPage, timeout time.Duration, serviceId string, html []byte) []byte
	SendEmail(p diary.IPage, timeout time.Duration, serviceId, from, fromName, subject, body string, to ...string)
	SendEmailX(p diary.IPage, timeout time.Duration, serviceId, from, fromName, subject, body string, attachments []EmailAttachment, to ...string)
	SendSms(p diary.IPage, timeout time.Duration, serviceId, body string, to ...string)
	SendEmailTemplate(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId, from, fromName, path string, vars M, to ...string)
	SendEmailTemplateX(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId, from, fromName, path string, vars M, attachments []EmailAttachment, to ...string)
	SendSmsTemplate(p diary.IPage, timeout time.Duration, asset func(string) []byte, serviceId, path string, vars M, to ...string)

	// Populates model with the raw underlying connector which may be required by more advanced users
	Raw(model interface{})

	Drain() error
	Close()
}

A definition of the public functions for a connection interface

func ConnectorAwsEventBridge

func ConnectorAwsEventBridge(d diary.IDiary, c interface{}) (IConn, error)

func ConnectorAzureEventGrid

func ConnectorAzureEventGrid(d diary.IDiary, c interface{}) (IConn, error)

func ConnectorGoogleCloudPubSub

func ConnectorGoogleCloudPubSub(d diary.IDiary, c interface{}) (IConn, error)

func ConnectorNats

func ConnectorNats(d diary.IDiary, c *nats.Conn) (IConn, error)

type IRequest

type IRequest interface {
	Conn() IConn

	Read(interface{})
	Parameters() P
	Context() M

	CanReply() bool
	Reply(Request) error
	ReplyContinue(Request, S) error
	Raw() Request
	Bytes() []byte
	Channel() string

	Timeout() time.Duration
	StartedAt() time.Time
	Remainder() time.Duration

	HasError() bool
	Error() string
}

A definition of the public functions for a request interface

type ISubscription

type ISubscription interface {
	Unsubscribe() error
}

A definition of the public functions for a subscription interface

type IValidator

type IValidator interface {
	Error(field string, errors ...string)
	Validate()
	Check() Q

	Required(document M, fields ...string) M
	MinimumInt(document M, minimum int64, fields ...string) M
	MaximumInt(document M, maximum int64, fields ...string) M
	MinimumFloat(document M, minimum float64, fields ...string) M
	MaximumFloat(document M, maximum float64, fields ...string) M
	RangeInt(document M, minimum, maximum int64, fields ...string) M
	RangeFloat(document M, minimum, maximum float64, fields ...string) M
	Mobile(country string, document M, minimum, maximum float64, fields ...string) M
	Email(country string, document M, minimum, maximum float64, fields ...string) M
	PassportNumber(country string, document M, minimum, maximum float64, fields ...string) M
	IdentityNumber(country string, document M, minimum, maximum float64, fields ...string) M
	Date(country string, document M, minimum, maximum float64, fields ...string) M
	DateTime(country string, document M, minimum, maximum float64, fields ...string) M
}

A definition of the public functions for a request interface

func NewValidator

func NewValidator() IValidator

type M

type M map[string]interface{}

A package shorthand for a map[string]interface

type Money

type Money struct {
	CurrencyCode   string
	CurrencySymbol string
	DateTime       DateTime

	WholeNumber int64
	Precision   byte
	Value       float64

	Display string
}

type P

type P map[string]string

A package shorthand for a map[string]string

type Protected

type Protected struct {
	Kind      reflect.Kind
	Hash      string
	Encrypted []byte
}

func NewProtectedValue

func NewProtectedValue(value interface{}) Protected

func (*Protected) CompareHash

func (p *Protected) CompareHash(value string) bool

func (*Protected) Decrypt

func (p *Protected) Decrypt() interface{}

type Q

type Q map[string][]string

A package shorthand for map[string][]string

type Real

type Real struct {
	WholeNumber int64
	Precision   byte
	Value       float64
}

type Request

type Request struct {
	Model      interface{}
	Parameters P
	Context    M
	Error      string
}

type S

type S func(r IRequest, p diary.IPage)

A package shorthand for func(r IRequest, p diary.IPage)

Directories

Path Synopsis
common
sms

Jump to

Keyboard shortcuts

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