util

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package util contains various utilities utilized for rq

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fake added in v0.0.7

func Fake(kind string) (interface{}, error)

Fake provides a simpler calling convention for fake data generation with reasonable defaults are assumed for faker functions that require arguments, as described on the GetDefaultFakeOptions() function.

Note that this function's seed cannot be changed. If you need a custom seed, you should instantiate a FakeOptions object and use that.

func GetTemplateFuncs added in v0.0.6

func GetTemplateFuncs() template.FuncMap

GetTemplateFuncs returns the standard template utility functions used wherever string templating happens in rq.

func IsPrimitive added in v0.0.5

func IsPrimitive(v interface{}) bool

IsPrimitive returns true if and only if the type of `v` is primitive.

func SortMapPaths added in v0.0.6

func SortMapPaths(paths []MapPath)

SortMapPaths sorts a slice of MapPath objects in-place. If one path is shorter and the shared elements are identical, then the longer path collates last.

func StringToValue

func StringToValue(s string) interface{}

StringToValue attempts to convert the given string into a value. This supports booleans, integers, and floats. The following values are considered booleans, case-insensitive: true, yes, false, no.

Integer radix is inferred by prefix, supporting 0b, 0o, or 0x (per strconv.ParseInt()).

func Tabularize added in v0.0.5

func Tabularize(v interface{}, headers bool) ([][]string, error)

Tabularize will attempt to convert the given value to a table of strings.

If the value is a list of maps, then each map key will become a column, and each map will become one row. If any of the map elements is a slice, then each index in the slice will be a separate column. If any map element contains another map, then each key in that map will be a column as well.

If the value is a list of lists, then the list elements will simply be converted to strings.

If the value is a primitive type, then the resulting table will have just a single cell being the stringification of that value.

Any other shape or type will result in an error.

If headers is false, then the header row will not be generated for the slice-of-maps case.

func Truthy added in v0.0.3

func Truthy(s string) bool

Truthy is the canonical way of determining if a string value is "truthy" in rq.

Truthy values are: 1, true, t, yes, y

Whitespace and case are ignored. All other values are considered falsey.

This method should NOT be used for deserializing string values into booleans, StringToValue() should be used instead. This method is appropriate for user-supplied values that need to be interpreted as booleans.

func Unescape

func Unescape(s string) (string, error)

Unescape unescapes escapes escape codes. I spent at least 15 minutes googling how to do this and couldn't find anything that does, or even anyone else who wanted to do this. I find this pretty shocking and I'm sure there's a better version of this out there somewhere. I also tried using Sprintf(), and that didn't work either.

This unescapes the following sequences: \a, \b, \e, \f, \n, \r, \t, \v, \\, \', and \"

func ValueToString added in v0.0.3

func ValueToString(v interface{}) string

ValueToString can be used to convert primitive types to strings.

NOTE: because int32 aliases with rune, I have chosen to support rune out of the two. Thus any int32 values will treated as runes. A workaround for this behavior is to case them to int64 first.

Types

type FakeOptions added in v0.0.7

type FakeOptions struct {
	// Faker is the faker.Faker instance to use for calls to .Fake().
	Faker faker.Faker

	// MinTime is the time to use for faker functions that accept a minimum time.
	MinTime time.Time

	// Maxtime is the time to use for faker functions that accept a maximum time.
	MaxTime time.Time

	// BinaryStringMaxLength is The maximum length of binary strings to generate.
	BinaryStringMaxLength int

	// PathLength is length to use for directory or file paths. If the
	// value is negative, then the directory length is instead randomized
	// each call to be between `0` and `-1 * PathLength`, inclusive.
	PathLength int

	// LoremParagraphLength is used as the value for nbParagraph in calls
	// to the Lorem API.
	LoremParagraphLength int
}

FakeOptions reprints the entire domain for all faker functions that are wrapped by this API.

var TemplateFakeOptions FakeOptions = GetDefaultFakeOptions()

TemplateFakeOptions stores the fake options used with GetTemplateFuncs().

func GetDefaultFakeOptions added in v0.0.7

func GetDefaultFakeOptions() FakeOptions

GetDefaultFakeOptions returns a set of reasonable default options to use with the faker API.

* The maximum time is 256 years after 1970-01-01T00:00:00+00:00. * The minimum time is 256 years before 1970-01-01T00:00:00+00:00. * The maximum length for binary strings is 32. * Directory lengths are chosen to be random values between 0 and 7.

These date values are intentionally chosen to be earlier than 1900 (should catch date errors with 2-digit base 10 years and old Excel versions, among other incorrectly written software) and after the Y2038 bug (should catch date errors with old *NIX/libc) but also be relatively close to the present day, that make sense to reason about. Production software should be able to handle any dates in this range with ease.

func (FakeOptions) Fake added in v0.0.7

func (o FakeOptions) Fake(kind string) (interface{}, error)

Fake is a wrapper around github.com/jaswdr/faker. It's in the util package so that it can be exposed in rq's string templating functions, as well as in it's builtin repertoire.

The available `kind` strings correspond closely to faker functions, typically by converting to lower snake case, and eliminating spaces and `()`. For example, `Faker.Address.City()` corresponds to the kind `address.city`. Some of the kinds have aliases, such as `city` being equivalent to `address.city`.

The suggested way to determine the available `kind` values is to read the source code of this function.

type MapPath added in v0.0.6

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

MapPath represents a path within a nested collection of maps.

func Keyspace added in v0.0.6

func Keyspace(prefix MapPath, v interface{}) []MapPath

Keyspace returns one MapPath object for every value recursively nested under the map v. All generate MapPath objects are created by calling .Child() on prefix.

func ParseMapPath added in v0.0.6

func ParseMapPath(s string) MapPath

ParseMapPath parses a string to a new MapPath object. It is suitable for use on the output of MapPath.String().

func (MapPath) Access added in v0.0.6

func (p MapPath) Access(v interface{}) (interface{}, bool)

Access attempts to retrieve the value from v at the path specified.

func (MapPath) Child added in v0.0.6

func (p MapPath) Child(elem string) MapPath

Child creates a new map path with a single new path element appended.

func (MapPath) Slice added in v0.0.6

func (p MapPath) Slice() []string

Slice returns a slice representation of the MapPath elements.

func (MapPath) String added in v0.0.6

func (p MapPath) String() string

String generates a canonical string representation of p, suitable for use with ParseMapPath().

func (MapPath) Suffix added in v0.0.6

func (p MapPath) Suffix() MapPath

Suffix creates a new map path containing all but the rightmost element of p.

Jump to

Keyboard shortcuts

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