util

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2024 License: GPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRPCTimeout = errors.New("RPC call timed out")
View Source
var RXTotal, TXTotal int64

Functions

func Atof

func Atof(s string) (float64, error)

atof is a utility for parsing floating point values that sends errors to the logging system.

func CheckJSONVsSchema

func CheckJSONVsSchema[T any](contents []byte, e *ErrorLogger)

func CommaKeyExpand

func CommaKeyExpand[T any](in map[string]T) map[string]T

Given a map from strings to some type T where the keys are assumed to be of the form "foo,bar,bat", return a new map where each comma-delineated string in the keys has its own entry in the returned map. Panics if a key is repeated.

func DecompressZstd

func DecompressZstd(s string) (string, error)

DecompressZstd decompresses data that was compressed using zstd. There's no error handling to speak of, since this is currently only used for data that's baked into the vice binary, so any issues with that should be evident upon a first run.

func DeleteSliceElement

func DeleteSliceElement[V any](s []V, i int) []V

DeleteSliceElement deletes the i'th element of the given slice, returning the resulting slice. Note that the provided slice s is modified!

func DuplicateMap

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

DuplicateMap returns a newly-allocated map that stores copies of all of the values in the given map.

func DuplicateSlice

func DuplicateSlice[V any](s []V) []V

DuplicateSlice returns a newly-allocated slice that is a copy of the provided one.

func FetchURL

func FetchURL(url string) ([]byte, error)

func FilterMap

func FilterMap[K comparable, V any](m map[K]V, pred func(K, V) bool) map[K]V

FilterMap returns a newly-allocated result that is the result of applying the given predicate function to all of the elements in the given map and only including those for which the predicate returned true.

func FilterSlice

func FilterSlice[V any](s []V, pred func(V) bool) []V

FilterSlice applies the given filter function pred to the given slice, returning a new slice that only contains elements where pred returned true.

func FlattenMap

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

FlattenMap takes a map and returns separate slices corresponding to the keys and values stored in the map. (The slices are ordered so that the i'th key corresponds to the i'th value, needless to say.)

func GetLoggedRPCBandwidth

func GetLoggedRPCBandwidth() (int64, int64)

func InsertSliceElement

func InsertSliceElement[V any](s []V, i int, v V) []V

InsertSliceElement inserts the given value v at the index i in the slice s, moving all elements after i one place forward.

func IsAllLetters

func IsAllLetters(s string) bool

func IsAllNumbers

func IsAllNumbers(s string) bool

func IsRPCServerError

func IsRPCServerError(err error) bool

func LoadRawResource

func LoadRawResource(path string) []byte

func LoadResource

func LoadResource(path string) []byte

LoadResource loads the specified file from the resources directory, decompressing it if it is zstd compressed. It panics if the file is not found; missing resources are pretty much impossible to recover from.

func MakeGOBClientCodec

func MakeGOBClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

func MakeGOBServerCodec

func MakeGOBServerCodec(conn io.ReadWriteCloser, lg *log.Logger) rpc.ServerCodec

func MapContains

func MapContains[K comparable, V any](m map[K]V, pred func(K, V) bool) bool

func MapSlice

func MapSlice[F, T any](from []F, xform func(F) T) []T

MapSlice returns the slice that is the result of applying the provided xform function to all of the elements of the given slice.

func ReduceMap

func ReduceMap[K comparable, V any, R any](m map[K]V, reduce func(K, V, R) R, initial R) R

ReduceMap applies the provided reduction function to the given map, starting with the provided initial value. The update rule applied is result=reduce(key, value, result), where the initial value of result is given by the initial parameter.

func ReduceSlice

func ReduceSlice[V any, R any](s []V, reduce func(V, R) R, initial R) R

ReduceSlice applies the provided reduction function to the given slice, starting with the provided initial value. The update rule applied is result=reduce( value, result), where the initial value of result is given by the initial parameter.

func Select

func Select[T any](sel bool, a, b T) T

func SortedMapKeys

func SortedMapKeys[K constraints.Ordered, V any](m map[K]V) []K

SortedMapKeys returns the keys of the given map, sorted from low to high.

func SortedMapKeysPred

func SortedMapKeysPred[K comparable, V any](m map[K]V, pred func(a *K, b *K) bool) []K

SortedMapKeysPred returns the keys of the given map sorted using the provided predicate function which should perform a "less than" comparison of key values.

func StopShouting

func StopShouting(orig string) string

StopShouting turns text of the form "UNITED AIRLINES" to "United Airlines"

func UnmarshalJSON

func UnmarshalJSON[T any](b []byte, out *T) error

Unmarshal the bytes into the given type but go through some efforts to return useful error messages when the JSON is invalid...

func WalkResources

func WalkResources(root string, fn func(path string, d fs.DirEntry, filesystem fs.FS, err error) error) error

func WrapText

func WrapText(s string, columnLimit int, indent int, wrapAll bool) (string, int)

WrapText wraps the provided text string to the given column limit, returning the wrapped string and the number of lines it became. indent gives the amount to indent wrapped lines. By default, lines that start with a space are assumed to be preformatted and are not wrapped; providing a true value for wrapAll overrides that behavior and causes them to be wrapped as well.

Types

type AtomicBool

type AtomicBool struct {
	atomic.Bool
}

AtomicBool is a simple wrapper around atomic.Bool that adds support for JSON marshaling/unmarshaling.

func (AtomicBool) MarshalJSON

func (a AtomicBool) MarshalJSON() ([]byte, error)

