Documentation
¶
Overview ¶
Package datagen provides tools for generating testdata
Index ¶
- Constants
- Variables
- func NewRand() *rand.Rand
- func SepFactors[T constraints.Float | constraints.Integer](sc []int) []T
- func SignedMkStrFunc[T constraints.Signed](nf NumFmt) func(T) string
- func UnsignedMkStrFunc[T constraints.Unsigned](nf NumFmt) func(T) string
- type Case
- type CcySymbolPlacement
- type ComputedValSetter
- type ConstMakeString
- type Country
- type Currency
- type Field
- type Gen
- type GenOptFunc
- type Generator
- type IncrementingValSetter
- type Money
- type MoneyStringMaker
- type MoneyValSetter
- type NegativeFormat
- type NormValSetter
- type NumFmt
- type NumFmtOptFunc
- func NumFmtSetDecimalSep(s string) NumFmtOptFunc
- func NumFmtSetDigitGrpSep(s string) NumFmtOptFunc
- func NumFmtSetNegFmt(negFmt NegativeFormat) NumFmtOptFunc
- func NumFmtSetPrefix(s string) NumFmtOptFunc
- func NumFmtSetSepCount(sc ...int) NumFmtOptFunc
- func NumFmtSetSuffix(s string) NumFmtOptFunc
- func NumFmtSetZeroVal(s string) NumFmtOptFunc
- type Passer
- type Record
- type StringMaker
- type SwitchGen
- type Time2Str
- type TimeGen
- type TimeGenIntervalF
- type TimeGenOptFunc
- type TimeValSetConstInterval
- type TimeValSetGaussianInterval
- type TypedGenerator
- type TypedVal
- type ValCk
- type ValSetter
- type WStringGen
- type WeightedString
Constants ¶
const ( Random seqOrRandType = iota Sequential )
These constants represent the control over whether values are generated sequentially or randomly.
Variables ¶
var Countries = map[string]Country{ "US": { // contains filtered or unexported fields }, "CN": { // contains filtered or unexported fields }, "JP": { // contains filtered or unexported fields }, "DE": { // contains filtered or unexported fields }, "IN": { // contains filtered or unexported fields }, "GB": { // contains filtered or unexported fields }, "FR": { // contains filtered or unexported fields }, "BR": { // contains filtered or unexported fields }, "IT": { // contains filtered or unexported fields }, "CA": { // contains filtered or unexported fields }, "RU": { // contains filtered or unexported fields }, }
Countries is a map giving the country details of the top 10 countries by GDP (as of August 2022). The map keys are the ISO 3166 codes for the countries.
Functions ¶
func NewRand ¶
NewRand returns a new rand.Rand with its own unique source, suitable for generating random variables. It generates the seed for the new value from the current time (in nano seconds) plus a monotonically increasing value.
func SepFactors ¶
func SepFactors[T constraints.Float | constraints.Integer](sc []int) []T
SepFactors returns a slice containing the appropriate factor for each sepCount.
func SignedMkStrFunc ¶
func SignedMkStrFunc[T constraints.Signed](nf NumFmt) func(T) string
SignedMkStrFunc returns a string-maker function which can be use to format a signed integer value according to the NumFmt.
func UnsignedMkStrFunc ¶
func UnsignedMkStrFunc[T constraints.Unsigned](nf NumFmt) func(T) string
UnsignedMkStrFunc returns a string-maker function which can be use to format an unsigned integer value according to the NumFmt.
Types ¶
type Case ¶
type Case[T any] struct { // contains filtered or unexported fields }
Case represents a case in a switch
type CcySymbolPlacement ¶
type CcySymbolPlacement int
CcySymbolPlacement encodes where the currency symbol should appear when displaying a currency amount.
const ( CcySymBefore CcySymbolPlacement = iota CcySymAfter CcySymAtDecimal )
CCYSymBefore means that the symbol should come before the number: $1.23
CcySymAfter means that the symbol comes after the number: 1.23$
CcySymAtDecimal means that the symbol appears where the decimal place would otherwise appear: 1$23
type ComputedValSetter ¶
type ComputedValSetter[T any] struct { // contains filtered or unexported fields }
ComputedValSetter applies the aggregator to the values. The aggregator is expected to apply the result of the aggregation to the passed value.
func NewComputedValSetter ¶
func NewComputedValSetter[T any](aggregator func(*T, ...TypedGenerator[T]), val0 TypedGenerator[T], vals ...TypedGenerator[T], ) *ComputedValSetter[T]
NewComputedValSetter builds and returns a ComputedValSetter of the given type.
func (ComputedValSetter[T]) SetVal ¶
func (vs ComputedValSetter[T]) SetVal(v *T)
SetVal applies the aggregator to the passed value and the list of values.
type ConstMakeString ¶
ConstMakeString implements a const MakeString method always returning the same string
func (ConstMakeString[T]) MakeString ¶
func (s ConstMakeString[T]) MakeString(_ T) string
MakeString returns the const string
type Country ¶
type Country struct {
// contains filtered or unexported fields
}
Country records details about a country. These include the name, the ISO 3166 code, the number format used and the currency.
type Currency ¶
type Currency struct {
// contains filtered or unexported fields
}
Currency records details about a specific currency. These include the name, currency symbol, the ISO 4217 code and how many decimals it uses.
func (Currency) MoneyMkStrFunc ¶
MoneyMkStrFunc returns a to-string function which can be used to format a money amount. A money amount is held as an int64 in the fractional currency unit (cents, pence etc).
func (Currency) NumFmtWithCCY ¶
NumFmtWithCCY converts a number format into one that will format a number as a currency amount (with the currency symbol in the correct place)
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field describes a field in a record
type Gen ¶
type Gen[T any] struct { // contains filtered or unexported fields }
Gen records the information needed to generate a field. It implements the TypedGenerator interface.
func NewConstGen ¶
NewConstGen returns a new Gen of type T which always generates the given string and doesn't change the value.
func NewGen ¶
func NewGen[T any](opts ...GenOptFunc[T]) *Gen[T]
NewGen constructs and returns a new generator of the supplied generic type
type GenOptFunc ¶
GenOptFunc represents a generic option function
func GenSetStringMaker ¶
func GenSetStringMaker[T any](sm StringMaker[T]) GenOptFunc[T]
GenSetStringMaker returns an option func that will set the function used to generate the string value.
func GenSetValSetter ¶
func GenSetValSetter[T any](vs ValSetter[T]) GenOptFunc[T]
GenSetValSetter returns an option func that will set the valSetter on a Gen to the supplied value
func GenSetValue ¶
func GenSetValue[T any](v T) GenOptFunc[T]
GenSetValue returns an option func that will set the value on a Gen to the supplied value
type Generator ¶
type Generator interface {
Generate() string
Next()
}
Generator describes the methods that a field generator must provide.
type IncrementingValSetter ¶
type IncrementingValSetter[T constraints.Integer | constraints.Float] struct { // contains filtered or unexported fields }
IncrementingValSetter implements a ValSetter that will increment the passed value by the incr amount
func NewIncrementingValSetter ¶
func NewIncrementingValSetter[T constraints.Integer | constraints.Float]( i T, ) *IncrementingValSetter[T]
NewIncrementingValSetter creates and returns an IncrementingValSetter
func (IncrementingValSetter[T]) SetVal ¶
func (vs IncrementingValSetter[T]) SetVal(v *T)
SetVal increments the given value by the incr amount
type Money ¶
Money represents a monetary amount. It gives the amount and the currency. Note that the amount is held as an integer. It represents the number of fractional currency units and gives correct results when adding amounts. This is in contrast to holding money amounts as a float where occaisional rounding errors will give incorrect results. Note that for currencies without fractional units in common use (Japanese Yen, Indian Rupee, etc.) the amount is in the major unit (Yen, Rupee etc.)
For instance a Money value of 123, with Currency of US dollars would represent $1.23 not $123.00.
type MoneyStringMaker ¶
type MoneyStringMaker struct {
// contains filtered or unexported fields
}
MoneyStringMaker implements the StringMaker interface
func NewMoneyStringMaker ¶
func NewMoneyStringMaker(f func(Money) string) *MoneyStringMaker
NewMoneyStringMaker returns a new MoneyStringMaker with the StringMaker function initialised correctly.
func (MoneyStringMaker) MakeString ¶
func (m MoneyStringMaker) MakeString(v Money) string
MakeString returns a string representing the money value
type MoneyValSetter ¶
type MoneyValSetter struct {
// contains filtered or unexported fields
}
MoneyValSetter implements the ValSetter interface
func NewMoneyValSetter ¶
func NewMoneyValSetter(vs ValSetter[int64]) *MoneyValSetter
NewMoneyValSetter returns a new MoneyValSetter with the ValSetter function initialised correctly.
func (MoneyValSetter) SetVal ¶
func (m MoneyValSetter) SetVal(v *Money)
SetVal sets the money amount to the next value
type NegativeFormat ¶
type NegativeFormat int
NegativeFormat encodes how negative values should be expressed.
const ( NegFmtMinus NegativeFormat = iota NegFmtAccounts )
NegFmtMinus means that a negative value should be shown with a leading minus sign.
NegFmtAccounts means that a negative value should be indicated by being shown in parentheses.
type NormValSetter ¶
type NormValSetter[T constraints.Integer | constraints.Float] struct { // contains filtered or unexported fields }
NormValSetter implements a ValSetter that will set the passed value to a normally distributed value constrained by the min, max, mean and sd values
func NewNormValSetter ¶
func NewNormValSetter[T constraints.Integer | constraints.Float]( minimum, maximum T, mean, sd float64, ) *NormValSetter[T]
NewNormValSetter creates and returns a NormValSetter
func (NormValSetter[T]) SetVal ¶
func (vs NormValSetter[T]) SetVal(v *T)
SetVal increments the given value by the incr amount
type NumFmt ¶
type NumFmt struct {
// contains filtered or unexported fields
}
NumFmt records details of how to format a number. These include the decimal place character (or radix point), the digit group separator and how many digits are in the groups of digits.
func NewNumFmt ¶
func NewNumFmt(opts ...NumFmtOptFunc) *NumFmt
NewNumFmt generates and returns a new NumFmt. The default value returned will generate a value with a decimal separator of "," and groups of three digits before the decimal separated by commas.
type NumFmtOptFunc ¶
NumFmtOptFunc is the type of a parameter to the NewNumFmt function. It is used to supply optional parameters.
func NumFmtSetDecimalSep ¶
func NumFmtSetDecimalSep(s string) NumFmtOptFunc
NumFmtSetDecimalSep sets the decimalSep on a NumFmt. This is the value (typically a single character) to appear between the whole number and fractional parts of the value.
func NumFmtSetDigitGrpSep ¶
func NumFmtSetDigitGrpSep(s string) NumFmtOptFunc
NumFmtSetDigitGrpSep sets the digitGrpSep on a NumFmt. This is the value (typically a single character) to appear between the groups of digits as specified by the sepCount values.
func NumFmtSetNegFmt ¶
func NumFmtSetNegFmt(negFmt NegativeFormat) NumFmtOptFunc
NumFmtSetNegFmt sets the format of negative values on a NumFmt. The default format places a minus sign at the front of the value, after any prefix.
func NumFmtSetPrefix ¶
func NumFmtSetPrefix(s string) NumFmtOptFunc
NumFmtSetPrefix sets the prefix on a NumFmt. This is what appears before any of the number has been generated.
func NumFmtSetSepCount ¶
func NumFmtSetSepCount(sc ...int) NumFmtOptFunc
NumFmtSetSepCount sets the sepCount on a NumFmt. This is the number of digits in the groups of digits separated by the digitGrpSep. Multiple values can be given and the first will be used as the number of digits in the first group, the second value used for the second group and so on. The last value given is used repeatedly thereafter.
func NumFmtSetSuffix ¶
func NumFmtSetSuffix(s string) NumFmtOptFunc
NumFmtSetSuffix sets the suffix on a NumFmt. This is what appears after the rest of the number has been generated.
func NumFmtSetZeroVal ¶
func NumFmtSetZeroVal(s string) NumFmtOptFunc
NumFmtSetZeroVal sets the zeroVal on a NumFmt. This is the value to be returned when the value is zero. If it has not been set then the zero value as generated by the rest of the number format values will be generated.
type Passer ¶
type Passer interface {
Passes() bool
}
Passer is the interface describing the Passes methof
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record describes a record
func NewRecord ¶
NewRecord constructs and returns a new Record. The Fields given should be in the order wanted in the final record.
func (Record) GenerateAsMap ¶
GenerateAsMap will return a map of field names to strings generated from the fields.
func (Record) GenerateTitles ¶
GenerateTitles will return a slice of strings generated from the field names
type StringMaker ¶
StringMaker is the interface wrapping the MakeString method
type SwitchGen ¶
type SwitchGen[T any] struct { // contains filtered or unexported fields }
SwitchGen records the set of cases and the default value. The Value used is that for the first case that passes. If none pass then the default value is used.
func NewSwitchGen ¶
func NewSwitchGen[T any]( dfltVal TypedGenerator[T], cases ...*Case[T], ) *SwitchGen[T]
NewSwitchGen returns a new switcher of type T. Note that the first parameter gives the default value. This will be used if none of the cases pass. The supplied cases are evaluated in the order they are given and the first one to pass is used to supply the generated value.
type Time2Str ¶
type Time2Str struct {
// contains filtered or unexported fields
}
Time2Str records the details needed to convert from a time.Time to a string.
func NewTime2Str ¶
NewTime2Str returns a new Time2Str with the format value set
type TimeGen ¶
type TimeGen struct {
// contains filtered or unexported fields
}
TimeGen records the information needed to generate a time field
func NewTimeGen ¶
func NewTimeGen(opts ...TimeGenOptFunc) *TimeGen
NewTimeGen creates a new TimeGen object. It will panic if any of the option functions returns an error.
type TimeGenIntervalF ¶
TimeGenIntervalF is the type of the function that returns the next interval between times
func TimeGenConstIntervalF ¶
func TimeGenConstIntervalF(d time.Duration) TimeGenIntervalF
TimeGenConstIntervalF returns an interval func which always returns the supplied duration.
type TimeGenOptFunc ¶
TimeGenOptFunc is the type of an option-setting function that will set a value in a TimeGen
func TimeGenSetInitialTime ¶
func TimeGenSetInitialTime(t time.Time) TimeGenOptFunc
TimeGenSetInitialTime returns a TimeGen Opt function which sets the initial current time. The default value is the current time.
func TimeGenSetIntervalF ¶
func TimeGenSetIntervalF(f TimeGenIntervalF) TimeGenOptFunc
TimeGenSetIntervalF returns a TimeGen Opt function which sets the function which generates the interval between subsequent times
func TimeGenSetLayout ¶
func TimeGenSetLayout(layout string) TimeGenOptFunc
TimeGenSetLayout returns a TimeGen Opt function which sets the layout (the format for displaying the time)
type TimeValSetConstInterval ¶
type TimeValSetConstInterval struct {
// Ival gives the constant duration used as the interval
Ival time.Duration
}
TimeValSetConstInterval provides a SetVal method that will set the time value to a constant interval from its current value.
func (TimeValSetConstInterval) SetVal ¶
func (tvs TimeValSetConstInterval) SetVal(t *time.Time)
SetVal sets the time to a constant duration from its current value
type TimeValSetGaussianInterval ¶
type TimeValSetGaussianInterval struct {
// contains filtered or unexported fields
}
TimeValSetGaussianInterval provides a SetVal method that will set the time value to a random interval from its current value. The interval used is some random value centred on the mean with a standard deviation of sd and then scaled by the units.
func NewTimeValSetGaussianInterval ¶
func NewTimeValSetGaussianInterval(mean, sd float64, units time.Duration, forceGT0 bool, ) *TimeValSetGaussianInterval
NewTimeValSetGaussianInterval constructs a new time value setter with a random interval which follows a Gaussian (normal) distribution.
func (TimeValSetGaussianInterval) SetVal ¶
func (tvs TimeValSetGaussianInterval) SetVal(t *time.Time)
SetVal sets the time to a random duration from its current value. This interval may be negative unless the forceGT0 flag is set. The interval will always be in whole multiples of the units.
type TypedGenerator ¶
TypedGenerator adds the TypedVal interface to a Generator.
type TypedVal ¶
type TypedVal[T any] interface { Value() T }
TypedVal represents a method that returns the internal value of the field rather than the string representation.
type ValCk ¶
type ValCk struct {
Passer
}
ValCk represents a check to be performed on a value
func NewValCk ¶
NewValCk constructs a simple value check that can check whether or not the supplied value passes the supplied check
func NewValCkAnd ¶
NewValCkAnd constructs a value check that will pass if all the supplied checks pass and fail otherwise.
func NewValCkOr ¶
NewValCkOr constructs a value check that will pass if any of the supplied checks pass and fail if none of them do.
type ValSetter ¶
type ValSetter[T any] interface { SetVal(*T) }
ValSetter is the interface wrapping the SetVal method
type WStringGen ¶
type WStringGen struct {
// contains filtered or unexported fields
}
WStringGen is used to generate strings selected from a population of weighted values.
func NewWStringGen ¶
func NewWStringGen(seqOrRand seqOrRandType, ws WeightedString, strs ...WeightedString, ) *WStringGen
NewWStringGen creates a new WStringGen object and returns it. It will panic if any of the weights is <= 0.
The seqOrRand value can be set to Random to cause the values to be chosen randomly from the supplied values. Or it can be set to Sequential and the values are returned in the order supplied. In either case the values are returned in proportions reflecting the weights.
func (WStringGen) Generate ¶
func (sg WStringGen) Generate() string
Generate returns the next string
type WeightedString ¶
WeightedString records a string and an associated weight. When there are multiple such entries the WStringGen will generate strings in the appropriate proportions.