utils

package
v0.0.0-...-c4af43d Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineRegexp

func CombineRegexp(regexps ...*regexp.Regexp) *regexp.Regexp

CombineRegexp creates a single regexp by joining the argument regexps together using the | operator. Each regexp is put into a separate non-capturing group before being combined.

func CreateAndWriteTempFile

func CreateAndWriteTempFile(fileName string, data []byte) error

CreateAndWriteTempFile writes a file in the directory specified by writeBufferFolder.

This directory must be cleaned up with a call to RemoveTempFilesDirectory().

func ExtractArchiveFile

func ExtractArchiveFile(archivePath string, outputDir string) error

ExtractArchiveFile extracts a .tar.gz / .tgz file located at archivePath, using outputDir as the root of the extracted files.

func FloatEquals

func FloatEquals(x1, x2, absTol float64) bool

FloatEquals compares two floats and returns true if they are both within absTol of each other, or are both NaN. Note that normally NaN != NaN, but we define it as true because it's convenient for comparing arrays and structs that contain floats.

func JSONEquals

func JSONEquals(j1, j2 []byte) (bool, error)

JSONEquals compares two byte sequences containing JSON data and returns true if 1) both j1 and j2 contain valid JSON data, and 2) the JSON objects that they represent are equal. If j1 or j2 contain invalid JSON data, an error is returned.

func LastNBytes

func LastNBytes(b []byte, n int) []byte

LastNBytes returns the last n bytes from b. If len(b) <= n, b itself is returned, otherwise a copy of the bytes is returned. If n is negative, the function will panic

func OpenTempFile

func OpenTempFile(fileName string) (*os.File, error)

func RemoveDuplicates

func RemoveDuplicates[T comparable](items []T) []T

RemoveDuplicates takes a slice and returns a new slice with only the unique elements from the input slice. Ordering of the elements in the returned slice corresponds is done according to the earliest index of each unique value in the input slice.

func RemoveTempFilesDirectory

func RemoveTempFilesDirectory() error

func SHA256Hash

func SHA256Hash(path string) (string, error)

SHA256Hash returns the SHA256 hashsum of a file.

func Transform

func Transform[T, R any](ts []T, fn func(T) R) []R

Transform applies the given transform function fn: T -> R to each element t of slice ts and returns a slice containing the corresponding results.

func WriteFile

func WriteFile(path string, contents []byte, executable bool) error

WriteFile writes the given file contents to the given path. The file may optionally be marked as executable.

Types

type CommaSeparatedFlagsData

type CommaSeparatedFlagsData struct {
	Name   string
	Values []string
	Info   string
}

func CommaSeparatedFlags

func CommaSeparatedFlags(name string, values []string, usage string) CommaSeparatedFlagsData

CommaSeparatedFlags creates a struct which can be used with the Golang flag library, to allow passing a comma-separated list of strings as a single command-line argument.

Make sure to call InitFlag() on the returned struct before calling flag.Parse().

func (*CommaSeparatedFlagsData) InitFlag

func (csl *CommaSeparatedFlagsData) InitFlag()

func (*CommaSeparatedFlagsData) Set

func (csl *CommaSeparatedFlagsData) Set(values string) error

func (*CommaSeparatedFlagsData) String

func (csl *CommaSeparatedFlagsData) String() string

Jump to

Keyboard shortcuts

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