func (*AtomicBool) UnmarshalJSON

func (a *AtomicBool) UnmarshalJSON(data []byte) error

type CompressedConn

type CompressedConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func MakeCompressedConn

func MakeCompressedConn(c net.Conn) (*CompressedConn, error)

func (*CompressedConn) Close

func (c *CompressedConn) Close() error

func (*CompressedConn) Read

func (c *CompressedConn) Read(b []byte) (n int, err error)

func (*CompressedConn) Write

func (c *CompressedConn) Write(b []byte) (n int, err error)

type ErrorLogger

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

ErrorLogger is a small utility class used to log errors when validating the parsed JSON scenarios. It tracks context about what is currently being validated and accumulates multiple errors, making it possible to log errors while still continuing validation.

func (*ErrorLogger) Error

func (e *ErrorLogger) Error(err error)

func (*ErrorLogger) ErrorString

func (e *ErrorLogger) ErrorString(s string, args ...interface{})

func (*ErrorLogger) HaveErrors

func (e *ErrorLogger) HaveErrors() bool

func (*ErrorLogger) Pop

func (e *ErrorLogger) Pop()

func (*ErrorLogger) PrintErrors

func (e *ErrorLogger) PrintErrors(lg *log.Logger)

func (*ErrorLogger) Push

func (e *ErrorLogger) Push(s string)

func (*ErrorLogger) String

func (e *ErrorLogger) String() string

type LoggingClientCodec

type LoggingClientCodec struct {
	rpc.ClientCodec
	// contains filtered or unexported fields
}

func MakeLoggingClientCodec

func MakeLoggingClientCodec(label string, c rpc.ClientCodec, lg *log.Logger) *LoggingClientCodec

func (*LoggingClientCodec) ReadResponseHeader

func (c *LoggingClientCodec) ReadResponseHeader(r *rpc.Response) error

func (*LoggingClientCodec) WriteRequest

func (c *LoggingClientCodec) WriteRequest(r *rpc.Request, v any) error

type LoggingConn

type LoggingConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func MakeLoggingConn

func MakeLoggingConn(c net.Conn, lg *log.Logger) *LoggingConn

func (*LoggingConn) Read

func (c *LoggingConn) Read(b []byte) (n int, err error)

func (*LoggingConn) Write

func (c *LoggingConn) Write(b []byte) (n int, err error)

type LoggingMutex

type LoggingMutex struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*LoggingMutex) Lock

func (l *LoggingMutex) Lock(lg *log.Logger)

func (*LoggingMutex) LogValue

func (l *LoggingMutex) LogValue() slog.Value

func (*LoggingMutex) Unlock

func (l *LoggingMutex) Unlock(lg *log.Logger)

type LoggingServerCodec

type LoggingServerCodec struct {
	rpc.ServerCodec
	// contains filtered or unexported fields
}

func MakeLoggingServerCodec

func MakeLoggingServerCodec(label string, c rpc.ServerCodec, lg *log.Logger) *LoggingServerCodec

func (*LoggingServerCodec) ReadRequestHeader

func (c *LoggingServerCodec) ReadRequestHeader(r *rpc.Request) error

func (*LoggingServerCodec) WriteResponse

func (c *LoggingServerCodec) WriteResponse(r *rpc.Response, body any) error

type PendingCall

type PendingCall struct {
	Call      *rpc.Call
	IssueTime time.Time
	OnSuccess func(any)
	OnErr     func(error)
}

func (*PendingCall) CheckFinished

func (p *PendingCall) CheckFinished() bool

type Profiler

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

func CreateProfiler

func CreateProfiler(cpu, mem string) (Profiler, error)

func (*Profiler) Cleanup

func (p *Profiler) Cleanup()

type RPCClient

type RPCClient struct {
	*rpc.Client
}

func (*RPCClient) CallWithTimeout

func (c *RPCClient) CallWithTimeout(serviceMethod string, args any, reply any) error

type RingBuffer

type RingBuffer[V any] struct {
	// contains filtered or unexported fields
}

RingBuffer represents an array of no more than a given maximum number of items. Once it has filled, old items are discarded to make way for new ones.

func NewRingBuffer

func NewRingBuffer[V any](capacity int) *RingBuffer[V]

func (*RingBuffer[V]) Add

func (r *RingBuffer[V]) Add(values ...V)

Add adds all of the provided values to the ring buffer.

func (*RingBuffer[V]) Get

func (r *RingBuffer[V]) Get(i int) V

Get returns the specified element of the ring buffer where the index i is between 0 and Size()-1 and 0 is the oldest element in the buffer.

func (*RingBuffer[V]) Size

func (r *RingBuffer[V]) Size() int

Size returns the total number of items stored in the ring buffer.

type TransientMap

type TransientMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TransientMap represents a set of objects with a built-in expiry time in the future; after an item's time passes, it is automatically removed from the set.

func NewTransientMap

func NewTransientMap[K comparable, V any]() *TransientMap[K, V]

func (*TransientMap[K, V]) Add

func (t *TransientMap[K, V]) Add(key K, value V, d time.Duration)

Add adds a given value to the set; it will no longer be there after the specified duration has passed.

func (*TransientMap[K, V]) Delete

func (t *TransientMap[K, V]) Delete(key K)

Delete deletes the item in the map with the given key, if present.

func (*TransientMap[K, V]) Get

func (t *TransientMap[K, V]) Get(key K) (V, bool)

Get looks up the given key in the map and returns its value and a Boolean that indicates whether it was found.

Jump to

Keyboard shortcuts

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