gogb

package module
v0.0.0-...-8d9c20c Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

GOGB

Golang Utilities

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Black to white
	Black     color.RGBA = color.RGBA{0x00, 0x00, 0x00, 0xFF} // #000000
	DarkGray  color.RGBA = color.RGBA{0x26, 0x26, 0x26, 0xFF} // #262626
	Gray      color.RGBA = color.RGBA{0x80, 0x80, 0x80, 0xFF} // #808080
	LightGray color.RGBA = color.RGBA{0xD3, 0xD3, 0xD3, 0xFF} // #D3D3D3
	White     color.RGBA = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF} // #FFFFFF

	// Primary Colors
	Red  color.RGBA = color.RGBA{0xFF, 0x00, 0x00, 0xFF} // #FF0000
	Lime color.RGBA = color.RGBA{0x00, 0xFF, 0x00, 0xFF} // #00FF00
	Blue color.RGBA = color.RGBA{0x00, 0x00, 0xFF, 0xFF} // #0000FF

	// half strength primary colors
	Maroon   color.RGBA = color.RGBA{0x80, 0x00, 0x00, 0xFF} // #800000
	Green    color.RGBA = color.RGBA{0x00, 0x80, 0x00, 0xFF} // #008000
	NavyBlue color.RGBA = color.RGBA{0x00, 0x00, 0x80, 0xFF} // #000080

	// full strength primary mixes
	Yellow  color.RGBA = color.RGBA{0xFF, 0xFF, 0x00, 0xFF} // #FFFF00
	Aqua    color.RGBA = color.RGBA{0x00, 0xFF, 0xFF, 0xFF} // #00FFFF
	Cyan               = Aqua                               // #00FFFF
	Magenta color.RGBA = color.RGBA{0xFF, 0x00, 0xFF, 0xFF} // #FF00FF
	Fuchsia            = Magenta                            // #FF00FF

	// half strength primary mixes
	Olive  color.RGBA = color.RGBA{0x80, 0x80, 0x00, 0xFF} // #808000
	Purple color.RGBA = color.RGBA{0x80, 0x00, 0x80, 0xFF} // #800080
	Teal   color.RGBA = color.RGBA{0x00, 0x80, 0x80, 0xFF} // #008080

	// Other interesting colors
	Orange      color.RGBA = color.RGBA{0xFF, 0xA5, 0x00, 0xFF} // #FFA500
	Indigo      color.RGBA = color.RGBA{0x4B, 0x00, 0x82, 0xFF} // #4B0082
	Violet      color.RGBA = color.RGBA{0xEE, 0x82, 0xEE, 0xFF} // #EE82EE
	Gold        color.RGBA = color.RGBA{0xFF, 0xD7, 0x00, 0xFF} // #FFD700
	SkyBlue     color.RGBA = color.RGBA{0x87, 0xCE, 0xEB, 0xFF} // #87CEEB
	SaddleBrown color.RGBA = color.RGBA{0x8B, 0x45, 0x13, 0xFF} // #8B4513
	Tan         color.RGBA = color.RGBA{0xD2, 0xB4, 0x8C, 0xFF} // #D2B48C
	Crimson     color.RGBA = color.RGBA{0xDC, 0x14, 0x3C, 0xFF} // #DC143C
	Pink        color.RGBA = color.RGBA{0xFF, 0xC0, 0xCB, 0xFF} // #FFC0CB
	Salmon      color.RGBA = color.RGBA{0xFA, 0x80, 0x72, 0xFF} // #FA8072
	Turquoise   color.RGBA = color.RGBA{0x40, 0xE0, 0xD0, 0xFF} // #40E0D0
)

A basic set of colors based on the colors found here: https://www.w3schools.com/colors/colors_names.asp

Functions

func DateUtc

func DateUtc(year int, month time.Month, day int) time.Time

func DateUtcPtr

func DateUtcPtr(year int, month time.Month, day int) *time.Time

func FirstNotZero

func FirstNotZero[T comparable](inputs ...T) T

Generic variadic function to return the first non "zero" value.

func GetAge

func GetAge(dateOfBirth, compareDate time.Time) (int, error)

func HashPassword

func HashPassword(password string) (string, error)

Hash password using Bcrypt

func Lerp

func Lerp[T constraints.Integer | constraints.Float](a, b T, ratio float64) T

Linearly interpolate between any two numbers of the same type using a ratio. The ratio is capped between 0 and 1

func LerpColor

func LerpColor(a, b color.RGBA, ratio float64) color.RGBA

Creates a color between the given a and b. 0 means a is given, 1 means b is given, .5 is a color half way between. The ratio is capped between 0 and 1 Currently the function floors the number instead of rounding to nearest.

func NewPasetoMaker

func NewPasetoMaker(symmetricKey string) (*pasetoMaker, error)

func NewWorkerPool

func NewWorkerPool(workerCount int) *workerPool

func Time

func Time(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) time.Time

func TimePtr

func TimePtr(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *time.Time

func ToFloat32OrZero

func ToFloat32OrZero[T RealNumber](in *T) float32

func ToFloat64OrZero

func ToFloat64OrZero[T RealNumber](in *T) float64

func ToIntOrZero

func ToIntOrZero[T RealNumber](in *T) int

func ToPtr

func ToPtr[T any](in T) *T

func ToValOrZero

func ToValOrZero[T any](in *T) T

Return the underlying value or zero

func VerifyPassword

func VerifyPassword(hashedPassword, currPassword string) bool

Check if two passwords match using Bcrypt's CompareHashAndPassword

Types

type Broker

type Broker[T any] struct {
	// contains filtered or unexported fields
}

A data broker that can be published to, and any entity that has subscribed will get a copy of the message. Any subscriber should make sure to unsubscribe before deleting the reference. An independent go routine is started to receive published messages and to distribute them to all subscribers. The go routine stops once Stop() is called and all subscribers have unsubscribed. Ideally this is a 1-to-n communication system. If there are multiple publishers, it would be hard to know when to call Stop.

func NewBroker

func NewBroker[T any](bufferSize int) *Broker[T]

Creates a new Broker of any given type and start it running.

func (*Broker[T]) Publish

func (b *Broker[T]) Publish(msg T)

Send a message to every current subscriber.

func (*Broker[T]) Stop

func (b *Broker[T]) Stop()

Stop the data broker. Only call this once. All subscriptions will be closed

func (*Broker[T]) Subscribe

func (b *Broker[T]) Subscribe() chan T

Subscribe to the data broker. Messages can be received on the returned channel.

func (*Broker[T]) Unsubscribe

func (b *Broker[T]) Unsubscribe(msgCh chan T)

Unsubscribe from the data broker. The reference to the channel can be safely discarded after calling this.

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Username  string    `json:"username"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

func NewPayload

func NewPayload(username string, duration time.Duration) (*Payload, error)

type RealNumber

type RealNumber interface {
	int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

type Token

type Token string

type TokenMaker

type TokenMaker interface {
	Create(username string, duration time.Duration) (*Token, error)
	Verify(token *Token) (*Payload, error)
}

type WorkerPool

type WorkerPool interface {
	AddTask(task func())
	WaitForCompletion()
}

Based on https://medium.com/code-chasm/go-concurrency-pattern-worker-pool-a437117025b1 WorkerPool is a contract for Worker Pool implementation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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