Skip to content

sheriff.Marshal now returns unexported named type, breaking map type assertions #62

@lucasoares

Description

@lucasoares

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions