Skip to content

Commit 24939ac

Browse files
authored
various improvements (#15)
1 parent 9de94a0 commit 24939ac

13 files changed

Lines changed: 57 additions & 7 deletions

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All package updates & migration steps will be listed in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [3.9.0] - 2024-07-01
8+
### Added
9+
- Additional checks to prevent declaring references with `IBaseInfo` and `IBaseStruct`
10+
### Changed
11+
- Printing info at runtime will print interface name & identifier
12+
- Updated README about inheritance
13+
714
## [3.8.6] - 2024-06-06
815
### Fixed
916
- Missing `using` for `DateTime` & `TimeSpan` usage in structs

Editor/EditorParameterConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class EditorParameterConstants
1818
/// Ideally it would be the most convenient to use the package version but that requires file I/O to
1919
/// the package.json which can be costly if we're doing it all of the time.
2020
/// </summary>
21-
public const string InterfaceHashSalt = "7e10aadc-9b6c-41a7-9c7e-ee672e5a21c8";
21+
public const string InterfaceHashSalt = "27d36d97-4bdd-48f5-9d75-a4490bf5aecf";
2222

2323
public static string SanitizedDataPath()
2424
{

Editor/Models/ParameterInterface.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Text.RegularExpressions;
6+
using PocketGems.Parameters.Interface;
67
using PocketGems.Parameters.PropertyTypes;
78
using PocketGems.Parameters.Util;
89

@@ -92,6 +93,12 @@ public virtual bool Validate(out IReadOnlyList<string> outErrors)
9293
errors.Add($"Duplicate property [{propertyName}] in interface [{_type.Name}].");
9394
if (EditorParameterConstants.Interface.InvalidReservedPropertyNames.Contains(propertyName.ToLower()))
9495
errors.Add($"Property name [{propertyName}] is invalid & reserved. It cannot be used in interface [{_type.Name}].");
96+
if ((ParameterReferencePropertyType.IsReferenceType(propertyType.PropertyInfo, out var genericType) && genericType == typeof(IBaseInfo)) ||
97+
(ParameterReferenceListPropertyType.IsListReferenceType(propertyType.PropertyInfo, out genericType) && genericType == typeof(IBaseInfo)))
98+
errors.Add($"Cannot define {propertyName} as {nameof(ParameterReference)} with {nameof(IBaseInfo)} in {InterfaceName}.");
99+
if ((ParameterStructReferencePropertyType.IsReferenceType(propertyType.PropertyInfo, out genericType) && genericType == typeof(IBaseStruct)) ||
100+
(ParameterStructReferenceListPropertyType.IsListReferenceType(propertyType.PropertyInfo, out genericType) && genericType == typeof(IBaseStruct)))
101+
errors.Add($"Cannot define {propertyName} as {nameof(ParameterStructReference)} with {nameof(IBaseStruct)} in {InterfaceName}.");
95102
propertyNames.Add(propertyName);
96103
}
97104

Editor/Operations/Code/GenerateImplementationFilesOperation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override void Execute(ICodeOperationContext context)
2828
var soFilename = CodeGenerator.GenerateScriptableObjectFile(parameterInfo, index, scriptableObjectDir);
2929
soFileRemover.UsedFile(soFilename);
3030

31-
var fbClassFile = CodeGenerator.GenerateFlatBufferClassFile(parameterInfo, flatBufferClassesDir);
31+
var fbClassFile = CodeGenerator.GenerateFlatBufferClassFile(parameterInfo, true, flatBufferClassesDir);
3232
fbFileRemover.UsedFile(fbClassFile);
3333
index++;
3434
}
@@ -40,7 +40,7 @@ public override void Execute(ICodeOperationContext context)
4040
var structFilename = CodeGenerator.GenerateStructFile(parameterStruct, structsDir);
4141
structFileRemover.UsedFile(structFilename);
4242

43-
var fbClassFile = CodeGenerator.GenerateFlatBufferClassFile(parameterStruct, flatBufferClassesDir);
43+
var fbClassFile = CodeGenerator.GenerateFlatBufferClassFile(parameterStruct, false, flatBufferClassesDir);
4444
fbFileRemover.UsedFile(fbClassFile);
4545
}
4646

Editor/Templates/FlatBufferClass.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ end
4343
_fb = fb;
4444
}
4545

