Skip to content

fix(tags): fill slice and array fields from built-in tags - #108

Merged
bxcodec merged 1 commit into
mainfrom
fix/slice-builtin-tags
Nov 29, 2026
Merged

bxcodec merged 1 commit into
mainfrom
fix/slice-builtin-tags

Conversation

@bxcodec

@bxcodec bxcodec commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #37.

Problem

A built-in tag on a slice field doesn't work. On current main it panics:

type Entity struct {
	Uris    []string `faker:"slice_len=3, url"`
	Devices []string `faker:"uuid_digit"`
}
panic: reflect.Set: value of type string is not assignable to type []string

There are two causes:

  • userDefinedArray passes the whole slice to the tag provider and assigns the result to the field. Built-in providers return a single value, such as a string, so reflect.Set panics.
  • When the tag is combined with slice_len (slice_len=3, url), the full tag string never matches a provider. It falls through to the generic string path, so url is ignored.

Fix

When a slice or array field carries a provider tag, with or without slice_len:

  1. Call the provider with the field. If the result fits the slice type, use it as-is. Custom providers that build the whole slice keep working, including the existing custom-type-over-slice and myint tests.
  2. Otherwise, call the provider once per element. The length comes from slice_len, or from WithRandomMapAndSliceMinSize/MaxSize when slice_len is not set. Arrays are filled to their length. Named element types like type Email string are converted.
  3. If the result fits neither, return an error instead of panicking.

No new tag syntax is needed. The forms people already wrote in the issue now work:

Uris    []string `faker:"slice_len=3, url"`
Devices []string `faker:"uuid_digit"`
IDs     [3]string `faker:"uuid_digit"`

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 as lang=, the bracket idea from #49 could still build on top of this.

Tests

  • New slice_tag_test.go:
    • built-in tags on slices with min/max size options
    • slice_len in both orders and with a space (the exact form from faker slice of urls don't work #37), plus slice_len=0
    • fixed-size arrays
    • named element types
    • a custom per-element provider
    • a custom whole-slice provider with slice_len
    • an incompatible tag returns an error, not a panic
  • example_with_tags_slicelength_test.go shows the new usage.
  • go test -race ./... passes and golangci-lint reports 0 issues.

Note: go test -count=2 fails with "Tag exists" in TestExtend and similar tests. That already happens on main, because those tests register global providers, so it isn't touched here.

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
@bxcodec
bxcodec merged commit 413e6ea into main Nov 29, 2026
2 checks passed
@bxcodec
bxcodec deleted the fix/slice-builtin-tags branch November 29, 2026 14:42
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.

faker slice of urls don't work

2 participants