Skip to content

Conversation

@mathieu-lemay
Copy link
Contributor

Description

Add the ability to register custom functions and use them to dynamically set fields in a struct. This is done by setting the fake tag on a struct field to fn=some_function and then calling faker.RegisterFunction("some_function", func() interface{} {...}). When fake.Struct().Fill(&o) is called, it will call the registered function and use that value to fill the struct. If the return value of the function can't be assigned to the field, the normal logic will be used.

Are you trying to fix an existing issue?

No

Go Version

$ go version
go version go1.24.5 darwin/arm64

Go Tests

$ go test
2025/07/17 09:59:07 Error while requesting https://loremflickr.com/300/200 : request failed
2025/07/17 09:59:07 Error while creating a temp file: temp file creation failed
2025/07/17 09:59:08 Error while requesting https://randomuser.me : request failed
2025/07/17 09:59:08 Error while creating a temp file: temp file creation failed
PASS
ok      github.com/jaswdr/faker/v2      3.394s

@jaswdr jaswdr self-assigned this Jul 18, 2025
@jaswdr
Copy link
Owner

jaswdr commented Jul 29, 2025

Sorry for the delay, I'll take a look on this between today and tomorrow.

@jaswdr jaswdr merged commit 2455a43 into jaswdr:master Aug 5, 2025
23 of 25 checks passed
@jaswdr
Copy link
Owner

jaswdr commented Aug 5, 2025

LGTM, I reviewed but forgot to merge 😂 my bad. I'll do a new release soon. Thank you for the contribution, nice work there.

@mathieu-lemay
Copy link
Contributor Author

No worries! I actually had this sitting on my computer for months before I remembered to open the PR, so what's a few more days at that point? 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants