Repository navigation
fix(tags): fill slice and array fields from built-in tags - #108
Merged
Merged
Conversation
A built-in tag such as `uuid_digit` or `url` on a `[]string` field panicked with "reflect.Set: value of type string is not assignable to type []string", because the tag's single value was assigned to the whole slice. When a tag provider returns a value that fits the slice, it is used as-is, so custom providers that build the whole slice keep working. Otherwise the provider is called once per element, honouring slice_len or the random slice size options. A value that fits neither now returns an error instead of panicking. This also makes `slice_len=3, url` work, which previously failed because the combined tag never matched a provider. Fixes #37
geshtng
approved these changes
Nov 29, 2026
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.
Fixes #37.
Problem
A built-in tag on a slice field doesn't work. On current
mainit panics:There are two causes:
userDefinedArraypasses the whole slice to the tag provider and assigns the result to the field. Built-in providers return a single value, such as astring, soreflect.Setpanics.slice_len(slice_len=3, url), the full tag string never matches a provider. It falls through to the generic string path, sourlis ignored.Fix
When a slice or array field carries a provider tag, with or without
slice_len:custom-type-over-sliceandmyinttests.slice_len, or fromWithRandomMapAndSliceMinSize/MaxSizewhenslice_lenis not set. Arrays are filled to their length. Named element types liketype Email stringare converted.No new tag syntax is needed. The forms people already wrote in the issue now work:
Relation to #49
Thanks to @HARDY8118 for #49, which started on this problem. That PR proposes a nested bracket syntax (
faker:"slice_len=3, [url]"). There was an open question in its review about whether new syntax would set expectations for more of it. This PR takes the smaller route: built-in tags on slices just work with the existing syntax, and custom whole-slice providers behave as before. If we later want per-element options on slices, such aslang=, the bracket idea from #49 could still build on top of this.Tests
slice_tag_test.go:slice_lenin both orders and with a space (the exact form from faker slice of urls don't work #37), plusslice_len=0slice_lenexample_with_tags_slicelength_test.goshows the new usage.go test -race ./...passes andgolangci-lintreports 0 issues.Note:
go test -count=2fails with "Tag exists" inTestExtendand similar tests. That already happens onmain, because those tests register global providers, so it isn't touched here.