Skip to content

Commit

Permalink
Reserved words are escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Nov 7, 2023
1 parent 13f6186 commit f61bb31
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace Thinktecture.ValueObjects;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Thinktecture.CodeAnalysis;

public readonly record struct ArgumentName(string Raw, string Escaped);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public sealed class DefaultMemberState : IMemberState, IEquatable<DefaultMemberS
private readonly ITypedMemberState _typedMemberState;

public string Name { get; }
public string ArgumentName { get; }
public ArgumentName ArgumentName { get; }

public SpecialType SpecialType => _typedMemberState.SpecialType;
public string TypeFullyQualified => _typedMemberState.TypeFullyQualified;
Expand All @@ -19,7 +19,7 @@ public sealed class DefaultMemberState : IMemberState, IEquatable<DefaultMemberS
public bool IsParsable => _typedMemberState.IsParsable;
public ImplementedComparisonOperators ComparisonOperators => _typedMemberState.ComparisonOperators;

public DefaultMemberState(ITypedMemberState typedMemberState, string name, string argumentName)
public DefaultMemberState(ITypedMemberState typedMemberState, string name, ArgumentName argumentName)
{
_typedMemberState = typedMemberState;
Name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Thinktecture.CodeAnalysis;

public interface IMemberState : IEquatable<IMemberState>, IMemberInformation
{
string ArgumentName { get; }
ArgumentName ArgumentName { get; }
string TypeFullyQualifiedNullAnnotated { get; }
string TypeFullyQualifiedWithNullability { get; }
bool IsFormattable { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public sealed class InstanceMemberInfo : IMemberState, IEquatable<InstanceMember
private readonly ITypedMemberState _typedMemberState;
private readonly (IFieldSymbol? Field, IPropertySymbol? Property) _symbol;

private string? _argumentName;
public string ArgumentName => _argumentName ??= Name.MakeArgumentName();
private ArgumentName? _argumentName;
public ArgumentName ArgumentName => _argumentName ??= Name.MakeArgumentName();

public string Name { get; }
public bool IsStatic { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public sealed class EnumSourceGeneratorState : ITypeInformation, IEquatable<Enum
public bool IsReferenceType { get; }
public bool IsAbstract { get; }

private string? _argumentName;
public string ArgumentName => _argumentName ??= Name.MakeArgumentName();
private ArgumentName? _argumentName;
public ArgumentName ArgumentName => _argumentName ??= Name.MakeArgumentName();

public IReadOnlyList<string> ItemNames { get; }
public IReadOnlyList<InstanceMemberInfo> AssignableInstanceFieldsAndProperties { get; }
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public sealed class ValueObjectJsonConverter : global::System.Text.Json.Serializ
if (needsConverter)
{
_sb.Append(@"
private readonly global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("> _").Append(memberInfo.ArgumentName).Append("Converter;");
private readonly global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("> _").Append(memberInfo.ArgumentName.Raw).Append("Converter;");
}

_sb.Append(@"
private readonly string _").Append(memberInfo.ArgumentName).Append("PropertyName;");
private readonly string _").Append(memberInfo.ArgumentName.Raw).Append("PropertyName;");
}

_sb.Append(@"
Expand All @@ -76,11 +76,11 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o
if (needsConverter)
{
_sb.Append(@"
this._").Append(memberInfo.ArgumentName).Append("Converter = (global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(">)options.GetConverter(typeof(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("));");
this._").Append(memberInfo.ArgumentName.Raw).Append("Converter = (global::System.Text.Json.Serialization.JsonConverter<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(">)options.GetConverter(typeof(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("));");
}

_sb.Append(@"
this._").Append(memberInfo.ArgumentName).Append("PropertyName = namingPolicy?.ConvertName(\"").Append(memberInfo.Name).Append(@""") ?? """).Append(memberInfo.Name).Append(@""";");
this._").Append(memberInfo.ArgumentName.Raw).Append("PropertyName = namingPolicy?.ConvertName(\"").Append(memberInfo.Name).Append(@""") ?? """).Append(memberInfo.Name).Append(@""";");
}

_sb.Append(@"
Expand All @@ -103,7 +103,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
").Append(memberInfo.TypeFullyQualifiedNullAnnotated).Append(" ").Append(memberInfo.ArgumentName).Append(" = default;");
").Append(memberInfo.TypeFullyQualifiedNullAnnotated).Append(" ").Append(memberInfo.ArgumentName.Escaped).Append(" = default;");
}

_sb.Append(@"
Expand Down Expand Up @@ -141,9 +141,9 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o
else if ");
}

_sb.Append(@"(comparer.Equals(propName, this._").Append(memberInfo.ArgumentName).Append(@"PropertyName))
_sb.Append("(comparer.Equals(propName, this._").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyName))
{
").Append(memberInfo.ArgumentName).Append(" = ");
").Append(memberInfo.ArgumentName.Escaped).Append(" = ");

GenerateReadValue(_sb, memberInfo);

Expand Down Expand Up @@ -172,7 +172,7 @@ public ValueObjectJsonConverter(global::System.Text.Json.JsonSerializerOptions o
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
").Append(memberInfo.ArgumentName).Append("!,");
").Append(memberInfo.ArgumentName.Escaped).Append("!,");
}

_sb.Append(@"
Expand Down Expand Up @@ -200,25 +200,25 @@ public override void Write(global::System.Text.Json.Utf8JsonWriter writer, ").Ap
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
var ").Append(memberInfo.ArgumentName).Append(@"PropertyValue = value.").Append(memberInfo.Name).Append(@";
var ").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue = value.").Append(memberInfo.Name).Append(@";
");

if (memberInfo.IsReferenceTypeOrNullableStruct)
{
_sb.Append(@"
if(!ignoreNullValues || ").Append(memberInfo.ArgumentName).Append(@"PropertyValue is not null)
if(!ignoreNullValues || ").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyValue is not null)
{
");
}
else
{
_sb.Append(@"
if(!ignoreDefaultValues || !").Append(memberInfo.ArgumentName).Append("PropertyValue.Equals(default(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(@")))
if(!ignoreDefaultValues || !").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue.Equals(default(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(@")))
{
");
}

_sb.Append("writer.WritePropertyName(this._").Append(memberInfo.ArgumentName).Append(@"PropertyName);
_sb.Append("writer.WritePropertyName(this._").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyName);
");

_sb.Append(" ");
Expand Down Expand Up @@ -292,13 +292,13 @@ private static bool GenerateWriteValue(StringBuilder? sb, InstanceMemberInfo mem
}
else
{
sb?.Append("this._").Append(memberInfo.ArgumentName).Append("Converter.Write(writer, ").Append(memberInfo.ArgumentName).Append("PropertyValue, options);");
sb?.Append("this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Write(writer, ").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue, options);");

return true;
}
}

sb?.Append("writer.").Append(command).Append("(").Append(memberInfo.ArgumentName).Append("PropertyValue);");
sb?.Append("writer.").Append(command).Append("(").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue);");

return false;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ private static void GenerateReadValue(StringBuilder sb, InstanceMemberInfo membe
}
else
{
sb.Append("this._").Append(memberInfo.ArgumentName).Append("Converter.Read(ref reader, typeof(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("), options)");
sb.Append("this._").Append(memberInfo.ArgumentName.Raw).Append("Converter.Read(ref reader, typeof(").Append(memberInfo.TypeFullyQualifiedWithNullability).Append("), options)");
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public sealed class ValueObjectMessagePackFormatter : global::MessagePack.Format
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
var ").Append(memberInfo.ArgumentName).Append(" = ");
var ").Append(memberInfo.ArgumentName.Escaped).Append(" = ");

GenerateReadValue(_sb, memberInfo);

Expand All @@ -81,7 +81,7 @@ public sealed class ValueObjectMessagePackFormatter : global::MessagePack.Format
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
").Append(memberInfo.ArgumentName).Append(",");
").Append(memberInfo.ArgumentName.Escaped).Append(",");
}

_sb.Append(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override bool CanConvert(global::System.Type objectType)
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
").Append(memberInfo.TypeFullyQualifiedNullAnnotated).Append(" ").Append(memberInfo.ArgumentName).Append(" = default;");
").Append(memberInfo.TypeFullyQualifiedNullAnnotated).Append(" ").Append(memberInfo.ArgumentName.Escaped).Append(" = default;");
}

_sb.Append(@"
Expand Down Expand Up @@ -138,9 +138,9 @@ public override bool CanConvert(global::System.Type objectType)
else if ");
}

_sb.Append(@"(comparer.Equals(propName, """).Append(memberInfo.ArgumentName).Append(@"""))
_sb.Append(@"(comparer.Equals(propName, """).Append(memberInfo.ArgumentName.Escaped).Append(@"""))
{
").Append(memberInfo.ArgumentName).Append(" = serializer.Deserialize<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(@">(reader);
").Append(memberInfo.ArgumentName.Escaped).Append(" = serializer.Deserialize<").Append(memberInfo.TypeFullyQualifiedWithNullability).Append(@">(reader);
}");
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public override bool CanConvert(global::System.Type objectType)
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
").Append(memberInfo.ArgumentName).Append("!,");
").Append(memberInfo.ArgumentName.Escaped).Append("!,");
}

_sb.Append(@"
Expand Down Expand Up @@ -214,13 +214,13 @@ public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, object
var memberInfo = _assignableInstanceFieldsAndProperties[i];

_sb.Append(@"
var ").Append(memberInfo.ArgumentName).Append(@"PropertyValue = obj.").Append(memberInfo.Name).Append(@";
var ").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyValue = obj.").Append(memberInfo.Name).Append(@";
");

if (memberInfo.IsReferenceTypeOrNullableStruct)
{
_sb.Append(@"
if(serializer.NullValueHandling != global::Newtonsoft.Json.NullValueHandling.Ignore || ").Append(memberInfo.ArgumentName).Append(@"PropertyValue is not null)
if(serializer.NullValueHandling != global::Newtonsoft.Json.NullValueHandling.Ignore || ").Append(memberInfo.ArgumentName.Raw).Append(@"PropertyValue is not null)
{
");
}
Expand Down Expand Up @@ -300,11 +300,11 @@ private static void GenerateWriteValue(StringBuilder? sb, InstanceMemberInfo mem
break;

default:
sb?.Append("serializer.Serialize(writer, ").Append(memberInfo.ArgumentName).Append("PropertyValue);");
sb?.Append("serializer.Serialize(writer, ").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue);");
return;
}
}

sb?.Append("writer.").Append(command).Append("(").Append(memberInfo.ArgumentName).Append("PropertyValue);");
sb?.Append("writer.").Append(command).Append("(").Append(memberInfo.ArgumentName.Raw).Append("PropertyValue);");
}
}
Loading

0 comments on commit f61bb31

Please sign in to comment.