Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 5385d3c

Browse files
author
Paul Balaji
authored
Resolving type/command/event name clashes (#1222)
1 parent 5bdbc9c commit 5385d3c

File tree

8 files changed

+103
-6
lines changed

8 files changed

+103
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- Fixed an issue where the Deployment Launcher window would feel unresponsive due to saving changes after every input. [#1219](https://github.com/spatialos/gdk-for-unity/pull/1219)
1212
- It will now wait for at least 1 second to elapse after the last change before writing the configuration back to disk.
13+
- Fixed issues ([#957](https://github.com/spatialos/gdk-for-unity/issues/957), [#958](https://github.com/spatialos/gdk-for-unity/issues/958)) where valid schema would generate invalid code due to name clashes. [#1222](https://github.com/spatialos/gdk-for-unity/pull/1222)
14+
- The offending schema properties will no longer be generated and are now logged in the Unity Editor.
1315

1416
### Internal
1517

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Wait, that's illegal!
2+
3+
package improbable.gdk.test;
4+
5+
type InvalidTypeWithNesting {
6+
enum NestedEnum {
7+
SUCCESS = 0;
8+
FAILURE = 1;
9+
}
10+
type NestedType {}
11+
NestedEnum nested_enum = 1;
12+
NestedType nested_type = 2;
13+
}
14+
15+
type EmptyType {}
16+
17+
component InvalidComponentCommand
18+
{
19+
id=12499;
20+
command EmptyType invalid_component_command(EmptyType);
21+
}
22+
23+
component InvalidComponentEvent
24+
{
25+
id=12599;
26+
event EmptyType invalid_component_event;
27+
}

workers/unity/Packages/io.improbable.gdk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/DetailsStore.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public DetailsStore(SchemaBundle bundle, List<string> serializationOverrides, IF
8383

8484
foreach (var kv in Types)
8585
{
86-
kv.Value.PopulateFields(this);
87-
kv.Value.PopulateChildren(this);
86+
kv.Value.Populate(this);
8887
}
8988

9089
foreach (var kv in Components)

workers/unity/Packages/io.improbable.gdk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/UnityCommandDetails.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Improbable.Gdk.CodeGeneration.Model.Details
44
{
55
public class UnityCommandDetails
66
{
7+
public string RawCommandName { get; }
78
public string CommandName { get; }
89
public string CamelCaseCommandName { get; }
910

@@ -14,8 +15,10 @@ public class UnityCommandDetails
1415

1516
public UnityCommandDetails(ComponentDefinition.CommandDefinition commandDefinitionRaw)
1617
{
17-
CommandName = Formatting.SnakeCaseToPascalCase(commandDefinitionRaw.Name);
18+
RawCommandName = commandDefinitionRaw.Name;
19+
CommandName = Formatting.SnakeCaseToPascalCase(RawCommandName);
1820
CamelCaseCommandName = Formatting.PascalCaseToCamelCase(CommandName);
21+
1922
FqnRequestType =
2023
CommonDetailsUtils.GetCapitalisedFqnTypename(commandDefinitionRaw.RequestType);
2124
FqnResponseType =

workers/unity/Packages/io.improbable.gdk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/UnityComponentDetails.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34

@@ -26,11 +27,33 @@ public UnityComponentDetails(string package, ComponentDefinition componentDefini
2627

2728
CommandDetails = componentDefinitionRaw.Commands
2829
.Select(command => new UnityCommandDetails(command))
30+
.Where(commandDetail =>
31+
{
32+
// Return true to keep commands that do not have a name clash with the component
33+
if (!commandDetail.CommandName.Equals(ComponentName))
34+
{
35+
return true;
36+
}
37+
38+
Console.Error.WriteLine($"Error in component \"{ComponentName}\". Command \"{commandDetail.RawCommandName}\" clashes with component name.");
39+
return false;
40+
})
2941
.ToList()
3042
.AsReadOnly();
3143

3244
EventDetails = componentDefinitionRaw.Events
3345
.Select(ev => new UnityEventDetails(ev))
46+
.Where(eventDetail =>
47+
{
48+
// Return true to keep events that do not have a name clash with the component
49+
if (!eventDetail.EventName.Equals(ComponentName))
50+
{
51+
return true;
52+
}
53+
54+
Console.Error.WriteLine($"Error in component \"{ComponentName}\". Event \"{eventDetail.RawEventName}\" clashes with component name.");
55+
return false;
56+
})
3457
.ToList()
3558
.AsReadOnly();
3659

workers/unity/Packages/io.improbable.gdk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/UnityEventDetails.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ namespace Improbable.Gdk.CodeGeneration.Model.Details
44
{
55
public class UnityEventDetails
66
{
7+
public string RawEventName { get; }
78
public string EventName { get; }
89
public string CamelCaseEventName { get; }
910
public string FqnPayloadType { get; }
1011
public uint EventIndex { get; }
1112

1213
public UnityEventDetails(ComponentDefinition.EventDefinition eventDefinitionRaw)
1314
{
14-
EventName = Formatting.SnakeCaseToPascalCase(eventDefinitionRaw.Name);
15+
RawEventName = eventDefinitionRaw.Name;
16+
EventName = Formatting.SnakeCaseToPascalCase(RawEventName);
1517
CamelCaseEventName = Formatting.PascalCaseToCamelCase(EventName);
18+
1619
FqnPayloadType = CommonDetailsUtils.GetCapitalisedFqnTypename(eventDefinitionRaw.Type);
20+
1721
EventIndex = eventDefinitionRaw.EventIndex;
1822
}
1923
}

workers/unity/Packages/io.improbable.gdk.tools/.CodeGenTemplate/CodeGenerationLib/Model/Details/UnityTypeDetails.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using Improbable.Gdk.CodeGeneration.Utils;
@@ -37,7 +38,13 @@ public string GetPartialResourceTypeName()
3738
return FullyQualifiedTypeName.Split("::")[1];
3839
}
3940

40-
public void PopulateChildren(DetailsStore store)
41+
public void Populate(DetailsStore store)
42+
{
43+
PopulateChildren(store);
44+
PopulateFields(store);
45+
}
46+
47+
private void PopulateChildren(DetailsStore store)
4148
{
4249
var children = store.GetNestedTypes(raw.QualifiedName);
4350

@@ -54,10 +61,41 @@ public void PopulateChildren(DetailsStore store)
5461
.AsReadOnly();
5562
}
5663

57-
public void PopulateFields(DetailsStore store)
64+
private void PopulateFields(DetailsStore store)
5865
{
5966
FieldDetails = raw.Fields
6067
.Select(field => new UnityFieldDetails(field, store))
68+
.Where(fieldDetail =>
69+
{
70+
var clashingChildEnums = ChildEnums
71+
.Where(childEnum =>
72+
{
73+
// When field does not clash with child enum, return false
74+
if (!fieldDetail.PascalCaseName.Equals(childEnum.TypeName))
75+
{
76+
return false;
77+
}
78+
79+
Console.Error.WriteLine($"Error in type \"{CapitalisedName}\". Field \"{fieldDetail.Raw.Name}\" clashes with child enum \"{childEnum.TypeName}\".");
80+
return true;
81+
});
82+
83+
var clashingChildTypes = ChildTypes
84+
.Where(childType =>
85+
{
86+
// When field does not clash with child type, return false
87+
if (!fieldDetail.PascalCaseName.Equals(childType.CapitalisedName))
88+
{
89+
return false;
90+
}
91+
92+
Console.Error.WriteLine($"Error in type \"{CapitalisedName}\". Field \"{fieldDetail.Raw.Name}\" clashes with child type \"{childType.CamelCaseName}\".");
93+
return true;
94+
});
95+
96+
// Only return true if the field has no name clashes with child enums and types
97+
return !clashingChildEnums.Any() && !clashingChildTypes.Any();
98+
})
6199
.ToList()
62100
.AsReadOnly();
63101
}

workers/unity/Packages/io.improbable.gdk.tools/GenerateCode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ private static void Generate()
157157
.WithArgs(ConstructArgs(CodegenExe, schemaCompilerPath, workerJsonPath))
158158
.RedirectOutputOptions(OutputRedirectBehaviour.None)
159159
.AddErrorProcessing((line) => errorMessage.Append($"\n{line}"))
160+
.AddErrorProcessing(Debug.LogError)
160161
.AddOutputProcessing(ProcessStdOut)
161162
.Run();
162163

0 commit comments

Comments
 (0)