Documentation
¶
Overview ¶
Package jfdi is short for JSON Fake Data Inventor. It uses a declarative, functional syntax to produce randomly-generated, heterogeneous data structures.
JSON, unlike Go, allows for data structures with mixed types. These are most-easily modeled in Go with map[string]interface{} (for objects) and []interface{} (for arrays). jfdi calls these Map and Slice, for short.
jfdi uses higher-order functions extensively -- constructor functions that return Generator functions, which are called to recursively produce the desired result. For example, to define a JSON object, use the Object function with a template Map. If any values in the Map are also Generators, they are recursively called to fill in the value:
factory := jfdi.Object( jfdi.Map{
"name" : jfdi.Pick("Alice", "Bob", "Carol"),
"age" : jfdi.Int(18, 65),
"ssn" : jfdi.Digits("###-##-####"),
"friends" : jfdi.Array(jfdi.Int(1,3), jfdi.Pick("Dan", "Eve", "Frank")),
})
object := factory(jfdi.NewContext())
fmt.Println(object)
// {"name":"Carol","age":42,"ssn":"314-15-9265","friends":["Eve"]}
Define custom Generators or Generator constructors as needed if built-in Generator constructors aren't enough.
Index ¶
- type Context
- type Generator
- func Array(length, elementModel interface{}) Generator
- func Digits(pattern string) Generator
- func Float64(low, high float64) Generator
- func HexDigits(pattern string) Generator
- func Int(low, high int) Generator
- func Int31(low, high int32) Generator
- func Join(inputs interface{}, separator interface{}) Generator
- func MaxDepthArray(maxDepth int, length, elementModel interface{}) Generator
- func MaxDepthObject(maxDepth int, xs ...interface{}) Generator
- func Object(xs ...interface{}) Generator
- func Pick(xs ...interface{}) Generator
- func RuneMap(pattern string, replacer func(*Context, rune) rune) Generator
- func Sentence() Generator
- func Sentences(n interface{}) Generator
- func Sequence(elementModels ...interface{}) Generator
- func Word() Generator
- func Words(n interface{}) Generator
- type Map
- type Slice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
The Context type is passed down through recursive Generator calls. It tracks depth, provides an independent PRNG, and supports user-defined key/value data.
func NewContext ¶
func NewContext() *Context
NewContext initializes a Context with a fresh PRNG and value map.
type Generator ¶
type Generator func(*Context) interface{}
A Generator is a function for producing arbitrary values. It consumes a jfdi.Context; if nil is provided, a new Context is initialized and used for any recursive value generation calls.
func Array ¶
func Array(length, elementModel interface{}) Generator
Array returns a Generator that constructs a Slice. The first argument, which must be an int or an int generator, determines the length of the Slice; the second argument, which may be a value or an arbitrary Generator, is used for elements of the Slice.
By mixing Generators or constants, various Slice structures are possible:
jfdi.Array(3, 42) // [42, 42, 42] jfdi.Array(3, jfdi.Int(1,6)) // 3 elements of integers from 1-6 jfdi.Array(jfdi.Int(2,4), jfdi.Int(1,6)) // 2-4 elements of integers from 1-6
func Digits ¶
Digits returns a generator that replaces `#` characters in a template string with a random digit from 0 to 9. Backslashes will be treated as escape characters.
func Float64 ¶
Float64 returns a generator that a random float64 in the range [low,high). If `low` is greater than `high`, it panics.
func HexDigits ¶
HexDigits returns a generator that replaces `#` characters in a template string with a random hexadecimal digit from 0 to f. Backslashes will be treated as escape characters.
func Int ¶
Int returns a generator that a random integer in the range [low,high]. If `low` is greater than `high`, it panics.
func Int31 ¶
Int31 returns a generator that a random integer in the range [low,high]. If `low` is greater than `high`, it panics. The range must not exceed 31 bits.
func Join ¶
func Join(inputs interface{}, separator interface{}) Generator
Join returns a generator that joins a slice of strings with a separator string. The first argument must be a slice of string or a generator of them; the second argument must be a string or a generator of one. The Generator panics if either argument produces an invalid type.
func MaxDepthArray ¶
MaxDepthArray works like Array, but it takes an initial argument indicating a maximum depth in a compound data structure. The resulting Generator will return nil instead of an Array if the maxDepth is exceeded. A maxDepth of 0 means depth is unlimited.
func MaxDepthObject ¶
MaxDepthObject works like Object, but it takes an initial argument indicating a maximum depth in a compound data structure. The resulting Generator will return nil instead of a Map if the maxDepth is exceeded. A maxDepth of 0 means depth is unlimited.
func Object ¶
func Object(xs ...interface{}) Generator
Object consumes a variable number of Maps or Map generators to produce a new Generator that constructs a Map. The input list of (possibly generated) Maps are merged by key (last key wins), and then any Generators among the Map values are replaced by the output of the Generator.
If any argument is not a Map or Map generator, the function will panic.
func Pick ¶
func Pick(xs ...interface{}) Generator
Pick returns a generator that chooses one of the arguments with uniform liklihood. If the chosen item is a Generator, the value produced by that generator is returned instead. If no arguments are provided, the generator returns nil.
func RuneMap ¶
RuneMap returns a generator that replaces runes in a template pattern via a user-defined replacement function. Runes in the pattern may be backslash-escaped to prevent replacement.
func Sentence ¶
func Sentence() Generator
Sentence returns a generator that produces a randomly-generated 'latin sentence'.
func Sentences ¶
func Sentences(n interface{}) Generator
Sentences returns a generator that produces a slice of randomly-generated latin 'sentences' of a given slice length. The argument must be an integer or a generator of integers. If the length is negative or zero, the generator panics.
func Sequence ¶
func Sequence(elementModels ...interface{}) Generator
Sequence returns a Generator that constructs a Slice. Each elementModel, which may be a value or a Generator, will be used as elements of the Slice at their respective positions.
By mixing Generators or constants, various Slice structures are possible:
jfdi.Sequence(3, 42) // [3, 42] jfdi.Sequence(jfdi.Int(1,3), jfdi.Int(4,6)) // 2 elements of integers between 1-3 and 4-6 respectively.