This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
UTY-1970: invalid enum definition #1412
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7f29c33
Shifted C# enums to start from 0
BryanJY-Wong 8077ba5
update changelog
BryanJY-Wong 7c6e938
Changelog entry moved to breaking change
BryanJY-Wong 006fe38
no more sandwich
BryanJY-Wong 1edc476
throw ex when no enum
BryanJY-Wong 3388b3c
Remove extra line in Changelog
BryanJY-Wong a3bfa96
Update CHANGELOG.md
BryanJY-Wong 3303712
Update CHANGELOG.md
BryanJY-Wong e506b56
Update CHANGELOG.md
BryanJY-Wong d71ba80
Update CHANGELOG.md
BryanJY-Wong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,8 @@ public string GetSerializationStatement(string instance, string schemaObject, ui | |
case ValueType.Primitive: | ||
return $"{schemaObject}.{SchemaFunctionMappings.AddSchemaFunctionFromType(PrimitiveType.Value)}({fieldNumber}, {instance});"; | ||
case ValueType.Enum: | ||
return $"{schemaObject}.AddEnum({fieldNumber}, (uint) {instance});"; | ||
var shifter = UnityEnumDetails.GetEnumMinimum(FqnType); | ||
return $"{schemaObject}.AddEnum({fieldNumber}, (uint) ({instance}) + {shifter});"; | ||
case ValueType.Type: | ||
return $"{FqnType}.Serialization.Serialize({instance}, {schemaObject}.AddObject({fieldNumber}));"; | ||
default: | ||
|
@@ -60,7 +61,8 @@ public string GetDeserializationExpression(string schemaObject, uint fieldNumber | |
return | ||
$"{schemaObject}.{SchemaFunctionMappings.GetSchemaFunctionFromType(PrimitiveType.Value)}({fieldNumber})"; | ||
case ValueType.Enum: | ||
return $"({FqnType}) {schemaObject}.GetEnum({fieldNumber})"; | ||
var shifter = UnityEnumDetails.GetEnumMinimum(FqnType); | ||
return $"({FqnType}) ({schemaObject}.GetEnum({fieldNumber}) - {shifter})"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried a |
||
case ValueType.Type: | ||
return $"{FqnType}.Serialization.Deserialize({schemaObject}.GetObject({fieldNumber}))"; | ||
default: | ||
|
27 changes: 26 additions & 1 deletion
27
...dk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/TypeDetails/UnityEnumDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NLog; | ||
|
||
namespace Improbable.Gdk.CodeGeneration.Model.Details | ||
{ | ||
public class UnityEnumDetails : GeneratorInputDetails | ||
{ | ||
public readonly IReadOnlyList<(uint, string)> Values; | ||
|
||
private static readonly Dictionary<string, uint> EnumMinimums = new Dictionary<string, uint>(); | ||
|
||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); | ||
|
||
public UnityEnumDetails(string package, EnumDefinition rawEnumDefinition) | ||
: base(package, rawEnumDefinition) | ||
{ | ||
Values = rawEnumDefinition.Values.Select(value => (value.Value, value.Name)).ToList(); | ||
var min = rawEnumDefinition.Values.Select(list => list.Value).Min(); | ||
BryanJY-Wong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EnumMinimums[FullyQualifiedName] = min; | ||
if (min != 0) | ||
{ | ||
Logger.Warn($"The enum, {Name}, is defined with a minimum value of {min}, which is greater than 0. Shifting the enum values to start from 0. " + | ||
"This will lead to inconsistencies in the values used in Unity and the values captured in snapshots. " + | ||
$"This inconsistency is handled in the serialization/deserialization process but please consider redefining the values in {Name} to start from 0"); | ||
} | ||
|
||
Values = rawEnumDefinition.Values.Select(value => (value.Value - min, value.Name)).ToList(); | ||
} | ||
|
||
public static uint GetEnumMinimum(string fullQualifiedName) | ||
{ | ||
if (!EnumMinimums.TryGetValue(fullQualifiedName, out var output)) | ||
{ | ||
throw new ArgumentException($"Could not find the following enum: {fullQualifiedName}"); | ||
} | ||
|
||
return output; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically we put 'Breaking Changes' at the top as that will be most important to a developer when upgrading