Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support set-like array/map fields #14

Open
fumoboy007 opened this issue Jun 5, 2018 · 1 comment
Open

Support set-like array/map fields #14

fumoboy007 opened this issue Jun 5, 2018 · 1 comment

Comments

@fumoboy007
Copy link

Go doesn’t have a native set type. That means developers have to use either an array type or a map type. In both cases, there is high potential for a developer to make a backwards-incompatible change (in terms of hashing).

In the array case, the order of the array is significant for the hashing algorithm. However, for a developer to deduplicate the array, they would most likely use an intermediate map. The problem is Go forces the order of the map to be non-deterministic, so the order of the final array and thus the hash becomes non-deterministic. In this case, we should sort the array before hashing to ensure determinism.

In the map case, the values of the map are insignificant. We should ignore them to prevent the developer breaking the determinism of the hash by using a different map value.

I propose introducing a new struct tag called set that is a Boolean true or false. If true, the library will deduplicate and sort the set values. It would support arrays and maps.

@oliviabarrick
Copy link

I started to implement this and the main issue I ran into is when we're decoding it's hard to check the struct tags as they're out of scope at that point.

To unblock myself, I went ahead and just implemented it on the user side by creating a type that is a string array that has a Strings method that returns a deduplicated sorted list of strings. Then I can use the method setting in the struct tag:

Deps *StringSet `hash:"method:Strings"`

See the commit here: oliviabarrick/hone@091ffbe

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

No branches or pull requests

2 participants