Skip to content

Commit 213756c

Browse files
committed
init only & record type
1 parent 91e1d61 commit 213756c

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

ExpressionDebugger.Tests/DebugInfoInjectorTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ public void TestProperties()
736736
Type = typeof(string),
737737
IsReadOnly = true
738738
});
739+
translator.Properties.Add(new PropertyDefinitions
740+
{
741+
Name = "Prop3",
742+
Type = typeof(string),
743+
IsInitOnly = true
744+
});
739745
var str = translator.ToString();
740746
Assert.AreEqual(@"
741747
namespace ExpressionDebugger.Tests
@@ -744,6 +750,7 @@ public partial class MockClass
744750
{
745751
public string Prop1 { get; set; }
746752
public string Prop2 { get; }
753+
public string Prop3 { get; init; }
747754
748755
public MockClass(string prop2)
749756
{
@@ -753,6 +760,46 @@ public MockClass(string prop2)
753760
}".Trim(), str);
754761
}
755762

763+
764+
[TestMethod]
765+
public void TestRecordType()
766+
{
767+
var translator = new ExpressionTranslator(new TypeDefinitions
768+
{
769+
IsStatic = false,
770+
Namespace = "ExpressionDebugger.Tests",
771+
TypeName = "MockClass",
772+
IsRecordType = true,
773+
});
774+
translator.Properties.Add(new PropertyDefinitions
775+
{
776+
Name = "Prop1",
777+
Type = typeof(string)
778+
});
779+
translator.Properties.Add(new PropertyDefinitions
780+
{
781+
Name = "Prop2",
782+
Type = typeof(string),
783+
IsReadOnly = true
784+
});
785+
translator.Properties.Add(new PropertyDefinitions
786+
{
787+
Name = "Prop3",
788+
Type = typeof(string),
789+
IsInitOnly = true
790+
});
791+
var str = translator.ToString();
792+
Assert.AreEqual(@"
793+
namespace ExpressionDebugger.Tests
794+
{
795+
public partial record MockClass(string prop2)
796+
{
797+
public string Prop1 { get; set; }
798+
public string Prop3 { get; init; }
799+
}
800+
}".Trim(), str);
801+
}
802+
756803
public class Data
757804
{
758805
public string Id { get; set; }

ExpressionTranslator/ExpressionTranslator.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,8 @@ public override string ToString()
18411841
.ToList();
18421842
var properties = _properties?
18431843
.ToDictionary(it => it.Name,
1844-
it => $"{Translate(it.Type)} {it.Name} {{ get; {(it.IsReadOnly ? "" : "set; ")}}}");
1844+
it => $"{Translate(it.Type)} {it.Name} {{ get; {(it.IsReadOnly ? "" : it.IsInitOnly ? "init; " : "set; ")}}}");
1845+
var ctorParams = _properties?.Where(it => it.IsReadOnly).ToList();
18451846

18461847
if (Definitions?.TypeName != null)
18471848
{
@@ -1881,7 +1882,11 @@ public override string ToString()
18811882
isInternal = Definitions.Implements?.Any(it =>
18821883
!it.GetTypeInfo().IsInterface && !it.GetTypeInfo().IsPublic) ?? false;
18831884
WriteModifierNextLine(isInternal ? "internal" : "public");
1884-
Write("partial class ", Definitions.TypeName);
1885+
Write("partial ", Definitions.IsRecordType ? "record " : "class ", Definitions.TypeName);
1886+
if (Definitions.IsRecordType && ctorParams?.Count > 0)
1887+
{
1888+
WriteCtorParams(ctorParams);
1889+
}
18851890
if (implements?.Any() == true)
18861891
{
18871892
Write(" : ", string.Join(", ", implements));
@@ -1901,28 +1906,22 @@ public override string ToString()
19011906
{
19021907
foreach (var property in _properties)
19031908
{
1909+
if (Definitions.IsRecordType && property.IsReadOnly)
1910+
continue;
19041911
var isInternal = property.Type.GetTypeInfo().IsNotPublic;
19051912
WriteModifierNextLine(isInternal ? "internal" : "public");
19061913
Write(properties![property.Name]);
19071914
}
19081915
WriteLine();
19091916

1910-
var parameters = _properties.Where(it => it.IsReadOnly).ToList();
1911-
if (parameters.Count > 0 && !Definitions.IsStatic)
1917+
if (ctorParams?.Count > 0 && !Definitions.IsRecordType)
19121918
{
1913-
var isInternal = parameters.Any(it => it.Type.GetTypeInfo().IsNotPublic);
1919+
var isInternal = ctorParams.Any(it => it.Type.GetTypeInfo().IsNotPublic);
19141920
WriteModifierNextLine(isInternal ? "internal" : "public");
1915-
Write(Definitions.TypeName, "(");
1916-
for (var i = 0; i < parameters.Count; i++)
1917-
{
1918-
var parameter = parameters[i];
1919-
if (i > 0)
1920-
Write(", ");
1921-
Write($"{Translate(parameter.Type)} {char.ToLower(parameter.Name[0]) + parameter.Name.Substring(1)}");
1922-
}
1923-
Write(")");
1921+
Write(Definitions.TypeName);
1922+
WriteCtorParams(ctorParams);
19241923
Indent();
1925-
foreach (var parameter in parameters)
1924+
foreach (var parameter in ctorParams)
19261925
{
19271926
WriteNextLine("this.", parameter.Name, " = ", char.ToLower(parameter.Name[0]).ToString(), parameter.Name.Substring(1), ";");
19281927
}
@@ -1965,6 +1964,19 @@ public override string ToString()
19651964
}
19661965
}
19671966

1967+
private void WriteCtorParams(List<PropertyDefinitions> ctorParams)
1968+
{
1969+
Write("(");
1970+
for (var i = 0; i < ctorParams.Count; i++)
1971+
{
1972+
var parameter = ctorParams[i];
1973+
if (i > 0)
1974+
Write(", ");
1975+
Write($"{Translate(parameter.Type)} {char.ToLower(parameter.Name[0]) + parameter.Name.Substring(1)}");
1976+
}
1977+
Write(")");
1978+
}
1979+
19681980
public enum LambdaType
19691981
{
19701982
PublicMethod,

ExpressionTranslator/PropertyDefinitions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public class PropertyDefinitions
77
public Type Type { get; set; }
88
public string Name { get; set; }
99
public bool IsReadOnly { get; set; }
10+
public bool IsInitOnly { get; set; }
1011
}
1112
}

ExpressionTranslator/TypeDefinitions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public class TypeDefinitions
1111
public bool IsInternal { get; set; }
1212
public IEnumerable<Type>? Implements { get; set; }
1313
public bool PrintFullTypeName { get; set; }
14+
public bool IsRecordType { get; set; }
1415
}
1516
}

0 commit comments

Comments
 (0)