util

package
v0.0.0-...-d90c12c Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 32 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// RedactReplacement is the default replacement string
	RedactReplacement = "***"

	// RedactHook is the hook for expanding different representations of
	// redaction items. Setting to nil will disable redaction.
	RedactHook = RedactDefaultHook
)
View Source
var CtxLogger = struct{}{}
View Source
var ErrStateExpired = fmt.Errorf("state expired")
View Source
var LogAreaPadding = 6

LogAreaPadding of log areas

View Source
var (

	// OutThreshold is the default console log level
	OutThreshold = jww.LevelInfo
)

Functions

func CaptureLogs

func CaptureLogs(c chan<- Param)

CaptureLogs appends uiWriter to relevant log levels for loggers created before uiChan is initialized

func DecodeOther

func DecodeOther(other, cc interface{}) error

DecodeOther uses mapstructure to decode into target structure. Unused keys cause errors.

func DefaultPort

func DefaultPort(conn string, port int) string

DefaultPort appends given port to connection if not specified

func DefaultScheme

func DefaultScheme(uri, scheme string) string

DefaultScheme prepends given scheme to uri if not specified

func Flusher

func Flusher() flush

Flusher returns a new flush channel

func FormatValue

func FormatValue(format string, val interface{}) string

FormatValue will apply specific formatting in addition to standard sprintf

func GetNextOccurrence

func GetNextOccurrence(weekdays []int, timeStr string, tz string) (time.Time, error)

GetNextOccurrence returns the next occurrence of the given time on the specified weekdays.

func Getenv

func Getenv(key string, def ...string) string

func LocalIPs

func LocalIPs() (ips []net.IPNet)

LocalIPs returns a slice of local IPv4 addresses

func LogLevel

func LogLevel(defaultLevel string, areaLevels map[string]string)

LogLevel sets log level for all loggers

func Loggers

func Loggers(cb func(string, *Logger))

Loggers invokes callback for each configured logger

func NewConfigError

func NewConfigError(err error) error

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

func RedactDefaultHook

func RedactDefaultHook(s string) []string

RedactDefaultHook expands a redaction item to include URL encoding

func ReplaceFormatted

func ReplaceFormatted(s string, kv map[string]interface{}) (string, error)

ReplaceFormatted replaces all occurrences of ${key} with formatted val from the kv map

func SignFromPower

func SignFromPower(current, power float64) float64

SignFromPower is a helper function to create signed current from signed power bypassing already signed current

func TokenWithExpiry

func TokenWithExpiry(token *oauth2.Token) *oauth2.Token

func WithLogger

func WithLogger(ctx context.Context, log *Logger) context.Context

Types

type AuthCollection

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

func NewAuthCollection

func NewAuthCollection(paramC chan<- Param) *AuthCollection

func (*AuthCollection) Publish

func (ac *AuthCollection) Publish()

publish routes and status

func (*AuthCollection) Register

func (ac *AuthCollection) Register(baseURI, title string) *AuthProvider

type AuthProvider

type AuthProvider struct {
	Uri           string `json:"uri"`
	Authenticated bool   `json:"authenticated"`
	// contains filtered or unexported fields
}

func (*AuthProvider) Handler

func (ap *AuthProvider) Handler() chan<- bool

type Cache

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

Cache is a data store

func NewCache

func NewCache() *Cache

NewCache creates cache

func (*Cache) Add

func (c *Cache) Add(key string, param Param)

Add entry to cache

func (*Cache) All

func (c *Cache) All() []Param

All provides a copy of the cached values

func (*Cache) Get

func (c *Cache) Get(key string) Param

Get entry from cache

func (*Cache) Run

func (c *Cache) Run(in <-chan Param)

Run adds input channel's values to cache

func (*Cache) State

func (c *Cache) State(enc encode.Encoder) map[string]any

State provides a structured copy of the cached values. Loadpoints are aggregated as loadpoints array. Result values are formatted using encoder.

