{"id":42961957,"url":"https://github.com/matheus-rosa/go-factory","last_synced_at":"2026-01-30T23:18:39.801Z","repository":{"id":57630986,"uuid":"409377111","full_name":"matheus-rosa/go-factory","owner":"matheus-rosa","description":"Package to create test data in Go","archived":false,"fork":false,"pushed_at":"2021-10-12T15:27:03.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-06-20T15:45:47.425Z","etag":null,"topics":["factories","factory-definitions","go","golang","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matheus-rosa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-22T22:45:26.000Z","updated_at":"2021-10-13T12:52:49.000Z","dependencies_parsed_at":"2022-09-16T17:10:25.311Z","dependency_job_id":null,"html_url":"https://github.com/matheus-rosa/go-factory","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/matheus-rosa/go-factory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matheus-rosa%2Fgo-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matheus-rosa%2Fgo-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matheus-rosa%2Fgo-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matheus-rosa%2Fgo-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matheus-rosa","download_url":"https://codeload.github.com/matheus-rosa/go-factory/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matheus-rosa%2Fgo-factory/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28922677,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-30T22:32:35.345Z","status":"ssl_error","status_checked_at":"2026-01-30T22:32:31.927Z","response_time":66,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["factories","factory-definitions","go","golang","testing"],"created_at":"2026-01-30T23:18:38.895Z","updated_at":"2026-01-30T23:18:39.796Z","avatar_url":"https://github.com/matheus-rosa.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-factory\n\nA simple Golang package to help to fill and create your app domain models. \nIt is heavily inspired by the usage of [thoughtbot/factory_bot](https://github.com/thoughtbot/factory_bot)\nand [ex_machina](https://github.com/thoughtbot/ex_machina) libraries.\n\n## Installation\n\n```shell\ngo get -u github.com/matheus-rosa/go-factory\n```\n\n## Usage\n\nThis package integrates with [GORM](https://github.com/go-gorm/gorm) in order to easily insert your models.\nIf you don't use GORM, probably it isn't made for you (at least if you do want to insert anything on database).\n\n---\nLet's assume that you have an `User` model in your app defined like below:\n\n```go\npackage main\n\ntype User struct {\n    ID        int\n    Name      string\n    Email     string\n}\n```\n\nTo use `go-factory` you'll need to inform a base factory which tells how `User` will be factored:\nhow your models will be created/inserted:\n\n```go\npackage main\n\nimport (\n\tgoFactory \"github.com/matheus-rosa/go-factory\"\n)\n\nfunc main() {\n\tgoFactory.NewFactory(\u0026goFactory.Options{ \n\t\t// gorm.DB is optional if you don't want to insert records on database \n\t\tGorm: gorm.DB, \n\t\tBaseFactory: func() map[string]interface{} {\n\t\t\treturn map[string]interface{}{\n\t\t\t\t\"user\": func(factory goFactory.Factory) *User {\n\t\t\t\t\treturn \u0026User{\n\t\t\t\t\t\tName:  factory.String(\"name\", \"default name\"),\n\t\t\t\t\t\tEmail: factory.String(\"email\", \"mail@mail.com\"),\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t}\n\t\t},\n\t})\n}\n```\n\nFor now on you can easily create by using:\n\n```go\ngoFactory.Create(\"user\", goFactory.Fields{\n\t\"name\": \"John Doe\",\n\t\"email\": \"johndoe@email.com\",\n}).(*User)\n```\n\nOr if you want to insert that record on database:\n\n```go\nuser := goFactory.Insert(\"user\", goFactory.Fields{\n\t\"name\": \"John Doe\",\n\t\"email\": \"johndoe@email.com\",\n}).(*User)\n\nfmt.Println(u.ID)\n```\n\nOr even if you want to create/insert N records:\n\n```go\nusers := make([]User, 3)\ngoFactory.CreateN(\"users\", \u0026users)\n\nfor _, u := users {\n\tfmt.Println(u)\n}\n```\n\n```go\nusers := make([]User, 3)\ngoFactory.InsertN(\"users\", \u0026users)\n\nfor _, u := users {\n\tfmt.Println(u.ID)\n}\n```\n\n## The Fields type\n\nNotice we used the `goFactory.Fields` type on our `Basefactory`.\nThe `goFactory.Fields` can be used to correctly fill your model values, even default values,\nif none were informed. The default value is always the second argument of the type you're using:\n\n```go\nfactory.String(\"name\", \"this is a default name\")\nfactory.Int(\"age\", 29)\n```\n\nYou can use all Golang's basic types. [Checkout the API](https://github.com/matheus-rosa/go-factory/blob/master/fields.go).\n\n### What about nested models?\n\nIt's very unlikely you're only dealing with models without associations.\nSo let's assume our `User` we defined before have one or more `Account`:\n\n```go\npackage main\n\ntype User struct {\n    ID        int\n    Name      string\n    Email     string\n    Accounts  []*Account\n}\n\ntype Account struct {\n    ID        int\n    Name      string\n}\n```\n\nAnd then a little refactor on our `BaseFactory`:\n\n```go\npackage main\n\nimport goFactory \"github.com/matheus-rosa/go-factory\"\n\nfunc main() {\n\tgoFactory.NewFactory(\u0026goFactory.Options{\n\t\t// gorm.DB is optional if you don't want to insert records on database \n\t\tGorm: gorm.DB, \n\t\tBaseFactory: function() map[string]interface{} {\n\t\t\treturn map[string]interface{}{\n\t\t\t\t\"user\": function(factory goFactory.Factory) *User {\n\t\t\t\t\taccountData := factory.GetField(\"account\")\n\t\t\t\t\taccounts := make([]*Account, len(accountData))\n\t\t\t\t\tfactory.CreateN(\"account\", \u0026accounts, accountData...)\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\treturn \u0026User{\n\t\t\t\t\t\tName:  factory.String(\"name\", \"default name\"), \n\t\t\t\t\t\tEmail: factory.String(\"email\", \"mail@mail.com\"), \n\t\t\t\t\t\tAccounts: accounts,\n\t\t\t\t\t}\n\t\t\t\t}, \n\t\t\t\t\"account\": function(factory goFactory.Factory) *Accounts {\n\t\t\t\t\treturn \u0026Accounts{\n\t\t\t\t\t\tName: factory.String(\"name\", \"default account name\"),\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t})\n}\n```\n\nFor now on you can use like:\n\n```go\npackage main\n\nimport goFactory \"github.com/matheus-rosa/go-factory\"\n\nfunc main()  {\n\tgoFactory.Create(\"user\", goFactory.Fields{\n\t\t\"name\": \"John Doe\",\n\t\t\"email\": \"johndoe@email.com\",\n\t\t\"account\": []goFactory.Fields{\n\t\t\t{\"name\": \"first account\"},\n\t\t\t{\"name\": \"second account\"},\n\t\t\t{\"name\": \"third account\"},\n\t\t},\n\t}).(*User)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatheus-rosa%2Fgo-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatheus-rosa%2Fgo-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatheus-rosa%2Fgo-factory/lists"}