utils

package
v1.0.0-rc.15 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Copyright (C) 2025 timbastin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Index

Constants

View Source
const ISO8601Format = "2006-01-02T15:04:05.000"

Variables

View Source
var EgressClient = http.Client{
	Timeout: 30 * time.Second,
	Transport: EgressRoundTripper{
		// contains filtered or unexported fields
	},
}
View Source
var GitLister gitLister = commandLineGitLister{}

Functions

func AddSlashCommandsToDependencyVuln

func AddSlashCommandsToDependencyVuln(str *strings.Builder) *strings.Builder

func AddSlashCommandsToFirstPartyVuln

func AddSlashCommandsToFirstPartyVuln(str *strings.Builder) *strings.Builder

func AddToWhitespaceSeparatedStringList

func AddToWhitespaceSeparatedStringList(s string, item string) string

func All

func All[T any](s []T, f func(T) bool) bool

func Any

func Any[T any](s []T, f func(T) bool) bool

func CheckForValidLanguageCode

func CheckForValidLanguageCode(languageCode string) bool

supported languages are declared in `supportedLanguageCodes` at the start of this file

func CompareFirstTwoDecimals

func CompareFirstTwoDecimals(a, b float64) bool

func Concurrently

func Concurrently(fns ...func() (any, error)) concurrentResultSlice

func Contains

func Contains[T comparable](s []T, el T) bool

func ContainsAll

func ContainsAll[T comparable](s []T, needed []T) bool

func ContainsInWhitespaceSeparatedStringList

func ContainsInWhitespaceSeparatedStringList(s string, item string) bool

func Debounce

func Debounce(fn func(), delay time.Duration) func()

func Disjoin

func Disjoin[T any](a []T, predicate func(el T) bool) ([]T, []T)

func DrainChannel

func DrainChannel[T any](ch <-chan T) []T

func EmptyThenNil

func EmptyThenNil(s string) *string

func ErrGroup

func ErrGroup[T any](limit int) *errGroup[T]

func ExtractAttestationPayload

func ExtractAttestationPayload(content string) (any, error)

func Filter

func Filter[T any](s []T, f func(T) bool) []T

func Find

func Find[T any](s []T, f func(T) bool) (T, bool)

func Flat

func Flat[T any](s [][]T) []T

func GetDirFromPath

func GetDirFromPath(path string) string

func HashString

func HashString(s string) string

func Intersect

func Intersect[T comparable](a, b []T) []T

func Map

func Map[T, U any](s []T, f func(T) U) []U

func MapType

func MapType[New any, Old any](s []Old) []New

func Mapper

func Mapper[Key comparable, T any](s []T, f func(T) Key) map[Key]T

func MergeUnrelated

func MergeUnrelated[A, B any](a []A, b []B) []any

func Or

func Or[T any](
	val *T,
	fallback *T,
) *T

func OrDefault

func OrDefault[T any](val *T, def T) T

func PrintBuildInformation

func PrintBuildInformation()

func Ptr

func Ptr[T any](t T) *T

func ReadCsv

func ReadCsv(reader *csv.Reader, fn func(row []string) error) (int, error)

func ReadCsvFile

func ReadCsvFile(filePath string) ([]map[string]any, error)

func ReadCsvInChunks

func ReadCsvInChunks(reader *csv.Reader, chunkSize int, fn func(rows [][]string) error) (int, error)

func ReadFileFromGitRef

func ReadFileFromGitRef(path string, commitSha string, fileName string) ([]byte, error)

func ReadZipFile

func ReadZipFile(zf *zip.File) ([]byte, error)

func Reduce

func Reduce[T, U any](s []T, f func(U, T) U, init U) U

func RemoveFromWhitespaceSeparatedStringList

func RemoveFromWhitespaceSeparatedStringList(s string, item string) string

func RemovePrefixInsensitive

func RemovePrefixInsensitive(input string, prefix string) string

func RunsInCI

func RunsInCI() bool

func SafeDereference

func SafeDereference(s *string) string

func ShannonEntropy

func ShannonEntropy(str string) float64

func SlicePtr

func SlicePtr[T any](t []T) []*T

func Some

func Some[T any](s []T, f func(T) bool) bool

