Skip to content

Maps with optional keys #36

@whatyouhide

Description

@whatyouhide

Today it's quite nasty to generate maps (with fixed keys and generator values) with optional keys. You have to either build on top of fixed_map/1 but it's a bit convolute to add keys optionally and make it shrink towards having no optional keys. I think it might be nice to have a good interface for this. We can't get inspiration from PropEr or QuickCheck because they don't support maps, and test.check only supports hash-map which is our fixed_map/1.

One idea could be to have optional_map/1 that mirrors fixed_map/1 but which shrinks by taking away key-value pairs as well, and then a structured map with optional keys could be built by doing

fixed = %{
  name: string(:alphanumeric),
}
optional = %{
  age: map(integer(), &abs/1),
}

map({fixed, optional}, fn {fixed_map, optional_map} -> Map.merge(fixed_map, optional_map) end)

I am not sure this is a very elegant solution thought. Ideas? \cc @josevalim

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions