sheriff.Marshal now returns unexported named type, breaking map type assertions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 257
- Forks
- 30
- PR merge metrics
- No merged PRs in 30d
Description
Previously, my code used a type assertion to convert the return value of sheriff.Marshal into a map[string]any:
var publicOptions = &sheriff.Options{
Groups: []string{"public"},
IncludeEmptyTag: true,
}
func MarshalOnlyPublic(config any) (map[string]any, error) {
data, err := sheriff.Marshal(publicOptions, &config)
if err != nil {
return nil, fmt.Errorf("parsing public config data: %w", err)
}
if data == nil {
return nil, nil
}
if dataMap, ok := data.(map[string]any); ok {
return dataMap, nil
}
return nil, fmt.Errorf("invalid public config data format: %w", err)
}
After updating sheriff, my tests started failing because the type assertion no longer works: sheriff.Marshal now returns the named type sheriff.kvStore.
Since kvStore is unexported and not accessible outside the sheriff package, I cannot manipulate it directly.
What is the recommended way to work with the returned value as a regular map without having to copy or convert it again to map[string]any?
For now, my workaround is to type assert the result to the KVStore interface and manually rebuild the map. However, this approach is memory-intensive and far from ideal and this workaround won't fully work for my previous code. If there are embedded structs, all nested maps remain as KVStore instances.
I had a function that performed value replacements across the map, which is now broken. I'm starting to wonder if the only viable workaround is to use json.Marshal followed by json.Unmarshal on the result to convert everything to map[string]any, but that would be much more expensive in terms of time and memory.
func ApplyToMap(mapData *map[string]any, fn ApplyFn) error {
for k, v := range *mapData {
if v == nil {
continue
}
switch value := v.(type) {
case string:
result, err := fn(value)
if err != nil {
return err
}
(*mapData)[k] = result
case map[string]any:
err := ApplyToMap(&value, fn)
if err != nil {
return err
}
(*mapData)[k] = value
case []any:
err := ApplyToAnyList(&value, fn)
if err != nil {
return err
}
(*mapData)[k] = value
}
}
return nil
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining sheriff.Marshal's returned value and the KVStore interface described in the issue, then reproduce the failed map[string]any assertion and the nested-map behavior. Done should provide or document a practical way for callers to work with the marshalled result without the reported repeated copying or JSON round trip.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100