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 MustFill ¶
func MustFill(val interface{}, fillers ...Filler)
MustFill fills a type with random data and panics if there is an error.
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.
Types ¶
Click to show internal directories.
Click to hide internal directories.