generators

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2018 License: MIT Imports: 13 Imported by: 0

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 :

It was created as part of mgodatagen, but is standalone and may be used on its own.

Index

Constants

View Source
const (

	// CountAggregator count mode
	CountAggregator = 0
	// ValueAggregator value mode
	ValueAggregator = 1
	// BoundAggregator bound mode
	BoundAggregator = 2
)

Variables

This section is empty.

Functions

func ClearRef added in v0.5.0

func ClearRef()

ClearRef empty references map for tests run

func Float64Bytes

func Float64Bytes(v float64) []byte

Float64Bytes convert an int32 into an array of bytes

func Int32Bytes

func Int32Bytes(v int32) []byte

Int32Bytes convert an int32 into an array of bytes

func Int64Bytes

func Int64Bytes(v int64) []byte

Int64Bytes convert an int64 into an array of bytes

func UInt32Bytes

func UInt32Bytes(v uint32) []byte

UInt32Bytes returns an uint32 into an array of bytes

func UInt64Bytes

func UInt64Bytes(v uint64) []byte

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

type CollInfo struct {
	Count      int
	ShortNames bool
	Version    []int
	Encoder    *Encoder
}

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

func (ci *CollInfo) PreGenerate(k string, v *config.GeneratorJSON, nb int) ([][]byte, byte, error)

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

type Encoder struct {
	Data  []byte
	PCG32 *pcg.PCG32
	PCG64 *pcg.PCG64
}

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

func NewEncoder(size int) *Encoder

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) Truncate

func (e *Encoder) Truncate(n int)

Truncate discards all but the first n unread bytes from the buffer

func (*Encoder) Write

func (e *Encoder) Write(b []byte)

Write appends bytes to the buffer

func (*Encoder) WriteAt

func (e *Encoder) WriteAt(startPos int, b []byte)

WriteAt writes bytes to the buffer at a specific position

func (*Encoder) WriteSingleByte

func (e *Encoder) WriteSingleByte(b byte)

WriteSingleByte appends a single byte to the buffer

type FakerGenerator

type FakerGenerator struct {
	EmptyGenerator
	Faker *faker.Faker
	F     func(f *faker.Faker) string
}

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 Generator

type Generator interface {
	Key() []byte
	Type() byte
	Value()
	Exists() bool
	SetEncoder(e *Encoder)
}

Generator interface for all generator objects

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

type UniqueGenerator struct {
	Values       [][]byte
	CurrentIndex int
}

UniqueGenerator used to create an array containing unique strings

Jump to

Keyboard shortcuts

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