Skip to content

Commit

Permalink
chore: refactor UnsafeAccessorContext
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 22, 2023
1 parent 51b1721 commit 23bb643
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class UnsafeFieldAccessor : IUnsafeAccessor
private readonly string _result;
private readonly string _memberName;

public UnsafeFieldAccessor(ITypeSymbol target, IFieldSymbol value, string methodName)
public UnsafeFieldAccessor(IFieldSymbol value, string methodName)
{
MethodName = methodName;
_targetType = target.FullyQualifiedIdentifierName();
_targetType = value.ContainingType.FullyQualifiedIdentifierName();
_result = value.Type.FullyQualifiedIdentifierName();
_memberName = value.Name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class UnsafeGetPropertyAccessor : IUnsafeAccessor
private readonly string _sourceType;
private readonly string _memberName;

public UnsafeGetPropertyAccessor(ITypeSymbol source, IPropertySymbol result, string methodName)
public UnsafeGetPropertyAccessor(IPropertySymbol result, string methodName)
{
_sourceType = source.FullyQualifiedIdentifierName();
_sourceType = result.ContainingType.FullyQualifiedIdentifierName();
_result = result.Type.FullyQualifiedIdentifierName();
_memberName = result.Name;
MethodName = methodName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class UnsafeSetPropertyAccessor : IUnsafeAccessor
private readonly string _valueType;
private readonly string _memberName;

public UnsafeSetPropertyAccessor(ITypeSymbol target, IPropertySymbol value, string methodName)
public UnsafeSetPropertyAccessor(IPropertySymbol value, string methodName)
{
MethodName = methodName;
_targetType = target.FullyQualifiedIdentifierName();
_targetType = value.ContainingType.FullyQualifiedIdentifierName();
_valueType = value.Type.FullyQualifiedIdentifierName();
_memberName = value.Name;
}
Expand Down
61 changes: 21 additions & 40 deletions src/Riok.Mapperly/Descriptors/UnsafeAccessorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,33 @@ public UnsafeAccessorContext(UniqueNameBuilder nameBuilder, SymbolAccessor symbo

public IReadOnlyCollection<IUnsafeAccessor> UnsafeAccessors => _unsafeAccessors.Values;

public IUnsafeAccessor BuildGetProperty(ITypeSymbol source, IPropertySymbol propertySymbol)
public IUnsafeAccessor GetOrBuildAccessor(UnsafeAccessorType type, ISymbol symbol)
{
var key = new UnsafeAccessorKey(propertySymbol, UnsafeAccessorType.GetProperty);
var key = new UnsafeAccessorKey(symbol, type);
if (_unsafeAccessors.TryGetValue(key, out var value))
{
return value;
}

var formatted = FormatAccessorName(propertySymbol.Name);
var methodName = GetValidMethodName(source, $"Get{formatted}");
var formatted = FormatAccessorName(symbol.Name);

var getter = new UnsafeGetPropertyAccessor(source, propertySymbol, methodName);
_unsafeAccessors.Add(key, getter);
return getter;
}

public IUnsafeAccessor BuildSetProperty(ITypeSymbol target, IPropertySymbol propertySymbol)
{
var key = new UnsafeAccessorKey(propertySymbol, UnsafeAccessorType.SetProperty);
if (_unsafeAccessors.TryGetValue(key, out var value))
var defaultMethodName = type switch
{
return value;
}

var formatted = FormatAccessorName(propertySymbol.Name);
var methodName = GetValidMethodName(target, $"Set{formatted}");

var setter = new UnsafeSetPropertyAccessor(target, propertySymbol, methodName);
_unsafeAccessors.Add(key, setter);
return setter;
}

public IUnsafeAccessor BuildGetField(ITypeSymbol source, IFieldSymbol fieldSymbol)
{
var key = new UnsafeAccessorKey(fieldSymbol, UnsafeAccessorType.GetField);
if (_unsafeAccessors.TryGetValue(key, out var value))
UnsafeAccessorType.GetField => $"Get{formatted}",
UnsafeAccessorType.GetProperty => $"Get{formatted}",
UnsafeAccessorType.SetProperty => $"Set{formatted}",
_ => throw new Exception($"Unknown {nameof(UnsafeAccessorType)}."),
};
var methodName = GetValidMethodName(symbol.ContainingType, defaultMethodName);

IUnsafeAccessor accessor = type switch
{
return value;
}

var formatted = FormatAccessorName(fieldSymbol.Name);
var methodName = GetValidMethodName(source, $"Get{formatted}");

var setter = new UnsafeFieldAccessor(source, fieldSymbol, methodName);
_unsafeAccessors.Add(key, setter);
return setter;
UnsafeAccessorType.GetField => new UnsafeFieldAccessor((IFieldSymbol)symbol, methodName),
UnsafeAccessorType.GetProperty => new UnsafeGetPropertyAccessor((IPropertySymbol)symbol, methodName),
UnsafeAccessorType.SetProperty => new UnsafeSetPropertyAccessor((IPropertySymbol)symbol, methodName),
_ => throw new Exception($"Unknown {nameof(UnsafeAccessorType)}."),
};

_unsafeAccessors.Add(key, accessor);
return accessor;
}

private string GetValidMethodName(ITypeSymbol symbol, string name)
Expand All @@ -87,7 +68,7 @@ private string FormatAccessorName(string name)
return char.ToUpper(name[0], CultureInfo.InvariantCulture) + name[1..];
}

