feign

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: Unlicense Imports: 8 Imported by: 0

README

feign

go.dev CircleCI Go Report Card

feign automatically fills types with random data.

It can be useful when testing, where you only need to verify persistence and retrieval and aren't concerned with valid data values.

Based on https://github.com/bxcodec/faker.

Install

go get -u github.com/mbranch/feign

Examples

// output prints the json value to stdout.
func output(v interface{}) {
  e := json.NewEncoder(os.Stdout)
  e.SetIndent("", "  ")
  _ = e.Encode(v)
}

// String example.

feign.Seed(0)
feign.MustFill(&s)

output(s)
// Output:
// "sVedgmJqWUdRj"

// Example with nested structs.

type OrderItem struct {
  ProductID       int
  Name            string
  PriceFractional int
  Attributes      map[string]string
}

type Order struct {
  ID      uuid.UUID
  Items   []OrderItem
  Created time.Time
}

var o Order
feign.Seed(1)
feign.MustFill(&o, func(path string) (interface{}, bool) {
  switch path {
  case ".Items.PriceFractional":
    return 100 + ((feign.Rand().Int63() % 400) * 25), true
  default:
    return nil, false
  }
})

output(o)
// Output:
// {
//   "ID": "210fc7bb-8186-39ac-48a4-c6afa2f1581a",
//   "Items": [
//     {
//       "ProductID": 23701,
//       "Name": "SCX",
//       "PriceFractional": 2875,
//       "Attributes": {
//         "AmTgVjiMDy": "AGsItGVGGRRDeTRPTNinYcyJ",
//         "EYAua wti": "NPFhIvN",
//         "IN NY": "XAnZHdKrMfWYLFocFYszCG eZj",
//         "TKvBqWJBscgSE": "IsJqDvttR",
//         "gTivDxUcOYVZwJCZbf": "PHPGGhoQQQoCFcgJCLF",
//         "hrzeTWVmkCrTDsmwcpWKwcxnzDyOyqx": "e",
//         "kdflCVbJoFXdsTMGBEdXryjTFQrd": "QW"
//       }
//     }
//   ],
//   "Created": "0073-06-09T21:30:44.650537874Z"
// }

Documentation

Overview

Package feign automatically fills types with random data. It can be useful when testing, where you only need to verify persistence and retrieval and aren't concerned with valid data values.

Example (Fillers)
package main

import (
	"encoding/json"
	"os"

	"github.com/google/uuid"
	"github.com/icrowley/fake"
	"github.com/mbranch/feign"
)

func main() {
	type customer struct {
		ID       uuid.UUID
		Email    string
		Disabled bool
	}
	var c customer
	fake.Seed(0)
	feign.Seed(0)
	feign.MustFill(&c, func(path string) (interface{}, bool) {
		switch path {
		case ".Email":
			return fake.EmailAddress(), true
		default:
			return nil, false
		}
	})

	output(c)
}

// output prints the json value to stdout.
func output(v interface{}) {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "  ")
	_ = e.Encode(v)
}
Output:

{
  "ID": "fa12f92a-fbe0-0f85-08d0-e83bab9cf8ce",
  "Email": "TeresaMiller@Zazio.edu",
  "Disabled": true
}
Example (Int)
package main

import (
	"encoding/json"
	"os"

	"github.com/mbranch/feign"
)

func main() {
	var i int
	feign.Seed(0)
	feign.MustFill(&i)

	output(i)
}

// output prints the json value to stdout.
func output(v interface{}) {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "  ")
	_ = e.Encode(v)
}
Output:

12282
Example (Nested)
package main

import (
	"encoding/json"
	"os"
	"time"

	"github.com/google/uuid"
	"github.com/mbranch/feign"
)

func main() {
	type OrderItem struct {
		ProductID       int
		Name            string
		PriceFractional int
		Attributes      map[string]string
	}

	type Order struct {
		ID      uuid.UUID
		Items   []OrderItem
		Created time.Time
	}

	var o Order
	feign.Seed(1)
	feign.MustFill(&o, func(path string) (interface{}, bool) {
		switch path {
		case ".Items.PriceFractional":
			return 100 + ((feign.Rand().Int63() % 400) * 25), true
		default:
			return nil, false
		}
	})

	output(o)
}

// output prints the json value to stdout.
func output(v interface{}) {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "  ")
	_ = e.Encode(v)
}
Output:

{
  "ID": "210fc7bb-8186-39ac-48a4-c6afa2f1581a",
  "Items": [
    {
      "ProductID": 23701,
      "Name": "SCX",
      "PriceFractional": 2875,
      "Attributes": {
        "AmTgVjiMDy": "AGsItGVGGRRDeTRPTNinYcyJ",
        "EYAua wti": "NPFhIvN",
        "IN NY": "XAnZHdKrMfWYLFocFYszCG eZj",
        "TKvBqWJBscgSE": "IsJqDvttR",
        "gTivDxUcOYVZwJCZbf": "PHPGGhoQQQoCFcgJCLF",
        "hrzeTWVmkCrTDsmwcpWKwcxnzDyOyqx": "e",
        "kdflCVbJoFXdsTMGBEdXryjTFQrd": "QW"
      }
    }
  ],
  "Created": "0073-06-09T21:30:44.650537874Z"
}
Example (String)
package main

import (
	"encoding/json"
	"os"

	"github.com/mbranch/feign"
)

func main() {
	var s string
	feign.Seed(0)
	feign.MustFill(&s)

	output(s)
}

// output prints the json value to stdout.
func output(v interface{}) {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "  ")
	_ = e.Encode(v)
}
Output:

"sVedgmJqWUdRj"
Example (Struct)
package main

import (
	"encoding/json"
	"os"

	"github.com/mbranch/feign"
)

func main() {
	type person struct {
		Name   string
		Age    int
		Skills map[string]bool
	}
	var p person
	feign.Seed(0)
	feign.MustFill(&p)

	output(p)
}

// output prints the json value to stdout.
func output(v interface{}) {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "  ")
	_ = e.Encode(v)
}
Output:

{
  "Name": "sVedgmJqWUdRj",
  "Age": 22264,
  "Skills": {
    " DMYkESUcXArFAGg": true,
    "HbMLpzQAnthAG": false,
    "IgFoyZenboACW": true,
    "osB": false,
    "pSb": true
  }
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnhandledType = errors.New("unhandled type")

ErrUnhandledType is an error returned when the type is not fillable. Unfillable types include funcs, chans, and interfaces. When filling structs, maps, or slices, these types will be ignored.

Functions

func Fill

func Fill(val interface{}, fillers ...Filler) error

Fill fills a type with random data.

func MustFill

func MustFill(val interface{}, fillers ...Filler)

MustFill fills a type with random data and panics if there is an error.

func Rand

func Rand() rand.Source

func Seed

func Seed(seed int64)

Seed uses the provided seed value to initialize the random number generator to a deterministic state. Seed should not be called concurrently.

func TimeFn added in v1.0.4

func TimeFn(fn func() time.Time)

TimeFn uses the provided function to generate random time values globally. This is often relevant when interacting with databases, which may store lower resolution timestamps than possible with time.Time, or when time values need to be within a certain date range.

Types

type Filler

type Filler func(path string) (val interface{}, ok bool)

Filler is a func used to provide the value used to fill a struct field.

Jump to

Keyboard shortcuts

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