stubs

package module
v0.0.0-...-98bff22 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2017 License: Apache-2.0 Imports: 13 Imported by: 0

README

OpenAPI initiative random data generator Build Status codecov Slack Status

license GoDoc

A library to generate random data for a swagger specification. This is a building block for generating stubs for your API as well as tests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAltGenNames

func RegisterAltGenNames(key string, alts ...string)

RegisterAltGenNames registers alternatives for a generator name

Types

type Generator

type Generator struct {
	Language string
}

Generator generates a stub for a descriptor. A descriptor can either be a parameter, response header or json schema

func (*Generator) GenHeader

func (s *Generator) GenHeader(key string, header *spec.Header) (interface{}, error)

GenHeader generates a random value for a header

func (*Generator) GenParameter

func (s *Generator) GenParameter(key string, param *spec.Parameter) (interface{}, error)

GenParameter generates a random value for a parameter

func (*Generator) GenSchema

func (s *Generator) GenSchema(key string, schema *spec.Schema) (interface{}, error)

GenSchema generates a random value for a schema

func (*Generator) Generate

func (s *Generator) Generate(key string, descriptor interface{}) (interface{}, error)

Generate a stub into the opts.Target

type GeneratorOpts

type GeneratorOpts interface {
	// value generator name
	Name() string

	// Args for the value generator (eg. number of words in a sentence)
	// Arguments here are used to generate a valid value when no validations are specified.
	// Args are used as default setting but validations can override the args should that be necessary.
	Args() []interface{}

	// FieldName for the value generator, this is mostly used as an alternative to the name
	// for inferring which value generator to use
	FieldName() string

	// Type for the value generator to return, adids in inferring the name of the value generator
	Type() string

	// Format for the value generator to return, aids in inferring the name of the value generator
	Format() string

	// Mode which kind of random data to return and to indicate which validation(s) should fail.
	// This is a bitmask so it allows for combinations of invalid values.
	Mode() StubMode

	// Maximum a numeric value can have, returns value, exclusive, defined
	Maximum() (float64, bool, bool)

	// Minimum a numeric value can have, returns value, exclusive, defined
	Minimum() (float64, bool, bool)

	// MaxLength a string can have, returns value, defined
	MaxLength() (int64, bool)

	// MinLength a string can have, returns value, defined
	MinLength() (int64, bool)

	// Pattern a string should match, returns value, defined
	Pattern() (string, bool)

	// MaxItems a collection of values can contain, returns length, defined
	MaxItems() (int64, bool)

	// MinItems a collection of values must contain, returns length, defined
	MinItems() (int64, bool)

	// UniqueItems when true the collection can't contain duplicates
	UniqueItems() bool

	// MultipleOf a numeric value should be divisible by this value, returns value, defined
	MultipleOf() (float64, bool)

	// Enum a list of acceptable values for a value, returns value, defined
	Enum() ([]interface{}, bool)

	// Items options for the members of a collection
	Items() (GeneratorOpts, error)

	// Required when true the property can't be nil
	Required() bool
}

GeneratorOpts interface to capture various types that can get data generated for them.

type StubMode

type StubMode uint64

StubMode for generating data

const (
	// Invalid produces a stub which is invalid for a random validation
	Invalid StubMode = 1 << iota
	// InvalidRequired produces a stub which is invalid for required
	InvalidRequired
	// InvalidMaximum produces a stub which is invalid for maximum
	InvalidMaximum
	// InvalidMinimum produces a stub which is invalid for minimum
	InvalidMinimum
	// InvalidMaxLength produces a stub which is invalid for max length
	InvalidMaxLength
	// InvalidMinLength produces a stub which is invalid for min length
	InvalidMinLength
	// InvalidPattern produces a stub which is invalid for pattern
	InvalidPattern
	// InvalidMaxItems produces a stub which is invalid for max items
	InvalidMaxItems
	// InvalidMinItems produces a stub which is invalid for min items
	InvalidMinItems
	// InvalidUniqueItems produces a stub which is invalid for unique items
	InvalidUniqueItems
	// InvalidMultipleOf produces a stub which is invalid for multiple of
	InvalidMultipleOf
	// InvalidEnum produces a stub which is invalid for enum
	InvalidEnum

	// Valid is the default value and generates valid data
	Valid StubMode = 0
)

func (StubMode) Has

func (s StubMode) Has(m StubMode) bool

Has returns true when this mode has the provided flag configured

type ValueGenerator

type ValueGenerator func(GeneratorOpts) (interface{}, error)

ValueGenerator represents a function to generate a piece of random data

Jump to

Keyboard shortcuts

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