type ConfigError

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

ConfigError wraps yaml configuration errors from mapstructure

func (*ConfigError) Error

func (e *ConfigError) Error() string

func (*ConfigError) Unwrap

func (e *ConfigError) Unwrap() error

type Logger

type Logger struct {
	*jww.Notepad
	*Redactor
	// contains filtered or unexported fields
}

Logger wraps a jww notepad to avoid leaking implementation detail

func NewLogger

func NewLogger(area string) *Logger

NewLogger creates a logger with the given log area and adds it to the registry

func NewLoggerWithLoadpoint

func NewLoggerWithLoadpoint(area string, lp int) *Logger

NewLoggerWithLoadpoint creates a logger with reference to at loadpoint

func (*Logger) Redact

func (l *Logger) Redact(items ...string) *Logger

Redact adds items for redaction

type Monitor

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

Monitor monitors values for regular updates

func NewMonitor

func NewMonitor[T any](timeout time.Duration) *Monitor[T]

NewMonitor created a new monitor with given timeout

func (*Monitor[T]) Done

func (m *Monitor[T]) Done() <-chan struct{}

Done signals if monitor has been updated at least once

func (*Monitor[T]) Get

func (m *Monitor[T]) Get() (T, error)

Get returns the current value or ErrOutdated if timeout exceeded

func (*Monitor[T]) GetFunc

func (m *Monitor[T]) GetFunc(get func(T)) error

GetFunc returns the current value or ErrOutdated if timeout exceeded while holding the lock

func (*Monitor[T]) Set

func (m *Monitor[T]) Set(val T)

Set updates the current value and timestamp

func (*Monitor[T]) SetFunc

func (m *Monitor[T]) SetFunc(set func(T) T)

SetFunc updates the current value and timestamp while holding the lock

type Param

type Param struct {
	Loadpoint *int
	Key       string
	Val       interface{}
}

Param is the broadcast channel data type

func (Param) UniqueID

func (p Param) UniqueID() string

UniqueID returns unique identifier for parameter Loadpoint/Key combination

type Queue

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

Queue is based on https://github.com/golang-ds/queue

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue constructs and returns an empty slice-queue.

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear empties the queue.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (val T, ok bool)

Dequeue removes and returns the front element of the queue. It returns false if the queue was empty.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(data T)

Enqueue adds an element to the end of the queue.

func (*Queue[T]) First

func (q *Queue[T]) First() (val T, ok bool)

First returns the front element of the queue. It returns false if the queue was empty.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the queue is empty.

func (*Queue[T]) Size

func (q *Queue[T]) Size() int

Size returns the number of the elements in the queue.

type Redactor

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

Redactor implements log redaction

func (*Redactor) Redact

func (l *Redactor) Redact(redact ...string)

Redact adds items for redaction

type State

type State struct {
	Time time.Time
}

func DecryptState

func DecryptState(enc string, key []byte) (*State, error)

func NewState

func NewState() State

func (*State) Encrypt

func (c *State) Encrypt(key []byte) string

func (*State) Validate

func (c *State) Validate() error

type Tee

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

Tee distributes parameters to subscribers

func (*Tee) Attach

func (t *Tee) Attach() <-chan Param

Attach creates a new receiver channel and attaches it to the tee

func (*Tee) Run

func (t *Tee) Run(in <-chan Param)

Run starts parameter distribution

type TeeAttacher

type TeeAttacher interface {
	Attach() <-chan Param
}

TeeAttacher allows attaching a listener to a tee

Directories

Path Synopsis
Package shortrfc3339 implements helpers for working with shortened RFC-3339 compliant timestamps (those without seconds).
Package shortrfc3339 implements helpers for working with shortened RFC-3339 compliant timestamps (those without seconds).
Package urlvalues provides functions for working with url.Values
Package urlvalues provides functions for working with url.Values

Jump to

Keyboard shortcuts

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