Repository navigation
Add template tag to enable derived values for string fields - #83
Merged
Merged
Conversation
geshtng
reviewed
Oct 29, 2025
Member
Contributor
Author
|
@geshtng Can you share how you run the example? I seem to cannot reproduce it. |
Member
I'm using your code
|
geshtng
reviewed
Nov 1, 2025
geshtng
reviewed
Nov 1, 2025
Member
Member
|
You still need to check about these possible bugs:
|
Contributor
Author
|
@geshtng Sorry, just have time to look at this PR again. |
Member
|
@rizesky func TestTemplateTagWithUnexportedField(t *testing.T) {
type PrivateField struct {
password string // unexported — triggers panic in ctx-building loop
FirstName string `faker:"first_name"`
Slug string `faker:"template:{{.FirstName | slug}}"`
}
var u PrivateField
if err := FakeData(&u); err != nil {
t.Fatal(err)
}
if u.Slug == "" {
t.Fatal("expected non-empty Slug")
}
} |
Co-authored-by: Gerald Sihotang <[email protected]>
…efix matches - faker.go: only treat tags of the form `template:...` as template fields, so bare `template` and unrelated tags like `templated` are no longer hijacked. - template_tag.go: enable `missingkey=error` so references to undefined fields fail loudly instead of expanding to `<no value>`; detect self, forward, and circular references between template fields and return a descriptive error. - template_tag_test.go: add regression cases for circular, self, forward, and missing-field references, plus a prefix-parsing test covering both the no-colon and false-prefix cases.
Addresses the review feedback on this PR.
Unexported fields: the second pass built a map[string]any by calling
reflect.Value.Interface on every field of the struct, which panics for
unexported ones, so any struct with both an unexported field and a template
field crashed FakeData. Templates now render against the struct itself, so
unexported fields are simply not reachable and referencing one is a clean
error rather than a panic. Rendering the struct also gives nested field
access, promoted fields of embedded structs and non-string values for free.
Field order: a template referencing a template field declared later in the
struct was rejected outright. Template fields are now evaluated in dependency
order, worked out by a topological sort over the references each template
actually makes, so declaration order no longer matters. Only a real cycle is
an error, and the message names its members.
References are extracted by walking the parsed template rather than matching
the source with a regular expression. Only the parse tree can tell a
reference from literal text such as "@Example.Com", report {{.Addr.City}} as
a reference to Addr alone, and see that range and with rebind the dot — the
last of which would otherwise invent dependencies and so report cycles that
do not exist.
Tag parsing: decodeTags splits a tag on commas and matches each chunk against
PriorityTags, which mangled template bodies. `template:...,unique` and
`template:...,keep` both blanked the tag, silently filling the field with a
random string, and a body containing something like ", email: {{.X}}" was
hijacked by the email tag. The body is now captured before the priority
scanner runs, so it may contain commas; keep and unique keep their usual
meaning as standalone chunks.
keep is honoured for template fields: a non-zero incoming value is kept.
unique is rejected, because a template over fixed inputs is deterministic and
the retry loop could never produce a different value.
Also: validate the destination type before evaluating anything, so a bad tag
cannot leave some fields written and others not; support named string types
and *string; keep Unicode letters and digits in slug, which used to return ""
for names from the Chinese and Russian providers; and unexport
EvaluateTemplateFields, which exposed reflect internals as public API.
The suite only exercised EvaluateTemplateFields directly, with hand-built
reflect values, so none of the bugs above could have been caught: they all
surface through FakeData. It now covers the unexported-field repro, dependency
ordering, cycles, comma-containing bodies, keep/unique, non-string and pointer
fields, nested and embedded structs, slice elements, option overrides, custom
tag names, unicode slugs and concurrent use, with unit tables for decodeTags,
reference extraction and the topological sort. TestTemplateTagPrefixParsing
asserted `err == nil && x == "..."`, which passed whenever any error was
returned; it now asserts the exact error.
The example had no Output block, so it compiled but never ran. Its inputs are
now pinned with field providers and it asserts its output, and it declares
Email before Username to demonstrate dependency ordering.
Follows how the other tags are documented here: a link to the example file in the Example list, and one Limitation bullet in the same shape as the `oneof` one.
rizesky
force-pushed
the
template-tag-support
branch
from
August 8, 2026 13:51
6c1fac3 to
a27c1e7
Compare
Contributor
Author
|
Hi @geshtng just have time to get back to this, fixed, and I add some more comments and tests to make it safer and clearer |
geshtng
approved these changes
Aug 11, 2026
Member
|
Wdyt? @bxcodec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Add
template:struct tag which renders a Go text/template using values generated for other fields. Templates are evaluated in a second pass after non-template fields are generated, so templates can reference provider-generated values and other template fields (chaining supported). Small helper functions are also provided.Rules
Why
Derived fields (username, email, slug, etc.) are quite common.
Example