Documentation ¶
Overview ¶
Package generators used to create bson objects Relevant documentation:
http://bsonspec.org/#/specification
Currently supported BSON types:
- string
- int
- long
- double
- boolean
- date
- objectId
- object
- array
- binary data
Custom types :
- GPS position
- constant
- autoincrement
- reference
- from array
- faker (cf https://github.com/manveru/faker)
It was created as part of mgodatagen, but is standalone and may be used on its own.
Index ¶
- Constants
- func ClearRef()
- func Float64Bytes(v float64) []byte
- func Int32Bytes(v int32) []byte
- func Int64Bytes(v int64) []byte
- func UInt32Bytes(v uint32) []byte
- func UInt64Bytes(v uint64) []byte
- type Aggregator
- type ArrayGenerator
- type AutoIncrementGenerator32
- type AutoIncrementGenerator64
- type BinaryDataGenerator
- type BoolGenerator
- type CollInfo
- func (ci *CollInfo) CreateGenerator(content map[string]config.GeneratorJSON) (*ObjectGenerator, error)
- func (ci *CollInfo) NewAggregatorFromMap(content map[string]config.GeneratorJSON) ([]Aggregator, error)
- func (ci *CollInfo) PreGenerate(k string, v *config.GeneratorJSON, nb int) ([][]byte, byte, error)
- type ConstGenerator
- type DateGenerator
- type Decimal128Generator
- type EmbeddedObjectGenerator
- type EmptyGenerator
- type Encoder
- type FakerGenerator
- type Float64Generator
- type FromArrayGenerator
- type Generator
- type Int32Generator
- type Int64Generator
- type ObjectGenerator
- type ObjectIDGenerator
- type PositionGenerator
- type StringGenerator
- type UniqueGenerator
Constants ¶
const ( // CountAggregator count mode CountAggregator = 0 // ValueAggregator value mode ValueAggregator = 1 // BoundAggregator bound mode BoundAggregator = 2 )
Variables ¶
This section is empty.
Functions ¶
func Float64Bytes ¶
Float64Bytes convert an int32 into an array of bytes
func Int32Bytes ¶
Int32Bytes convert an int32 into an array of bytes
func Int64Bytes ¶
Int64Bytes convert an int64 into an array of bytes
func UInt32Bytes ¶
UInt32Bytes returns an uint32 into an array of bytes
func UInt64Bytes ¶
UInt64Bytes returns an uint64 into an array of bytes
Types ¶
type Aggregator ¶
type Aggregator struct { K string Collection string Database string Field string Query bson.M Mode int }
Aggregator is a type of generator that use another collection to compute aggregation on it
type ArrayGenerator ¶
type ArrayGenerator struct { EmptyGenerator Size int Generator Generator }
ArrayGenerator struct that implements Generator. Used to generate random array
func (*ArrayGenerator) Value ¶
func (g *ArrayGenerator) Value()
Value add a random array of `g.size` size to the encoder. It's feed with the provided generator
type AutoIncrementGenerator32 ¶
type AutoIncrementGenerator32 struct { EmptyGenerator Counter int32 }
AutoIncrementGenerator32 struct that implements Generator. Used to generate auto-incremented int32
func (*AutoIncrementGenerator32) Value ¶
func (g *AutoIncrementGenerator32) Value()
Value add prev counter, starting from `g.counter` to the encoder
type AutoIncrementGenerator64 ¶
type AutoIncrementGenerator64 struct { EmptyGenerator Counter int64 }
AutoIncrementGenerator64 struct that implements Generator. Used to generate auto-incremented int64
func (*AutoIncrementGenerator64) Value ¶
func (g *AutoIncrementGenerator64) Value()
Value add prev counter, starting from `g.counter` to the encoder
type BinaryDataGenerator ¶
type BinaryDataGenerator struct { EmptyGenerator MinLength uint32 MaxLength uint32 }
BinaryDataGenerator struct that implements Generator. Used to generate random binary data
func (*BinaryDataGenerator) Value ¶
func (g *BinaryDataGenerator) Value()
Value add a random array of bytes of length `g.length` to the encoder
type BoolGenerator ¶
type BoolGenerator struct {
EmptyGenerator
}
BoolGenerator struct that implements Generator. Used to generate random bool
func (*BoolGenerator) Value ¶
func (g *BoolGenerator) Value()
Value add a random boolean to the encoder.
type CollInfo ¶ added in v0.5.0
CollInfo stores global info on the colelction to generate
func (*CollInfo) CreateGenerator ¶ added in v0.5.0
func (ci *CollInfo) CreateGenerator(content map[string]config.GeneratorJSON) (*ObjectGenerator, error)
CreateGenerator creates an object generator to get bson.Raw objects
func (*CollInfo) NewAggregatorFromMap ¶ added in v0.5.0
func (ci *CollInfo) NewAggregatorFromMap(content map[string]config.GeneratorJSON) ([]Aggregator, error)
NewAggregatorFromMap creates a slice of aggregator based on a JSON configuration map
func (*CollInfo) PreGenerate ¶ added in v0.5.0
PreGenerate generate `nb`values using a generator created from config
type ConstGenerator ¶
type ConstGenerator struct { EmptyGenerator Val []byte }
ConstGenerator struct that implements Generator. Used to generate constant value. Val already contains the bson element type and the key in addition of the actual value
func (*ConstGenerator) Value ¶
func (g *ConstGenerator) Value()
Value always add the same value as specified in the config file
type DateGenerator ¶
type DateGenerator struct { EmptyGenerator StartDate uint64 Delta uint64 }
DateGenerator struct that implements Generator. Used to generate random date within bounds
func (*DateGenerator) Value ¶
func (g *DateGenerator) Value()
Value add a random date within `g.StartDate` and `g.endDate` Date are not evenly distributed
type Decimal128Generator ¶
type Decimal128Generator struct {
EmptyGenerator
}
Decimal128Generator struct that implements Generator. Used to generate random decimal128
func (*Decimal128Generator) Value ¶
func (g *Decimal128Generator) Value()
Value returns a random Decimal128
type EmbeddedObjectGenerator ¶
type EmbeddedObjectGenerator ObjectGenerator
EmbeddedObjectGenerator struct that implements Generator. Used to generate embedded documents
func (*EmbeddedObjectGenerator) Value ¶
func (g *EmbeddedObjectGenerator) Value()
Value add a random document to the encoder
type EmptyGenerator ¶
type EmptyGenerator struct { // []byte(key) + OxOO K []byte // probability that the element is null, ie doesn't exist (nullPercentage/1000) NullPercentage uint32 // bson type as defined here: http://bsonspec.org/ T byte // structure to hold the encoded document Out *Encoder }
EmptyGenerator serves as base for the actual generators
func (*EmptyGenerator) Exists ¶
func (g *EmptyGenerator) Exists() bool
Exists returns true if the generation should be performed get the last 10 bits of the random int32 to get a number between 0 and 1023, and compare it to nullPercentage * 10
func (*EmptyGenerator) Key ¶
func (g *EmptyGenerator) Key() []byte
Key return the key (bson::e_name) encoded in UTF-8, followed by 0x00
func (*EmptyGenerator) SetEncoder ¶ added in v0.5.0
func (g *EmptyGenerator) SetEncoder(e *Encoder)
SetEncoder set the EmptyGenerator encoder
func (*EmptyGenerator) Type ¶
func (g *EmptyGenerator) Type() byte
Type return the bson type of the element created by the generator
type Encoder ¶
Encoder holds the encoded bytes of the generated document, and a random source. Use github.com/MichaelTJones/pcg instead of math/rand as it's way faster
func NewEncoder ¶
NewEncoder returns a new encoder with PCG seeded with time.Now()
func (*Encoder) Reserve ¶
func (e *Encoder) Reserve()
Reserve add 4 bytes to the buffer. Thoses bytes will be set once the bson value size is known
func (*Encoder) WriteSingleByte ¶
WriteSingleByte appends a single byte to the buffer
type FakerGenerator ¶
FakerGenerator struct that implements Generator. Used to genrate random string using faker library
func (*FakerGenerator) Value ¶
func (g *FakerGenerator) Value()
Value add a string generated by faker library to the encoder
type Float64Generator ¶
type Float64Generator struct { EmptyGenerator Mean float64 StdDev float64 }
Float64Generator struct that implements Generator. Used to generate random int64 between `Min` and `Max`
func (*Float64Generator) Value ¶
func (g *Float64Generator) Value()
Value returns a random float64 between `g.Min` and `g.Max`
type FromArrayGenerator ¶
type FromArrayGenerator struct { EmptyGenerator Size int Array [][]byte Index int DoNotTruncate bool }
FromArrayGenerator struct that implements Generator. Used to generate a random value from an array of user-defined values
func (*FromArrayGenerator) Value ¶
func (g *FromArrayGenerator) Value()
Value add an item of the input array to the encoder
type Int32Generator ¶
type Int32Generator struct { EmptyGenerator Min int32 Max int32 }
Int32Generator struct that implements Generator. Used to generate random int32 between `Min` and `Max`
func (*Int32Generator) Value ¶
func (g *Int32Generator) Value()
Value add a random int32 between `g.Min` and `g.Max` to the encoder
type Int64Generator ¶
type Int64Generator struct { EmptyGenerator Min int64 Max int64 }
Int64Generator struct that implements Generator. Used to generate random int64 between `Min` and `Max`
func (*Int64Generator) Value ¶
func (g *Int64Generator) Value()
Value add a random int64 between `g.Min` and `g.Max` to the encoder
type ObjectGenerator ¶
type ObjectGenerator struct { EmptyGenerator Generators []Generator }
ObjectGenerator struct that implements Generator. Used to generate random object
func (*ObjectGenerator) Value ¶
func (g *ObjectGenerator) Value()
Value add a random object to the encoder
type ObjectIDGenerator ¶
type ObjectIDGenerator struct {
EmptyGenerator
}
ObjectIDGenerator struct that implements Generator. Used to generate bson.ObjectId
func (*ObjectIDGenerator) Value ¶
func (g *ObjectIDGenerator) Value()
Value add a bson.ObjectId to the encoder. As object generation is done in a single goroutine, ne need for sync/atomic
type PositionGenerator ¶
type PositionGenerator struct {
EmptyGenerator
}
PositionGenerator struct that implements Generator. Used to generate random GPS coordinates
func (*PositionGenerator) Value ¶
func (g *PositionGenerator) Value()
Value add a random position to the encoder.
type StringGenerator ¶
type StringGenerator struct { EmptyGenerator MinLength uint32 MaxLength uint32 }
StringGenerator struct that implements Generator. Used to generate random string of a specific length in [`MinLength`, `MaxLength`]
func (*StringGenerator) Value ¶
func (g *StringGenerator) Value()
Value add a random String of a specific length to the encoder
type UniqueGenerator ¶
UniqueGenerator used to create an array containing unique strings