private enum UnsafeAccessorType
public enum UnsafeAccessorType
{
GetProperty,
SetProperty,
Expand Down
9 changes: 6 additions & 3 deletions src/Riok.Mapperly/Symbols/GetterMemberPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ private static IMappableMember BuildMappableMember(MappingBuilderContext ctx, IM

if (item.MemberSymbol.Kind == SymbolKind.Field)
{
var unsafeAccessor = ctx.UnsafeAccessorContext.BuildGetField(item.MemberSymbol.ContainingType, (IFieldSymbol)item.MemberSymbol);
var unsafeAccessor = ctx.UnsafeAccessorContext.GetOrBuildAccessor(
UnsafeAccessorContext.UnsafeAccessorType.GetField,
(IFieldSymbol)item.MemberSymbol
);

return new MethodAccessorMember(item, unsafeAccessor.MethodName);
}
else
{
var unsafeAccessor = ctx.UnsafeAccessorContext.BuildGetProperty(
item.MemberSymbol.ContainingType,
var unsafeAccessor = ctx.UnsafeAccessorContext.GetOrBuildAccessor(
UnsafeAccessorContext.UnsafeAccessorType.GetProperty,
(IPropertySymbol)item.MemberSymbol
);

Expand Down
8 changes: 4 additions & 4 deletions src/Riok.Mapperly/Symbols/SetterMemberPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ private static (IMappableMember, bool) BuildMemberSetter(MappingBuilderContext c

if (member.MemberSymbol.Kind == SymbolKind.Field)
{
var unsafeAccessor = ctx.UnsafeAccessorContext.BuildGetField(
member.MemberSymbol.ContainingType,
var unsafeAccessor = ctx.UnsafeAccessorContext.GetOrBuildAccessor(
UnsafeAccessorContext.UnsafeAccessorType.GetField,
(IFieldSymbol)member.MemberSymbol
);

return (new MethodAccessorMember(member, unsafeAccessor.MethodName), false);
}
else
{
var unsafeAccessor = ctx.UnsafeAccessorContext.BuildSetProperty(
member.MemberSymbol.ContainingType,
var unsafeAccessor = ctx.UnsafeAccessorContext.GetOrBuildAccessor(
UnsafeAccessorContext.UnsafeAccessorType.SetProperty,
(IPropertySymbol)member.MemberSymbol
);

Expand Down

0 comments on commit 23bb643

Please sign in to comment.