Skip to content

Commit

Permalink
fix: use static closures
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Aug 23, 2024
1 parent 2a92fc6 commit ddd191c
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((BaseClass)obj).Name,
Getter: static (object obj) => ((BaseClass)obj).Name,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
IsInit: false,
IsRequired: false,
Getter: null,
Setter: (object obj, object? value) => ((SetOnlyProperty)obj).Value = (string)value!,
Setter: static (object obj, object? value) => ((SetOnlyProperty)obj).Value = (string)value!,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((BaseModel)obj).Name,
Setter: (object obj, object? value) => ((BaseModel)obj).Name = (string)value!,
Getter: static (object obj) => ((BaseModel)obj).Name,
Setter: static (object obj, object? value) => ((BaseModel)obj).Name = (string)value!,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Age",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyModel)obj).Age,
Setter: (object obj, object? value) => ((MyModel)obj).Age = (int)value,
Getter: static (object obj) => ((MyModel)obj).Age,
Setter: static (object obj, object? value) => ((MyModel)obj).Age = (int)value,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand All @@ -44,8 +44,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyModel)obj).Name,
Setter: (object obj, object? value) => ((MyModel)obj).Name = (string)value!,
Getter: static (object obj) => ((MyModel)obj).Name,
Setter: static (object obj, object? value) => ((MyModel)obj).Name = (string)value!,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand Down Expand Up @@ -79,8 +79,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public System.Collections.Generic.IReadOnlyDictionary<System.Type, System.Action<object>> MixinHandlers { get; } = new System.Collections.Generic.Dictionary<System.Type, System.Action<object>>() {
[typeof(IMyMixin)] = (obj) => ((IMyMixin)obj).Handler(),
[typeof(IMySecondMixin)] = (obj) => ((IMySecondMixin)obj).Handler()
[typeof(IMyMixin)] = static (obj) => ((IMyMixin)obj).Handler(),
[typeof(IMySecondMixin)] = static (obj) => ((IMySecondMixin)obj).Handler()
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public System.Collections.Generic.IReadOnlyDictionary<System.Type, System.Action<object>> MixinHandlers { get; } = new System.Collections.Generic.Dictionary<System.Type, System.Action<object>>() {
[typeof(ISomeMixin)] = (obj) => ((ISomeMixin)obj).Handler()
[typeof(ISomeMixin)] = static (obj) => ((ISomeMixin)obj).Handler()
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "DeepName",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((DeeplyNestedBaseClass)obj).DeepName,
Getter: static (object obj) => ((DeeplyNestedBaseClass)obj).DeepName,
Setter: null,
GenericType: new GenericType(
OpenType: 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 {
Name: "Identifier",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((SomeBaseClass)obj).Identifier,
Getter: static (object obj) => ((SomeBaseClass)obj).Identifier,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "MyProperty",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyType)obj).MyProperty,
Setter: (object obj, object? value) => ((MyType)obj).MyProperty = (string)value!,
Getter: static (object obj) => ((MyType)obj).MyProperty,
Setter: static (object obj, object? value) => ((MyType)obj).MyProperty = (string)value!,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand All @@ -51,7 +51,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "NoAttributeSoNoMetadata",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyType)obj).NoAttributeSoNoMetadata,
Getter: static (object obj) => ((MyType)obj).NoAttributeSoNoMetadata,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
Expand All @@ -67,8 +67,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "OptionalFloat",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyType)obj).OptionalFloat,
Setter: (object obj, object? value) => ((MyType)obj).OptionalFloat = (Nullable<float>)value,
Getter: static (object obj) => ((MyType)obj).OptionalFloat,
Setter: static (object obj, object? value) => ((MyType)obj).OptionalFloat = (Nullable<float>)value,
GenericType: new GenericType(
OpenType: typeof(Nullable<>),
ClosedType: typeof(Nullable<float>),
Expand All @@ -94,8 +94,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "OptionalInt",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((MyType)obj).OptionalInt,
Setter: (object obj, object? value) => ((MyType)obj).OptionalInt = (int)value,
Getter: static (object obj) => ((MyType)obj).OptionalInt,
Setter: static (object obj, object? value) => ((MyType)obj).OptionalInt = (int)value,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((AttributesWithNamedArgs)obj).Name,
Getter: static (object obj) => ((AttributesWithNamedArgs)obj).Name,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Address",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((InitArgsModel)obj).Address,
Setter: (object obj, object? value) => ((InitArgsModel)obj).Address = (string)value,
Getter: static (object obj) => ((InitArgsModel)obj).Address,
Setter: static (object obj, object? value) => ((InitArgsModel)obj).Address = (string)value,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand All @@ -43,7 +43,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Age",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((InitArgsModel)obj).Age,
Getter: static (object obj) => ((InitArgsModel)obj).Age,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
Expand All @@ -62,7 +62,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Description",
IsInit: true,
IsRequired: false,
Getter: (object obj) => ((InitArgsModel)obj).Description,
Getter: static (object obj) => ((InitArgsModel)obj).Description,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand All @@ -81,7 +81,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "HasAttended",
IsInit: true,
IsRequired: false,
Getter: (object obj) => ((InitArgsModel)obj).HasAttended,
Getter: static (object obj) => ((InitArgsModel)obj).HasAttended,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(InitArgsEnum),
Expand All @@ -100,8 +100,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "IsAttending",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((InitArgsModel)obj).IsAttending,
Setter: (object obj, object? value) => ((InitArgsModel)obj).IsAttending = (InitArgsEnum)value!,
Getter: static (object obj) => ((InitArgsModel)obj).IsAttending,
Setter: static (object obj, object? value) => ((InitArgsModel)obj).IsAttending = (InitArgsEnum)value!,
GenericType: new GenericType(
OpenType: typeof(InitArgsEnum),
ClosedType: typeof(InitArgsEnum),
Expand All @@ -119,7 +119,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((InitArgsModel)obj).Name,
Getter: static (object obj) => ((InitArgsModel)obj).Name,
Setter: null,
GenericType: new GenericType(
OpenType: 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 {
Name: "Value",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((PropertyModel)obj).Value,
Getter: static (object obj) => ((PropertyModel)obj).Value,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "NestedCollections",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((Collections)obj).NestedCollections,
Getter: static (object obj) => ((Collections)obj).NestedCollections,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(Dictionary<,>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Age",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((DerivedModel)obj).Age,
Setter: (object obj, object? value) => ((DerivedModel)obj).Age = (int)value!,
Getter: static (object obj) => ((DerivedModel)obj).Age,
Setter: static (object obj, object? value) => ((DerivedModel)obj).Age = (int)value!,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Age",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((PartialModel)obj).Age,
Getter: static (object obj) => ((PartialModel)obj).Age,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(int),
Expand All @@ -43,7 +43,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: true,
IsRequired: true,
Getter: (object obj) => ((PartialModel)obj).Name,
Getter: static (object obj) => ((PartialModel)obj).Name,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "MyProperty",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((NestedType)obj).MyProperty,
Setter: (object obj, object? value) => ((NestedType)obj).MyProperty = (string)value!,
Getter: static (object obj) => ((NestedType)obj).MyProperty,
Setter: static (object obj, object? value) => ((NestedType)obj).MyProperty = (string)value!,
GenericType: new GenericType(
OpenType: typeof(string),
ClosedType: typeof(string),
Expand All @@ -55,8 +55,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "OptionalFloat",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((NestedType)obj).OptionalFloat,
Setter: (object obj, object? value) => ((NestedType)obj).OptionalFloat = (float)value,
Getter: static (object obj) => ((NestedType)obj).OptionalFloat,
Setter: static (object obj, object? value) => ((NestedType)obj).OptionalFloat = (float)value,
GenericType: new GenericType(
OpenType: typeof(float),
ClosedType: typeof(float),
Expand All @@ -74,8 +74,8 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "OptionalInt",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((NestedType)obj).OptionalInt,
Setter: (object obj, object? value) => ((NestedType)obj).OptionalInt = (int)value,
Getter: static (object obj) => ((NestedType)obj).OptionalInt,
Setter: static (object obj, object? value) => ((NestedType)obj).OptionalInt = (int)value,
GenericType: new GenericType(
OpenType: typeof(int),
ClosedType: typeof(int),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public void Write(IndentedTextWriter writer, string typeSimpleNameClosed) {
var propertyValue = "value" + (IsNullable ? "" : "!");

var getter = HasGetter
? $"(object obj) => (({typeSimpleNameClosed})obj).{Name}"
? $"static (object obj) => (({typeSimpleNameClosed})obj).{Name}"
: "null";

var setter = HasSetter
? (
IsInit
? "null"
: $"(object obj, object? value) => (({typeSimpleNameClosed})obj)" +
$".{Name} = ({GenericType.ClosedType}){propertyValue}"
: $"static (object obj, object? value) => " +
$"(({typeSimpleNameClosed})obj)" +
$".{Name} = ({GenericType.ClosedType}){propertyValue}"
)
: "null";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ bool knownToBeAccessibleFromGlobalScope
) {
const string prefix = "Chickensoft.Introspection";
var name = $"\"{Reference.SimpleNameClosed}\"";
var genericTypeGetter = $"(r) => r.Receive<{FullNameClosed}>()";
var factory = $"() => System.Activator.CreateInstance<{FullNameClosed}>()";
var genericTypeGetter = $"static (r) => r.Receive<{FullNameClosed}>()";
var factory =
$"static () => System.Activator.CreateInstance<{FullNameClosed}>()";
var metatype = $"new {FullNameClosed}.{Constants.METATYPE_IMPL}()";
var id = Id ?? "";
var version = $"{Version}";
Expand Down Expand Up @@ -481,7 +482,7 @@ IEnumerable<DeclaredType> baseTypes
writer.WriteCommaSeparatedList(
orderedMixins,
(mixin) => writer.Write(
$"[typeof({mixin})] = (obj) => (({mixin})obj).Handler()"
$"[typeof({mixin})] = static (obj) => (({mixin})obj).Handler()"
),
multiline: true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MetatypeMetadata : Chickensoft.Introspection.IMetatype {
Name: "Name",
IsInit: false,
IsRequired: false,
Getter: (object obj) => ((Model)obj).Name,
Getter: static (object obj) => ((Model)obj).Name,
Setter: null,
GenericType: new GenericType(
OpenType: typeof(string),
Expand Down
Loading

0 comments on commit ddd191c

Please sign in to comment.