{"id":25269827,"url":"https://github.com/mikeschinkel/go-testdata-defaulter","last_synced_at":"2026-07-18T06:38:28.893Z","repository":{"id":53091328,"uuid":"355394115","full_name":"mikeschinkel/go-testdata-defaulter","owner":"mikeschinkel","description":"Simple package for Go to set table-driven test data defaults so that tables in tests only need include data that differs from defaults.","archived":false,"fork":false,"pushed_at":"2025-02-11T05:38:57.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-14T15:06:18.615Z","etag":null,"topics":["data","defaults","package","testing","tests"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikeschinkel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-07T02:56:16.000Z","updated_at":"2025-02-11T05:39:01.000Z","dependencies_parsed_at":"2025-02-11T06:38:09.756Z","dependency_job_id":null,"html_url":"https://github.com/mikeschinkel/go-testdata-defaulter","commit_stats":null,"previous_names":["mikeschinkel/test-data-defaulter","mikeschinkel/go-testdata-defaulter"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/mikeschinkel/go-testdata-defaulter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeschinkel%2Fgo-testdata-defaulter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeschinkel%2Fgo-testdata-defaulter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeschinkel%2Fgo-testdata-defaulter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeschinkel%2Fgo-testdata-defaulter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeschinkel","download_url":"https://codeload.github.com/mikeschinkel/go-testdata-defaulter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeschinkel%2Fgo-testdata-defaulter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35609253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-18T02:00:07.223Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["data","defaults","package","testing","tests"],"created_at":"2025-02-12T11:19:59.208Z","updated_at":"2026-07-18T06:38:28.858Z","avatar_url":"https://github.com/mikeschinkel.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test Data Defaulter for Go\n\nSimple Go package for defaulting tabular test data in a map of struct pointers\nto structs to the values in a default struct, e.g. `map[string]*yourTestDataStruct` \nwhere `yourTestDataStruct` is whatever struct you need to create for your tests.\n\nHere is an example struct `testdata` and it's related structure `child`:\n\n```go\ntype testdata struct {\n    Name  string\n    Child *child\n}\n\ntype child struct {\n    Key string\n}\n```\nLet's first define our defaults using that `testdata` struct:\n\n```go\nvar defaultData = testdata{\n    Name: \"default-data\",\n    Child: \u0026child{\n        Key: \"default\",\n    },\n}\n```\n\nNow let's create some actual test data, sing a map of pointers to `testdata` instances. Notice how for `testdata` we only set the properties that are changing from the defaults:\n\n```go\nvar testData = map[string]*testdata{\n    \"test1\": {\n        Name: \"foo\",\n    },\n    \"test2\": {\n        Child: \u0026child{Key: \"bar\"},\n    },\n}\n```\n\nTo use this package you simply create a new defaulter instance and then run `ApplyDefaults`:\n\n```go\ndefaulter := testdatadefaulter.New()\nerr := defaulter.ApplyDefaults(\u0026testData, defaultData)\nif err != nil {\n    panic(err)\n}\n```\nAfter running the above code you will see `testData` having the following values, show in JSON format:\n\n```json\n{\n   \"test1\": {\n      \"Name\": \"foo\",\n      \"Child\": {\n         \"Key\": \"default\"\n      }\n   },\n   \"test2\": {\n      \"Name\": \"default-data\",\n      \"Child\": {\n         \"Key\": \"bar\"\n      }\n   }\n}\n```\n\n## Explicitly setting \"empty\" values.\nSometimes you want a default value, but you want to explictly set your test data to an 'empty' value. As we know Go does not support empty values except for the data types that support `nil`, so it is not possible to tell if `string`s and `int`s are explicitly set to their empty value, or just initialized to their empty value.\n\nTo address this logical conundrum `testdatadefaulter` defines `EmptyString` and `ZeroInt` constants you can use instead of `\"\"` and `0` respectively in your test data, assuming the values we chose for these contstants do not clash with your test data needs.\n\nImagine our `testdata` struct from above also had a `Points` property of type `int` that we wanted to default to `100`, but we wanted `test2` to be zero after the defaulting. Here are the changes to the code above to see the merged value for `testdata[\"test1\"].Points==100` and for `testdata[\"test2\"].Points==0`:\n\n```go\nvar defaultData = testdata{\n    Name: \"default-data\",\n    Child: \u0026child{\n        Key: \"default\",\n    },\n    Points: 100,\n}\n\ntype testdata struct {\n    Name  string\n    Child *child\n    Points int\n}\n\nvar testData = map[string]*testdata{\n    \"test1\": {\n        Name: \"foo\"\n    },\n    \"test2\": {\n        Child: \u0026child{Key: \"bar\"},\n        Points: ZeroInt,\n    },\n}\n```\n\n### SetEmptyString() and SetZeroInt()\n\nIf the values you chose do clash with your test data needs then `testdatadefaulter` defines `SetEmptyString()` and `SetZeroInt()` methods to allow you to assign your own sentinel values to use instead. You might use it like so:\n\n```go\nconst myEmptyString = \"~~~\"\nvar testData = map[string]*testdata{\n    \"test3\": {\n        Name: myEmptyString,\n    },\n}\nfunc main() {\n    defaulter := testdatadefaulter.New()\n    defaulter.SetEmptyString(myEmptyString)\n    err := defaulter.ApplyDefaults(\u0026testData, defaultData)\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\n### String and Int not sufficient?\nNeed more than `string` or `int` values to be defaulted to an empty value?  Submit a pull request or even just an issue asking for the enhancement and it's like I will be able to add it quickly.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeschinkel%2Fgo-testdata-defaulter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeschinkel%2Fgo-testdata-defaulter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeschinkel%2Fgo-testdata-defaulter/lists"}