shims

package module
v0.0.0-...-4fac4ec Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 10 Imported by: 3

README

SHIMS

Go Report Card codecov GoDoc

This is a Golang library to make it a little easier to work with maps, arrays, etc, by abstracting away matching functions, etc.

gopher

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayConvert

func ArrayConvert[F any, T any](in []F, f func(F) T) []T

ArrayConvert converts an array of one type to an array of another via an arbitrary transform function.

func ArrayOfElements

func ArrayOfElements[T any](el T) []T

ArrayOfElements creates an array from a type.

func AutoConfigure

func AutoConfigure(src any, dst any) error

func DefaultValue

func DefaultValue[T comparable](in, def T) T

DefaultValue determines if the input is value (not empty), and if it is, returns the input value, otherwise returns a provided default.

func FindArrayMember

func FindArrayMember[T any](in []T, f func(check T) bool) (int, bool)

FindArrayMember locates an array element by arbitrary criteria

func FromGOB64

func FromGOB64[T any](str string) (T, error)

FromGOB64 decodes a base64 wrapped GOB encoded string into an arbitrary object

func IfElse

func IfElse[T any](cond bool, a, b T) T

IfElse is a convenience function for the missing trinary operator.

func InArray

func InArray[T comparable](value T, corpus []T) bool

InArray returns a bool value indicating whether value is found in corpus.

func Keys

func Keys[T any, K comparable](in map[K]T) []K

Keys returns an array of keys in an arbitrary map.

func MapCopy

func MapCopy[T any, K comparable](in map[K]T) map[K]T

MapCopy creates a copy of a map

func MatcherArray

func MatcherArray[T any](in []T, f func(check T) bool) []T

MatcherArray returns all members that match using an arbitrary matching function.

func MatcherArrayOne

func MatcherArrayOne[T any](in []T, f func(check T) bool) (T, bool)

MatcherArrayOne returns the first array member that matches using an arbitrary function. It returns a second parameter indicating whether or not a match had been made, to differentiate from the zero value for the type.

func MatcherKey

func MatcherKey[T any, K comparable](in map[K]T, f func(check T) bool) []K

MatcherKey returns a list of matched keys from an arbitrarily keyed map

func MatcherKeyOne

func MatcherKeyOne[T any, K comparable](in map[K]T, f func(check T) bool) K

MatcherKeyOne returns the first matched key from an arbitrarily keyed map

func MatcherValue

func MatcherValue[T any, K comparable](in map[K]T, f func(check T) bool) ([]T, error)

MatcherValue returns the matched values from an arbitrarily keyed map

func MatcherValueOne

func MatcherValueOne[T any, K comparable](in map[K]T, f func(check T) bool) (T, error)

MatcherValue returns the first matched value from an arbitrarily keyed map

func MergeMap

func MergeMap[V comparable, T any](orig map[V]T, addl map[V]T) map[V]T

MergeMap merges two of the same type of maps together, overwriting values with the additional data.

func ObjectArrayFunc

func ObjectArrayFunc[T any, O any](in []T, f func(reduce T) O) []O

ObjectArrayFunc executes an arbitrary function to reduce an array of objects to an array of arbitrary values.

func RemoveArrayIndex

func RemoveArrayIndex[T any](s []T, index int) []T

RemoveArrayIndex removes an element from an array

func SerializeJsonToFile

func SerializeJsonToFile[T any](fn string, data T) error

SerializeJsonToFile serializes an arbitrary data type in JSON to the specified file

func SerializeJsonToWriter

func SerializeJsonToWriter[T any](w io.Writer, data T) error

SerializeJsonToWriter serializes an arbitrary data type in JSON to the specified io.Writer

func SingleValueDiscardError

func SingleValueDiscardError[T any](ret T, err error) T

SingleValueDiscardError is used to wrap a single value function and discard the additional error.

func ToGOB64

func ToGOB64[T any](m T) (string, error)

ToGOB64 encodes an arbotrary object into a base64 wrapped GOB encoded string

func TypeConvert

func TypeConvert[T any](v any) T

TypeConvert is a convenience function to force cast

func UnserializeJsonFromFile

func UnserializeJsonFromFile[T any](fn string) (T, error)

UnserializeJsonFromFile deserializes a JSON representation from a specified file into an arbitrary object

func UnserializeJsonFromReader

func UnserializeJsonFromReader[T any](r io.Reader) (T, error)

UnserializeJsonFromReader deserializes a JSON representation from a specified io.Reader into an arbitrary object

func Values

func Values[T any, U comparable](in map[U]T) []T

Values returns an array of values in an arbitrary keyed map.

Types

type GOB64Serializer

type GOB64Serializer[T any] struct {
}

func (GOB64Serializer[T]) FromFile

func (s GOB64Serializer[T]) FromFile(fn string) (T, error)

FromFile deserializes a GOB64 representation from a specified file into an arbitrary object

func (GOB64Serializer[T]) FromReader

func (s GOB64Serializer[T]) FromReader(r io.Reader) (T, error)

FromWriter deserializes a GOB64 representation from a specified io.Reader into an arbitrary object

func (GOB64Serializer[T]) ToFile

func (s GOB64Serializer[T]) ToFile(fn string, data T) error

ToFile serializes an arbitrary data type in GOB64 to the specified file

func (GOB64Serializer[T]) ToWriter

func (s GOB64Serializer[T]) ToWriter(w io.Writer, data T) error

ToWriter serializes an arbitrary data type in GOB64 to the specified io.Writer

type JsonSerializer

type JsonSerializer[T any] struct {
}

func (JsonSerializer[T]) FromFile

func (s JsonSerializer[T]) FromFile(fn string) (T, error)

FromFile deserializes a JSON representation from a specified file into an arbitrary object

func (JsonSerializer[T]) FromReader

func (s JsonSerializer[T]) FromReader(r io.Reader) (T, error)

FromWriter deserializes a JSON representation from a specified io.Reader into an arbitrary object

func (JsonSerializer[T]) ToFile

func (s JsonSerializer[T]) ToFile(fn string, data T) error

ToFile serializes an arbitrary data type in JSON to the specified file

func (JsonSerializer[T]) ToWriter

func (s JsonSerializer[T]) ToWriter(w io.Writer, data T) error

ToWriter serializes an arbitrary data type in JSON to the specified io.Writer

type Serializer

type Serializer[T any] interface {
	ToFile(fn string, data T)
	ToWriter(w io.Writer, data T) error
	FromFile(fn string) (T, error)
	FromReader(r io.Reader) (T, error)
}

Directories

Path Synopsis
browsers module
factory module

Jump to

Keyboard shortcuts

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