jfdi

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

jfdi - JSON Fake Data Inventor (experimental)

Go Reference Go Report Card Github Actions codecov License

A Go library for declarative, randomly-generated, heterogeneous JSON data structures.

[Still under development.]

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.

Copyright 2019 by David A. Golden. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Depth int
	Rand  *rand.Rand
	Value Map
}

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

func Digits(pattern string) Generator

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

func Float64(low, high float64) Generator

Float64 returns a generator that a random float64 in the range [low,high). If `low` is greater than `high`, it panics.

func HexDigits

func HexDigits(pattern string) Generator

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

func Int(low, high int) Generator

Int returns a generator that a random integer in the range [low,high]. If `low` is greater than `high`, it panics.

func Int31

func Int31(low, high int32) Generator

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

func MaxDepthArray(maxDepth int, length, elementModel interface{}) Generator

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

func MaxDepthObject(maxDepth int, xs ...interface{}) Generator

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

func RuneMap(pattern string, replacer func(*Context, rune) rune) Generator

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.

func Word

func Word() Generator

Word returns a generator that produces a randomly-chosen latin word.

func Words

func Words(n interface{}) Generator

Words returns a generator that produces a slice of randomly-chosen latin word of a given length. The argument must be an integer or a generator of integers. If the length is negative or zero, the generator panics.

type Map

type Map map[string]interface{}

Map is shorthand for a key/value map of arbitrary type.

func (Map) String

func (m Map) String() string

String marshals a Map to JSON and returns it as a string. If an error occurs, the error is contained in the string. For more precise error handling, manually marshal to JSON and check the error value.

type Slice

type Slice []interface{}

Slice is shorthand for an array of values with arbitrary type.

func (Slice) String

func (s Slice) String() string

String marshals a Slice to JSON and returns it as a string. If an error occurs, the error is contained in the string. For more precise error handling, manually marshal to JSON and check the error value.

Directories

Path Synopsis
Package examples shows ways of using jfdi.
Package examples shows ways of using jfdi.

Jump to

Keyboard shortcuts

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