Skip to content

Commit

Permalink
csharpier format
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Aug 28, 2024
1 parent 7c8cdd1 commit 4be4318
Show file tree
Hide file tree
Showing 47 changed files with 282 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public enum PropertyNameMappingStrategy
/// <summary>
/// Matches a property by its name in case insensitive manner.
/// </summary>
CaseInsensitive
CaseInsensitive,
}
2 changes: 1 addition & 1 deletion src/Riok.Mapperly.Abstractions/RequiredMappingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public enum RequiredMappingStrategy
/// <summary>
/// Warnings are emitted for unmapped target members but not for source members.
/// </summary>
Target = 1 << 1
Target = 1 << 1,
}
13 changes: 7 additions & 6 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ IReadOnlyList<AttributeArgumentSyntax> argumentSyntax
{
return arg.Kind switch
{
_ when (targetType == typeof(AttributeValue?) || targetType == typeof(AttributeValue)) && syntax != null
=> new AttributeValue(arg, syntax.Expression),
_ when (targetType == typeof(AttributeValue?) || targetType == typeof(AttributeValue)) && syntax != null => new AttributeValue(
arg,
syntax.Expression
),
_ when arg.IsNull => null,
_ when targetType == typeof(StringMemberPath) => CreateMemberPath(arg, syntax),
TypedConstantKind.Enum => GetEnumValue(arg, targetType),
TypedConstantKind.Array => BuildArrayValue(arg, targetType),
TypedConstantKind.Primitive => arg.Value,
TypedConstantKind.Type when targetType == typeof(ITypeSymbol) => arg.Value,
_
=> throw new ArgumentOutOfRangeException(
$"{nameof(AttributeDataAccessor)} does not support constructor arguments of kind {arg.Kind.ToString()} or cannot convert it to {targetType}"
),
_ => throw new ArgumentOutOfRangeException(
$"{nameof(AttributeDataAccessor)} does not support constructor arguments of kind {arg.Kind.ToString()} or cannot convert it to {targetType}"
),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public IEnumerable<string> GetMembersWithExplicitConfigurations(MappingSourceTar
var members = sourceTarget switch
{
MappingSourceTarget.Source => ExplicitMappings.Where(x => x.Source.Path.Count > 0).Select(x => x.Source.Path[0]),
MappingSourceTarget.Target
=> ExplicitMappings.Select(x => x.Target.Path[0]).Concat(ValueMappings.Select(x => x.Target.Path[0])),
MappingSourceTarget.Target => ExplicitMappings
.Select(x => x.Target.Path[0])
.Concat(ValueMappings.Select(x => x.Target.Path[0])),
_ => throw new ArgumentOutOfRangeException(nameof(sourceTarget), sourceTarget, "Neither source or target"),
};
return members.Distinct();
Expand Down
185 changes: 86 additions & 99 deletions src/Riok.Mapperly/Descriptors/Enumerables/CollectionInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,128 +267,115 @@ private static CollectionType GetImplementedCollectionTypes(WellKnownTypes types
// when collectionType is None then manually check for implemented types with IterateImplementedTypes
return collectionType switch
{
CollectionType.Array
=> CollectionType.Array
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.Array => CollectionType.Array
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IEnumerable => CollectionType.IEnumerable,
CollectionType.List
=> CollectionType.List
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.List => CollectionType.List
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.Stack => CollectionType.Stack | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.Queue => CollectionType.Queue | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.IReadOnlyCollection => CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.IList => CollectionType.IList | CollectionType.ICollection | CollectionType.IEnumerable,
CollectionType.IReadOnlyList => CollectionType.IReadOnlyList | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.ICollection => CollectionType.ICollection | CollectionType.IEnumerable,
CollectionType.HashSet
=> CollectionType.HashSet
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.SortedSet
=> CollectionType.SortedSet
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.HashSet => CollectionType.HashSet
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.SortedSet => CollectionType.SortedSet
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ISet => CollectionType.ISet | CollectionType.ICollection | CollectionType.IEnumerable,
CollectionType.IReadOnlySet => CollectionType.IReadOnlySet | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.IDictionary => CollectionType.IDictionary | CollectionType.ICollection | CollectionType.IEnumerable,
CollectionType.IReadOnlyDictionary
=> CollectionType.IReadOnlyDictionary | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.Dictionary
=> CollectionType.Dictionary
| CollectionType.IDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,

CollectionType.ImmutableArray
=> CollectionType.ImmutableArray
| CollectionType.IImmutableList
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableList
=> CollectionType.ImmutableList
| CollectionType.IImmutableList
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IImmutableList
=> CollectionType.IImmutableList
| CollectionType.IReadOnlyList
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableHashSet
=> CollectionType.ImmutableHashSet
| CollectionType.IImmutableSet
| CollectionType.IReadOnlySet
| CollectionType.ISet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IReadOnlyDictionary => CollectionType.IReadOnlyDictionary
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.Dictionary => CollectionType.Dictionary
| CollectionType.IDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,

CollectionType.ImmutableArray => CollectionType.ImmutableArray
| CollectionType.IImmutableList
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableList => CollectionType.ImmutableList
| CollectionType.IImmutableList
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IImmutableList => CollectionType.IImmutableList
| CollectionType.IReadOnlyList
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableHashSet => CollectionType.ImmutableHashSet
| CollectionType.IImmutableSet
| CollectionType.IReadOnlySet
| CollectionType.ISet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IImmutableSet => CollectionType.IImmutableSet | CollectionType.IReadOnlyCollection | CollectionType.IEnumerable,
CollectionType.ImmutableSortedSet
=> CollectionType.ImmutableSortedSet
| CollectionType.IImmutableSet
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableSortedSet => CollectionType.ImmutableSortedSet
| CollectionType.IImmutableSet
| CollectionType.IList
| CollectionType.IReadOnlyList
| CollectionType.ISet
| CollectionType.IReadOnlySet
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableQueue => CollectionType.ImmutableQueue | CollectionType.IImmutableQueue | CollectionType.IEnumerable,
CollectionType.IImmutableQueue => CollectionType.IImmutableQueue | CollectionType.IEnumerable,
CollectionType.ImmutableStack => CollectionType.ImmutableStack | CollectionType.IImmutableStack | CollectionType.IEnumerable,
CollectionType.IImmutableStack => CollectionType.IImmutableStack | CollectionType.IEnumerable,
CollectionType.ImmutableDictionary
=> CollectionType.ImmutableDictionary
| CollectionType.IImmutableDictionary
| CollectionType.IDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IImmutableDictionary
=> CollectionType.IImmutableDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableSortedDictionary
=> CollectionType.ImmutableSortedDictionary
| CollectionType.IImmutableDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableDictionary => CollectionType.ImmutableDictionary
| CollectionType.IImmutableDictionary
| CollectionType.IDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.ICollection
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.IImmutableDictionary => CollectionType.IImmutableDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.ImmutableSortedDictionary => CollectionType.ImmutableSortedDictionary
| CollectionType.IImmutableDictionary
| CollectionType.IReadOnlyDictionary
| CollectionType.IReadOnlyCollection
| CollectionType.IEnumerable,
CollectionType.Span => CollectionType.Span,
CollectionType.ReadOnlySpan => CollectionType.ReadOnlySpan,
CollectionType.Memory => CollectionType.Memory,
CollectionType.ReadOnlyMemory => CollectionType.ReadOnlyMemory,

// check for if the type is a string, returning IEnumerable
CollectionType.None when type.SpecialType == SpecialType.System_String
=> CollectionType.IEnumerable,
CollectionType.None when type.SpecialType == SpecialType.System_String => CollectionType.IEnumerable,

// fallback for CollectionType.None, manually checking for known implemented types
_ => IterateImplementedTypes(type, types)
_ => IterateImplementedTypes(type, types),
};

static CollectionType IterateImplementedTypes(ITypeSymbol type, WellKnownTypes types)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,16 @@ private INewInstanceMapping TryInlineMapping(INewInstanceMapping mapping)
return mapping switch
{
// inline existing mapping
UserImplementedMethodMapping implementedMapping
=> InlineExpressionMappingBuilder.TryBuildMapping(this, implementedMapping) ?? implementedMapping,
UserImplementedMethodMapping implementedMapping => InlineExpressionMappingBuilder.TryBuildMapping(this, implementedMapping)
?? implementedMapping,

// build an inlined version
IUserMapping userMapping
=> BuildMapping(
userMapping,
new TypeMappingKey(userMapping),
MappingBuildingOptions.Default,
userMapping.Method.GetSyntaxLocation()
) ?? mapping,
IUserMapping userMapping => BuildMapping(
userMapping,
new TypeMappingKey(userMapping),
MappingBuildingOptions.Default,
userMapping.Method.GetSyntaxLocation()
) ?? mapping,

_ => mapping,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static class MemberMappingBuilder
public enum CodeStyle
{
Expression,
Statement
Statement,
}

public static bool TryBuildContainerAssignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,20 @@ INewInstanceMapping valueMapping
{
return ctx.CollectionInfos!.Target.CollectionType switch
{
CollectionType.ImmutableSortedDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableSortedDictionaryMethodName, keyMapping, valueMapping),
CollectionType.ImmutableDictionary
or CollectionType.IImmutableDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableDictionaryMethodName, keyMapping, valueMapping),
CollectionType.ImmutableSortedDictionary => new LinqDictionaryMapping(
ctx.Source,
ctx.Target,
ToImmutableSortedDictionaryMethodName,
keyMapping,
valueMapping
),
CollectionType.ImmutableDictionary or CollectionType.IImmutableDictionary => new LinqDictionaryMapping(
ctx.Source,
ctx.Target,
ToImmutableDictionaryMethodName,
keyMapping,
valueMapping
),

_ => null,
};
Expand All @@ -236,11 +245,10 @@ private static CollectionInfo BuildCollectionTypeForSourceIDictionary(MappingBui
if (ctx.HasUserSymbol)
return info;

CollectionType? dictionaryType = info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyDictionary)
? CollectionType.IReadOnlyDictionary
: info.ImplementedTypes.HasFlag(CollectionType.IDictionary)
? CollectionType.IDictionary
: null;
CollectionType? dictionaryType =
info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyDictionary) ? CollectionType.IReadOnlyDictionary
: info.ImplementedTypes.HasFlag(CollectionType.IDictionary) ? CollectionType.IDictionary
: null;

return dictionaryType == null
? info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static class EnumMappingBuilder
return ctx.Configuration.Enum.Strategy switch
{
EnumMappingStrategy.ByName when ctx.IsExpression => BuildCastMappingAndDiagnostic(ctx),
EnumMappingStrategy.ByValue when ctx is { IsExpression: true, Configuration.Enum.HasExplicitConfigurations: true }
=> BuildCastMappingAndDiagnostic(ctx),
EnumMappingStrategy.ByValue when ctx is { IsExpression: true, Configuration.Enum.HasExplicitConfigurations: true } =>
BuildCastMappingAndDiagnostic(ctx),
EnumMappingStrategy.ByValueCheckDefined when ctx.IsExpression => BuildCastMappingAndDiagnostic(ctx),
EnumMappingStrategy.ByName => BuildNameMapping(ctx),
EnumMappingStrategy.ByValueCheckDefined => BuildEnumToEnumCastMapping(ctx, checkTargetDefined: true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ or CollectionType.ICollection
CollectionType.ImmutableQueue or CollectionType.IImmutableQueue => CreateRangeQueueMethodName,
CollectionType.ImmutableStack or CollectionType.IImmutableStack => CreateRangeStackMethodName,
CollectionType.ImmutableSortedSet => ToImmutableSortedSetMethodName,
_ => null
_ => null,
};
}

Expand All @@ -388,11 +388,10 @@ private static CollectionInfo BuildCollectionTypeForICollection(MappingBuilderCo
if (ctx.HasUserSymbol)
return info;

CollectionType? collectionType = info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyCollection)
? CollectionType.IReadOnlyCollection
: info.ImplementedTypes.HasFlag(CollectionType.ICollection)
? CollectionType.ICollection
: null;
CollectionType? collectionType =
info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyCollection) ? CollectionType.IReadOnlyCollection
: info.ImplementedTypes.HasFlag(CollectionType.ICollection) ? CollectionType.ICollection
: null;

return collectionType == null ? info : CollectionInfoBuilder.BuildGenericCollectionInfo(ctx, collectionType.Value, info);
}
Expand Down
Loading

0 comments on commit 4be4318

Please sign in to comment.