Skip to content

Commit

Permalink
Merge pull request #1 from chickensoft-games/fix/invalid-setter
Browse files Browse the repository at this point in the history
fix: use null setter if not available
  • Loading branch information
jolexxa authored Jun 4, 2024
2 parents c97292d + 7ef6001 commit 00219ae
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
run: dotnet restore

- name: 🦺 Build Projects
run: dotnet build
run: dotnet build-server shutdown; dotnet build --no-incremental

- name: 🧪 Run Tests
run: dotnet test
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((BaseClass)obj).Name,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Name on BaseClass is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((DeeplyNestedBaseClass)obj).DeepName,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property DeepName on DeeplyNestedBaseClass is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((SomeBaseClass)obj).Identifier,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Identifier on SomeBaseClass is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyType)obj).NoAttributeSoNoMetadata,
Setter: (object obj, object? _) => { },
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((AttributesWithNamedArgs)obj).Name,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Name on AttributesWithNamedArgs is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((InitArgsModel)obj).Age,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Age on InitArgsModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand All @@ -58,7 +58,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: false,
Getter: (object obj) => ((InitArgsModel)obj).Description,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Description on InitArgsModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand All @@ -77,7 +77,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((InitArgsModel)obj).Name,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Name on InitArgsModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((PropertyModel)obj).Value,
Setter: (object obj, object? _) => { },
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((Collections)obj).NestedCollections,
Setter: (object obj, object? _) => { },
Setter: null,
GenericType: new GenericType(
OpenType: typeof(Dictionary<,>),
ClosedType: typeof(Dictionary<List<string>, List<List<int>>>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((PartialModel)obj).Age,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Age on PartialModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand All @@ -39,7 +39,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((PartialModel)obj).Name,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Name on PartialModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Equality() {
var registry = new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
Expand All @@ -26,7 +26,7 @@ public void Equality() {
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
Expand All @@ -36,15 +36,15 @@ public void Equality() {
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
).ShouldNotBe(
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: new DeclaredType[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Chickensoft.Introspection.Generator.Tests.Models;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Chickensoft.Collections;
using Chickensoft.Introspection.Generator.Models;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -146,7 +146,12 @@ public class ScopeTreeTest {
public void AddsDeclaredTypes() {
// Use a map to guarantee types are added in the order shown below.
var tree = new ScopeTree(
new Map<string, DeclaredType>() {
[
_inner,
_outer,
_genericOuter
],
new Dictionary<string, DeclaredType>() {
[_inner.FullNameOpen] = _inner,
[_outer.FullNameOpen] = _outer,
[_genericOuter.FullNameOpen] = _genericOuter
Expand All @@ -173,7 +178,8 @@ public void AddsDeclaredTypes() {
public void ThrowsIfContainingTypeIsNotAbleToBeFound() =>
Should.Throw<InvalidOperationException>(
() => new ScopeTree(
new Map<string, DeclaredType>() {
[_inner, _outer],
new Dictionary<string, DeclaredType>() {
[_inner.FullNameOpen] = _inner,
[_outer.FullNameOpen] = _outer
}
Expand All @@ -183,7 +189,8 @@ public void ThrowsIfContainingTypeIsNotAbleToBeFound() =>
[Fact]
public void FindsTypesInScope() {
var tree = new ScopeTree(
new Map<string, DeclaredType>() {
[_typeInOtherNs, _typeExtendingTypeInOtherNs],
new Dictionary<string, DeclaredType>() {
[_typeInOtherNs.FullNameOpen] = _typeInOtherNs,
[_typeExtendingTypeInOtherNs.FullNameOpen] = _typeExtendingTypeInOtherNs,
}
Expand Down
15 changes: 8 additions & 7 deletions Chickensoft.Introspection.Generator/src/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,22 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
var typesByFullName = declaredTypes
.GroupBy((type) => type.FullNameOpen);
var uniqueTypes = typesByFullName
var uniqueTypeList = typesByFullName
.Select(
// Combine non-unique type entries together.
group => group.Aggregate(
(DeclaredType typeA, DeclaredType typeB) =>
typeA.MergePartialDefinition(typeB)
)
)
.OrderBy(type => type.FullNameOpen) // Sort for deterministic output
.ToDictionary(
g => g.FullNameOpen,
g => g
);
.OrderBy(type => type.FullNameOpen);
var uniqueTypes = uniqueTypeList.ToDictionary(
type => type.FullNameOpen,
type => type
);
var tree = new ScopeTree(uniqueTypes);
var tree = new ScopeTree(uniqueTypeList, uniqueTypes);
var visibleTypeFullNames = tree.GetTypes().Select(t => t.FullNameOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ public void Write(IndentedTextWriter writer, string typeSimpleNameClosed) {
var setter = HasSetter
? (
IsInit
? "(object obj, object? value) => throw new " +
"System.InvalidOperationException(" +
$"\"Property {Name} on {typeSimpleNameClosed} is init-only.\"" +
")"
? "null"
: $"(object obj, object? value) => (({typeSimpleNameClosed})obj)" +
$".{Name} = ({GenericType.ClosedType}){propertyValue}"
)
: "(object obj, object? _) => { }";
: "null";

writer.WriteLine($"Name: \"{Name}\",");
writer.WriteLine($"IsInit: {(IsInit ? "true" : "false")},");
Expand Down
11 changes: 7 additions & 4 deletions Chickensoft.Introspection.Generator/src/models/ScopeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class ScopeTree {
/// <summary>Map of types by their full, open generic names.</summary>
public IDictionary<string, DeclaredType> TypesByFullNameOpen { get; }

public ScopeTree(IDictionary<string, DeclaredType> typesByFullNameOpen) {
public ScopeTree(
IEnumerable<DeclaredType> types,
IDictionary<string, DeclaredType> typesByFullNameOpen
) {
TypesByFullNameOpen = typesByFullNameOpen;
InitializeTree();
InitializeTree(types);
}

private void InitializeTree() {
foreach (var declaredType in TypesByFullNameOpen.Values) {
private void InitializeTree(IEnumerable<DeclaredType> typesByFullNameOpen) {
foreach (var declaredType in typesByFullNameOpen) {
AddType(declaredType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((Model)obj).Name,
Setter: (object obj, object? _) => { },
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: false,
Getter: (object obj) => ((ChildModel)obj).ChildName,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property ChildName on ChildModel is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((Model)obj).Age,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Age on Model is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand All @@ -46,7 +46,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: true,
IsRequired: false,
Getter: (object obj) => ((Model)obj).Name,
Setter: (object obj, object? value) => throw new System.InvalidOperationException("Property Name on Model is init-only."),
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down

0 comments on commit 00219ae

Please sign in to comment.