func TeeChannel

func TeeChannel[T any](input <-chan T) (<-chan T, <-chan T)

func UniqBy

func UniqBy[T any, K comparable](s []T, f func(T) K) []T

func Unzip

func Unzip(src, dest string) error

func Values

func Values[K comparable, T any](m map[K]T) []T

func WaitForChannelDrain

func WaitForChannelDrain[T any](ch <-chan T)

func WrapHTTPClient

func WrapHTTPClient(client *http.Client, wrap func(req *http.Request, next http.RoundTripper) (*http.Response, error))

func ZipReaderFromResponse

func ZipReaderFromResponse(resp *http.Response) (*zip.Reader, error)

Types

type BatchModelWriter

type BatchModelWriter[T Tabler, Tx any] interface {
	CreateBatch(tx Tx, ts []T) error
	SaveBatch(tx Tx, ts []T) error
	DeleteBatch(tx Tx, ids []T) error
	SaveBatchBestEffort(tx Tx, ts []T) error
}

type CacheTransport

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

func NewCacheTransport

func NewCacheTransport(cacheSize int, expiration time.Duration) *CacheTransport

func (*CacheTransport) Handler

func (c *CacheTransport) Handler() func(req *http.Request, next http.RoundTripper) (*http.Response, error)

type CompareResult

type CompareResult[T any] struct {
	OnlyInA []T
	OnlyInB []T
	InBoth  []T // returns the elements in A

	InBothB []T
}

func CompareSlices

func CompareSlices[T any, K comparable](a, b []T, serializer func(T) K) CompareResult[T]

type Date

type Date time.Time

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

Implement Marshaler and Unmarshaler interface

type EgressRoundTripper

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

func (EgressRoundTripper) RoundTrip

func (mrt EgressRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

type FireAndForgetSynchronizer

type FireAndForgetSynchronizer interface {
	FireAndForget(fn func())
}

useful for integration testing - use in production to just fire and forget a function "go func()" during testing, this can be used to synchronize the execution of multiple goroutines - and wait for them to finish

type GitVersionInfo

type GitVersionInfo struct {
	IsTag         bool
	BranchOrTag   string
	DefaultBranch *string
}

func GetAssetVersionInfo

func GetAssetVersionInfo(path string) (GitVersionInfo, error)

type GoroutineFireAndForgetSynchronizer

type GoroutineFireAndForgetSynchronizer struct{}

func NewFireAndForgetSynchronizer

func NewFireAndForgetSynchronizer() *GoroutineFireAndForgetSynchronizer

func (*GoroutineFireAndForgetSynchronizer) FireAndForget

func (f *GoroutineFireAndForgetSynchronizer) FireAndForget(fn func())

super simple: create a goroutine and call the function in it

type ModelReader

type ModelReader[ID any, T Tabler] interface {
	Read(id ID) (T, error)
	List(ids []ID) ([]T, error)
	All() ([]T, error)
	Upsert(t *[]*T, conflictingColumns []clause.Column, updateOnly []string) error
}

type ModelWriter

type ModelWriter[ID any, T Tabler, Tx any] interface {
	Create(tx Tx, t *T) error
	Save(tx Tx, t *T) error

	Delete(tx Tx, id ID) error
	Activate(tx Tx, id ID) error
}

type Repository

type Repository[ID any, T Tabler, Tx any] interface {
	ModelWriter[ID, T, Tx]
	ModelReader[ID, T]
	BatchModelWriter[T, Tx]
	Transactioner[Tx]
}

type SyncFireAndForgetSynchronizer

type SyncFireAndForgetSynchronizer struct {
}

func NewSyncFireAndForgetSynchronizer

func NewSyncFireAndForgetSynchronizer() *SyncFireAndForgetSynchronizer

func (*SyncFireAndForgetSynchronizer) FireAndForget

func (f *SyncFireAndForgetSynchronizer) FireAndForget(fn func())

just don't use a goroutine, but call the function directly

type Tabler

type Tabler interface {
	TableName() string
}

type Transactioner

type Transactioner[Tx any] interface {
	Transaction(func(tx Tx) error) error
	GetDB(tx Tx) Tx
	Begin() Tx
}

Jump to

Keyboard shortcuts

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