ComradeVanti / ComradeVanti/UnityEnumDict

Support for enums with same-value different-name values.

Open
#3 6 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue help wanted
Dominant language
C#
Stars
18
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Heya! Great thanks for your work, I use this package a lot.
I know the project is not active, but maybe this could help someone.

I've tried using the package with EnumDict like so: EnumDict.
That causes recurring errors: .
They were benign for my needs because I don't need the duplicate keys, but this could be more problematic for other uses.

The error:
```
ArgumentException: An item with the same key has already been added. Key: RightMeta
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
Dev.ComradeVanti.EnumDict.EnumDict`2[TEnum,TData].OnAfterDeserialize () (at ./Library/PackageCache/dev.comradevanti.enum-dict@d1fccb7868/Runtime/EnumDict.cs:84)
```

And I found a solution thanks to ChatGPT.
Seems to work perfectly, with little to no changes.

I propose:

On EnumDict.OnBeforeSerialize():72, I replaced GetEnumValues with GetUniqueEnumValues, which is implemented in EnumUtil, shown below.

EnumUtil.GetUniqueEnumValues():
```
// Custom addition by Blawnode.
// Replaces GetEnumValues().
// Supports enums like KeyCode, which has different keys (names) with the same value.
// Requires the addition of Linq, which is already used by EnumDict.
public static IEnumerable GetUniqueEnumValues() where TEnum : Enum
{
return Enum.GetValues(typeof(TEnum))
.Cast()
.GroupBy(e => Convert.ToInt32(e)) // Group by raw int value.
.Select(g => g.First()); // Pick only one name per value.
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.