You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Factory gen or fcgen is a library that generates code for your Go structures.
It can easily generate complex objects by using this, and easily maintain those objects generators.
Motivation
When writing code, we often use Go structures to store various data, such as various business entities. Structures can be large and small, passing between different parts of the business logic.
Almost always, we want to write tests and test our business process, make sure that everything works correctly (in fact, prove that it does not work incorrectly).
For example, a user leaves an order, we want to test that the order is valid, saved to the database or vice versa. The problem is that first you need to prepare a structure with an order, and it can contain quite a lot of fields or links to other structures.
Code generation and this library come to the rescue, which will help write factories preparing a structure with a pre-defined set of data with the ability to easily change the data.
Install
Classic way
go install github.com/metalfm/factory/fcgen@latest
Go >= 1.24
go get -tool github.com/metalfm/factory/fcgen@latest
CLI options
_gen.go for each type
-tags string
comma-separated list of build tags to apply
-type string
comma-separated list of type names; must be set">Usage of fcgen: fcgen [flags] -type T [directory] Arguments: [directory] if no set, '.' is uses Flags: -out string output directory; default fc/_gen.go for each type -tags string comma-separated list of build tags to apply -type string comma-separated list of type names; must be set
Okey, show me the code
This is just an example, for simplicity we will describe everything in one file.
Let's ask the factory to build us a User with the required values:
package test
import ( "fmt" "testing"
"fc_test"// the generated factory code will be in this package )
In terms of use, it looks simple, doesn't it? All the magic happens after launch //go:generate, we call a fcgen that analyzes the AST tree with the code, generates simple setters and setter functions for the User structure.
You can take a look at the generated code here, it is very easy to understand what it does.
Possibilities
Generation of factories for structures of any complexity
Support for all basic, composite and crazy data types, for example -- **func(a, b map[string]func(a, b time.Time, u **User, c map[any]any)) [5]**int
Support generics
Support embed basic types or structs
Strong typing for all fields, no reflection and runtime type assertions
Sub factories work from the box
Setter functions are executed when a new instance is created. Convenient if you need to generate a new uuid each time and use the current date and time
Okey, show me more
Random data
Feel free to use any library to generate random data if you need it:
// here you have a database connection and structure, nothing complicated to save it // db -- *sqlx.DB // e -- User struct // you can also extract the incremented ID from database and set it to User })
// we created and got a user that is in the database user:=factoryUser.MustBuildCtx(ctx) }