Skip to content

Commit

Permalink
Add fakeable types
Browse files Browse the repository at this point in the history
  • Loading branch information
mgilbir committed Apr 4, 2023
1 parent 81341e0 commit 53abad6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ fmt.Println(f.Created.String()) // 1908-12-07 04:14:25.685339029 +0000 UTC
// Nested Struct Fields and Embedded Fields
```

## Fakeable types

It is possible to extend a struct by implementing the `Fakeable` interface
in order to control the generation.

For example, this is useful when it is not possible to modify the struct that you want to fake by adding struct tags to a field but you still need to be able to control the generation process.

```go
// Imagine a CustomTime type that is needed to support a custom JSON Marshaler
type CustomTime time.Time

func (c *CustomTime) Fake(faker *gofakeit.Faker) interface{} {
return CustomTime(time.Now())
}

func (c *CustomTime) MarshalJSON() ([]byte, error) {
//...
}

// This is the struct that we cannot modify to add struct tags
type NotModifiable struct {
Token string
Creation *CustomTime
}

var f NotModifiable
gofakeit.Struct(&f)
fmt.Printf("%s", f.Token) // yvqqdH
fmt.Printf("%s", f.Creation) // 2023-04-02 23:00:00 +0000 UTC m=+0.000000001

```

## Custom Functions

In a lot of situations you may need to use your own random function usage for your specific needs.
Expand Down

0 comments on commit 53abad6

Please sign in to comment.