46+
{{~if isInfo~}}
47+
public override string ToString() => $"{{interfaceName}}:{_identifier}";
48+
49+
{{~end~}}
4650
#region property implementation
4751

4852
{{~for propertyTypeDict in propertyTypeDicts~}}

Editor/Util/CodeGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public static string GenerateStructFile(IParameterStruct parameterStruct, string
206206
/// <param name="parameterInterface">Interface to write the class for.</param>
207207
/// <param name="outputDirectory">Directory to write the class to.</param>
208208
/// <returns>filepath of the file written</returns>
209-
public static string GenerateFlatBufferClassFile(IParameterInterface parameterInterface, string outputDirectory)
209+
public static string GenerateFlatBufferClassFile(IParameterInterface parameterInterface, bool isInfo, string outputDirectory)
210210
{
211211
if (!Directory.Exists(outputDirectory))
212212
Directory.CreateDirectory(outputDirectory);
@@ -241,6 +241,7 @@ public static string GenerateFlatBufferClassFile(IParameterInterface parameterIn
241241
var args = new Dictionary<string, object>
242242
{
243243
{ "isAddressable", IsAddressable() },
244+
{ "isInfo", isInfo },
244245
{ "disableSymbol", EditorParameterConstants.Interface.DisableImplementationSymbol },
245246
{ "namespace", parameterInterface.GeneratedNameSpace },
246247
{ "className", parameterInterface.FlatBufferClassName(false) },

README/InterfacesAndEnums.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Requirements for interfaces are as follows:
7373
- Interface name must follow the pattern `I[A-Z]+[A-Za-z0-9]*Info`.
7474
- Visbility must be `public`.
7575
- Must not have a namespace.
76-
- Must inherit from or from an interface that inherits `IBaseInfo`.
76+
- Must inherit from `IBaseInfo` or from an interface that inherits `IBaseInfo`.
77+
- May inherit from multiple other `Info` interfaces as long as property names do not collide.
7778
- Must NOT inherit any other external interfaces (e.g. `IEnumerable`).
7879
- Can only define public property getters. Methods & setters are not allowed.
7980
- Propery names must follow the pattern `[A-Z]+[A-Za-z0-9]*`.

Tests/Editor/Models/ParameterInfoTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public void ValidInterface()
3232
[TestCase(typeof(IInternalPropertyInfo))] // property is defined as non public
3333
[TestCase(typeof(IMethodInfo))] // interface defined a method
3434
[TestCase(typeof(INameSpacedInfo))] // interface has a name space
35+
[TestCase(typeof(IReferenceBaseInfo))] // interface has reference to base info
36+
[TestCase(typeof(IListReferenceBaseInfo))] // interface has reference to base info
3537
public void InvalidInterface(Type interfaceType)
3638
{
3739
AssertInvalidInterface(new ParameterInfo(interfaceType));

Tests/Editor/Models/ParameterStructTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public void ValidInterface()
3232
[TestCase(typeof(IInternalPropertyStruct))] // property is defined as non public
3333
[TestCase(typeof(IMethodStruct))] // interface defined a method
3434
[TestCase(typeof(INameSpacedStruct))] // interface has a name space
35+
[TestCase(typeof(IReferenceBaseStruct))] // interface has reference to base struct
36+
[TestCase(typeof(IListReferenceBaseStruct))] // interface has reference to base struct
3537
public void InvalidInterface(Type interfaceType)
3638
{
3739
AssertInvalidInterface(new ParameterStruct(interfaceType));

Tests/Editor/Models/TestCode/TestInfos.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections;
2+
using System.Collections.Generic;
3+
using PocketGems.Parameters;
24
using PocketGems.Parameters.Interface;
35

46
// valid interface
@@ -90,3 +92,15 @@ public interface INameSpacedInfo : IBaseInfo
9092
int MyInt { get; }
9193
}
9294
}
95+
96+
// bad: referencing base info
97+
public interface IReferenceBaseInfo : IBaseInfo
98+
{
99+
ParameterReference<IBaseInfo> MyInfo { get; }
100+
}
101+
102+
// bad: referencing base info
103+
public interface IListReferenceBaseInfo : IBaseInfo
104+
{
105+
IReadOnlyList<ParameterReference<IBaseInfo>> MyInfos { get; }
106+
}

0 commit comments

Comments
 (0)