Skip to content

fix: generate slice_len elements of any type and convert tagged values to named types - #112

Merged
bxcodec merged 1 commit into
go-faker:mainfrom
mansur-akhmadi:fix/slice-len-element-types
Nov 29, 2026
Merged

bxcodec merged 1 commit into
go-faker:mainfrom
mansur-akhmadi:fix/slice-len-element-types

Conversation

@mansur-akhmadi

Copy link
Copy Markdown
Contributor

Fixes #111.

  • With only slice_len set, slice and array elements are now generated by getFakedValue, the same path an untagged slice uses. []float64, []float32, []bool, [][]int and [3]float64 with slice_len now get values instead of an error. getValueWithNoTag had no other caller and is removed.
  • Tagged slice elements and map keys and values are converted to the field's element type before they are set, so a named type such as type Name string no longer panics in reflect.Set / SetMapIndex.

Integer and string elements under slice_len come out as before: getFakedValue uses the same RandomIntegerBoundary and randomString(RandomStringLength, ...) that getValueWithNoTag used.

Not changed: slice_len combined with boundary_start/boundary_end still fails with "not written properly", because the leftover comma after removing slice_len reaches the boundary parser. #108 strips that comma for provider tags; I left boundaries alone to keep this PR small.

How it was tested

  • New TestSliceLenElementTypes (float, float32, bool, named float, named string, nested slice, float array, len + slice_len on a named string; 50 iterations, checks lengths, non-constant floats, both bool values) and TestOneOfNamedElementTypes (named slice and map with oneof, 50 iterations). On main the first fails with Type is not supported by tag. and the second panics with reflect.Set: value of type string is not assignable to type faker.sliceLenName.
  • TestSliceLenUnsupportedElementType: []any with slice_len still returns an error rather than panicking (passes before and after).
  • go test -race -run 'TestSliceLen|TestOneOfNamed' -count=50 .: ok
  • gofmt -l .: empty; go vet ./...: ok; go test -race -count=1 ./...: ok; golangci-lint run -c .golangci.yaml ./... (v2.13.2): 0 issues.
  • make test (gotestsum/tparse) not run; the plain go test commands above were used instead.

…s to named types

A slice_len tag with no other tag sent each element through getValueWithNoTag, which only handled integers and strings, so []float64, []bool, nested slices and float arrays failed with "Type is not supported by tag". Generate those elements with getFakedValue, as an untagged slice does.

Tagged slice and map elements of a named type (type Name string) panicked in reflect.Set because the generated value has the underlying type. Convert it to the element or key type before setting it.
@bxcodec
bxcodec merged commit 2d9ba92 into go-faker:main Nov 29, 2026
2 checks passed
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.

slice_len fails for float, bool and nested slices; tagged slices and maps of named types panic

3 participants