go-faker / faker

Go (Golang) Fake Data Generator for Struct, previously https://github.com/bxcodec/faker
https://pkg.go.dev/github.com/go-faker/faker/v4
MIT License
614 stars 30 forks source link

feat: adds the ability to change the default tag name (#27) #28

Closed hohmannr closed 11 months ago

hohmannr commented 1 year ago

This would solve the problem described in #27.

I have added the ability to change the default tag name to a custom one, which is configurable via an options.OptionFunc. I tried to be minimal invasive and have added a test to cover the custom tag case and an example file, so people can find this feature in go.pkg reference and the github repo.

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.09 :tada:

Comparison is base (04f3781) 88.95% compared to head (b102547) 89.04%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #28 +/- ## ========================================== + Coverage 88.95% 89.04% +0.09% ========================================== Files 12 12 Lines 1747 1762 +15 ========================================== + Hits 1554 1569 +15 Misses 138 138 Partials 55 55 ``` | [Impacted Files](https://app.codecov.io/gh/go-faker/faker/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [address.go](https://app.codecov.io/gh/go-faker/faker/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-YWRkcmVzcy5nbw==) | `95.74% <100.00%> (+0.50%)` | :arrow_up: | | [faker.go](https://app.codecov.io/gh/go-faker/faker/pull/28?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-ZmFrZXIuZ28=) | `86.02% <100.00%> (+0.14%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

bxcodec commented 1 year ago

The idea is great, but not sure how you will handle the maintainability and readability of your application later. How will your team understand you're using this library with the custom tag?

hohmannr commented 1 year ago

That is a very good question. We have solved this issue with another package, here goes the story: We have lightweight wrappers around multiple packages, which set them up to our needs. E.g. configuring them in an init() function. We use go playground validator for payload validation with custom functions and the convention is to prefix all custom validation functions with _ e.g. _rfc3339 for custom timestamps in RFC3339 format. In Code it looks like this:

type UserInfo struct {
    FirstName string `validate_gateway:"required,max=256" validate_api:"required,max=256"`
    LastName  string `validate_gateway:"required,max=256" validate_api:"required,max=256"`
    Source    string `validate_gateway:"eq=gateway"       validate_api:"eq=api"`
    Timestamp string `validate_gateway:"_rfc3339"         validate_api:"_rfc3339"`
}

The same strategy can be used for faker to generate testdata. Currently we have a testutil.go file, which has functions like func TestdataUserInfoGateway(t testing.TB) *UserInfo that generate the testdata, if faker supports multiple custom tags, we can get rid of about 60+ functions like this. Here is an example of how it would look like with faker:


type UserInfo struct {
    FirstName string `faker_gateway:"first_name"      faker_api:"first_name"`
    LastName  string `faker_gateway:"last_name"       faker_api:"last_name"`
    Source    string `faker_gateway:"_source_gateway" faker_api:"_source_api"`
    Timestamp string `faker_gateway:"_rfc3339"        faker_api:"_rfc3339"`
}
hohmannr commented 1 year ago

Hey @bxcodec! Hope you are fine! Just wanted to quickly check the status on this PR?