Skip to content

Latest commit

 

History

History
55 lines (44 loc) · 2.62 KB

README.md

File metadata and controls

55 lines (44 loc) · 2.62 KB

go-fakeit

Go Report Card Coverage Status Godoc Release

Go Fakeit provides a way to automatically fake data from Go Structs. It is designed to work with both builtin Go Data Types as well as go-openapi/strfmt Data Types.

It comes with support for Struct Field Tag interpolation to set specific fakers for Struct Fields.

TODO

  • Handle faking Interface, Map & Chanel Struct Fields
  • Complete test coverage for handlers and fakers
  • Add godocs for public functions
  • Add handling for limiting recursion depth of circular dependencies
  • Add Struct Field Tag option to omit Field from being faked

Available Fakers

Fakeit implements the available fakers in the https://github.com/icrowley/fake package. Available fakers can be found on Godoc

Available Handlers

Fakeit comes with support for all builtin Go Types, as well as all go-openapi/strfmt Types. The default fakers which are used for handling each Data Type can be found in the Godoc

Using tags to specify faker rules

The fakeit Tag can be used on Struct Fields to specify faker behaviour. The examples below describe how to utilise the functionality.

Example 1 - Overriding the default faker:

type AwesomeThing struct {
  ID            strfmt.UUID
  Name          string `fakeit:"password,min=25,max=30"`
  Description   *string `fakeit:"paragraph"`
}

Example 2 - Omitting a Field form being faked:

There are cases where you don't want to populate a certain field in a struct (such as when generating data for inserting into a database). In this case the field can be annotated with a hyphen (-). This follows the convention set the the JSON package in Go.

type AwesomeThing struct {
  ID        strfmt.UUID
  Name      string
  DeletedAt *string `fakeit:"-"`
}

The result will be:

thing := AwesomeThing{}
fakeit.Fakeit(&thing)
fmt.Printf("%+v\n", thing)

// Prints output:
// &{ID:34b7a0d3-c753-11e7-9dd1-dca90491e849 Name:Stagorchid DeletedAt:<nil>}