Skip to content

Commit 8c59173

Browse files
Functionality to control how AnonymousObject is written
1 parent 80bb593 commit 8c59173

15 files changed

+84
-58
lines changed

ReadableExpressions/Extensions/PublicTypeExtensions.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public static class PublicTypeExtensions
1515
/// Returns a friendly, readable version of the name of the given <paramref name="type"/>.
1616
/// </summary>
1717
/// <param name="type">The type for which to retrieve a friendly, readable name.</param>
18+
/// <param name="translationSettings"></param>
1819
/// <returns>A friendly, readable version of the name of the given <paramref name="type"/>.</returns>
19-
public static string GetFriendlyName(this Type type)
20+
public static string GetFriendlyName(this Type type, TranslationSettings translationSettings)
2021
{
2122
if (type.FullName == null)
2223
{
@@ -26,7 +27,7 @@ public static string GetFriendlyName(this Type type)
2627

2728
if (type.IsArray)
2829
{
29-
return type.GetElementType().GetFriendlyName() + "[]";
30+
return type.GetElementType().GetFriendlyName(translationSettings) + "[]";
3031
}
3132

3233
if (!type.IsGenericType())
@@ -35,7 +36,7 @@ public static string GetFriendlyName(this Type type)
3536

3637
if (type.IsNested)
3738
{
38-
return type.DeclaringType.GetFriendlyName() + "." + qualifiedTypeName;
39+
return type.DeclaringType.GetFriendlyName(translationSettings) + "." + qualifiedTypeName;
3940
}
4041

4142
return qualifiedTypeName;
@@ -45,16 +46,16 @@ public static string GetFriendlyName(this Type type)
4546

4647
if ((underlyingNullableType = Nullable.GetUnderlyingType(type)) != null)
4748
{
48-
return underlyingNullableType.GetFriendlyName() + "?";
49+
return underlyingNullableType.GetFriendlyName(translationSettings) + "?";
4950
}
5051

51-
return GetGenericTypeName(type);
52+
return GetGenericTypeName(type, translationSettings);
5253
}
5354

54-
private static string GetGenericTypeName(Type genericType)
55+
private static string GetGenericTypeName(Type genericType, TranslationSettings translationSettings)
5556
{
5657
var typeGenericTypeArguments = genericType.GetGenericTypeArguments();
57-
var genericTypeName = GetGenericTypeName(genericType.Name, typeGenericTypeArguments.Length, typeGenericTypeArguments);
58+
var genericTypeName = GetGenericTypeName(genericType.Name, typeGenericTypeArguments.Length, typeGenericTypeArguments, translationSettings);
5859

5960
if (!genericType.IsNested)
6061
{
@@ -102,7 +103,7 @@ private static string GetGenericTypeName(Type genericType)
102103
typeGenericTypeArguments = typeGenericTypeArgumentsSubset;
103104
}
104105

105-
parentTypeName = GetGenericTypeName(parentTypeName, numberOfParameters, typeArguments);
106+
parentTypeName = GetGenericTypeName(parentTypeName, numberOfParameters, typeArguments, translationSettings);
106107
}
107108

108109
genericTypeName = parentTypeName + "." + genericTypeName;
@@ -111,22 +112,21 @@ private static string GetGenericTypeName(Type genericType)
111112
return genericTypeName;
112113
}
113114

114-
private static string GetGenericTypeName(
115-
string typeName,
115+
private static string GetGenericTypeName(string typeName,
116116
int numberOfParameters,
117-
IEnumerable<Type> typeArguments)
117+
IEnumerable<Type> typeArguments, TranslationSettings translationSettings)
118118
{
119119
var typeGenericTypeArgumentFriendlyNames =
120-
typeArguments.Project(GetFriendlyName).Join(", ");
120+
typeArguments.Project(type => GetFriendlyName(type, translationSettings)).Join(", ");
121121

122122
typeName = typeName.Replace(
123123
"`" + numberOfParameters,
124124
"<" + typeGenericTypeArgumentFriendlyNames + ">");
125125

126-
return typeName.StartsWith('<') ? GetAnonymousTypeName(typeName) : typeName;
126+
return typeName.StartsWith('<') ? GetAnonymousTypeName(typeName, translationSettings) : typeName;
127127
}
128128

129-
private static string GetAnonymousTypeName(string typeName)
129+
private static string GetAnonymousTypeName(string typeName, TranslationSettings translationSettings)
130130
{
131131
var anonTypeIndex = typeName.IndexOf("AnonymousType", StringComparison.Ordinal);
132132

@@ -135,6 +135,11 @@ private static string GetAnonymousTypeName(string typeName)
135135
return typeName;
136136
}
137137

138+
if (translationSettings.AnonymousTypesAsObject)
139+
{
140+
return "object";
141+
}
142+
138143
typeName = typeName.Substring(anonTypeIndex);
139144

140145
var trimStartIndex = "AnonymousType".Length;
@@ -149,27 +154,29 @@ private static string GetAnonymousTypeName(string typeName)
149154
/// Retrieves a camel-case variable name for a variable of this <paramref name="type"/>.
150155
/// </summary>
151156
/// <param name="type">The Type for which to retrieve the variable name.</param>
157+
/// <param name="translationSettings"></param>
152158
/// <returns>A camel-case variable name for a variable of this <paramref name="type"/>.</returns>
153-
public static string GetVariableNameInCamelCase(this Type type) => GetVariableName(type).ToCamelCase();
159+
public static string GetVariableNameInCamelCase(this Type type, TranslationSettings translationSettings) => GetVariableName(type, translationSettings).ToCamelCase();
154160

155161
/// <summary>
156162
/// Retrieves a pascal-case variable name for a variable of this <paramref name="type"/>.
157163
/// </summary>
158164
/// <param name="type">The Type for which to retrieve the variable name.</param>
165+
/// <param name="translationSettings"></param>
159166
/// <returns>A pascal-case variable name for a variable of this <paramref name="type"/>.</returns>
160-
public static string GetVariableNameInPascalCase(this Type type) => GetVariableName(type).ToPascalCase();
167+
public static string GetVariableNameInPascalCase(this Type type, TranslationSettings translationSettings) => GetVariableName(type, translationSettings).ToPascalCase();
161168

162-
private static string GetVariableName(Type type)
169+
private static string GetVariableName(Type type, TranslationSettings translationSettings)
163170
{
164171
if (type.IsArray)
165172
{
166-
return GetVariableName(type.GetElementType()) + "Array";
173+
return GetVariableName(type.GetElementType(), translationSettings) + "Array";
167174
}
168175

169176
var typeIsEnumerable = type.IsEnumerable();
170177
var typeIsDictionary = typeIsEnumerable && type.IsDictionary();
171178
var namingType = (typeIsEnumerable && !typeIsDictionary) ? type.GetEnumerableElementType() : type;
172-
var variableName = GetBaseVariableName(namingType);
179+
var variableName = GetBaseVariableName(namingType, translationSettings);
173180

174181
if (namingType.IsInterface())
175182
{
@@ -178,31 +185,32 @@ private static string GetVariableName(Type type)
178185

179186
if (namingType.IsGenericType())
180187
{
181-
variableName = GetGenericTypeVariableName(variableName, namingType);
188+
variableName = GetGenericTypeVariableName(variableName, namingType, translationSettings);
182189
}
183190

184191
variableName = RemoveLeadingNonAlphaNumerics(variableName);
185192

186193
return (typeIsDictionary || !typeIsEnumerable) ? variableName : variableName.Pluralise();
187194
}
188195

189-
private static string GetBaseVariableName(Type namingType)
190-
=> namingType.IsPrimitive() ? namingType.GetFriendlyName() : namingType.Name;
196+
private static string GetBaseVariableName(Type namingType, TranslationSettings translationSettings)
197+
=> namingType.IsPrimitive() ? namingType.GetFriendlyName(translationSettings) : namingType.Name;
191198

192-
private static string GetGenericTypeVariableName(string variableName, Type namingType)
199+
private static string GetGenericTypeVariableName(string variableName, Type namingType,
200+
TranslationSettings translationSettings)
193201
{
194202
var nonNullableType = namingType.GetNonNullableType();
195203
var genericTypeArguments = namingType.GetGenericTypeArguments();
196204

197205
if (nonNullableType != namingType)
198206
{
199-
return "nullable" + genericTypeArguments[0].GetVariableNameInPascalCase();
207+
return "nullable" + genericTypeArguments[0].GetVariableNameInPascalCase(translationSettings);
200208
}
201209

202210
variableName = variableName.Substring(0, variableName.IndexOf('`'));
203211

204212
variableName += genericTypeArguments
205-
.Project(arg => "_" + arg.GetVariableNameInPascalCase())
213+
.Project(arg => "_" + arg.GetVariableNameInPascalCase(translationSettings))
206214
.Join(string.Empty);
207215

208216
return variableName;

ReadableExpressions/TranslationSettings.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,20 @@ public TranslationSettings ShowQuotedLambdaComments
4242
internal bool DoNotCommentQuotedLambdas => !CommentQuotedLambdas;
4343

4444
internal bool CommentQuotedLambdas { get; set; }
45+
46+
/// <summary>
47+
/// Annotate a Quoted Lambda Expression with a comment indicating that it has
48+
/// been Quoted.
49+
/// </summary>
50+
public TranslationSettings SerializeAnonymousTypesAsObject
51+
{
52+
get
53+
{
54+
AnonymousTypesAsObject = true;
55+
return this;
56+
}
57+
}
58+
59+
internal bool AnonymousTypesAsObject { get; private set; }
4560
}
4661
}

ReadableExpressions/Translators/AssignmentExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal static string GetAssignment(
5656
var symbol = _symbolsByNodeType[assignmentType];
5757

5858
var valueString = (value.NodeType == ExpressionType.Default)
59-
? DefaultExpressionTranslator.Translate((DefaultExpression)value)
59+
? DefaultExpressionTranslator.Translate((DefaultExpression)value, context.Settings)
6060
: GetValueTranslation(value, context);
6161

6262
var assignment = target + " " + symbol;

ReadableExpressions/Translators/BlockExpressionTranslator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static IList<string> GetVariableDeclarations(
4545
.GroupBy(v => v.Type)
4646
.Project(vGrp => new
4747
{
48-
TypeName = vGrp.Key.GetFriendlyName(),
48+
TypeName = vGrp.Key.GetFriendlyName(context.Settings),
4949
VariableNames = vGrp.Project(variable => ParameterExpressionTranslator.Translate(variable, context))
5050
})
5151
.Project(varData => $"{varData.TypeName} {varData.VariableNames.Join(", ")};")
@@ -160,7 +160,7 @@ private static string GetTerminatedStatementOrNull(Expression expression, Transl
160160
return translation;
161161
}
162162

163-
var typeName = GetVariableTypeName((BinaryExpression)expression);
163+
var typeName = GetVariableTypeName((BinaryExpression)expression, context.Settings);
164164

165165
return typeName + " " + translation;
166166
}
@@ -181,9 +181,9 @@ private static bool StatementIsTerminated(string translation, Expression express
181181
return translation.IsTerminated() || expression.IsComment();
182182
}
183183

184-
private static string GetVariableTypeName(BinaryExpression assignment)
184+
private static string GetVariableTypeName(BinaryExpression assignment, TranslationSettings translationSettings)
185185
{
186-
return UseFullTypeName(assignment) ? assignment.Left.Type.GetFriendlyName() : "var";
186+
return UseFullTypeName(assignment) ? assignment.Left.Type.GetFriendlyName(translationSettings) : "var";
187187
}
188188

189189
private static bool UseFullTypeName(BinaryExpression assignment)

ReadableExpressions/Translators/CastExpressionTranslator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static string TranslateCast(Expression expression, TranslationContext co
6464
#endif
6565

6666
var methodSubject = subjectMethod.IsStatic
67-
? subjectMethod.DeclaringType.GetFriendlyName()
67+
? subjectMethod.DeclaringType.GetFriendlyName(context.Settings)
6868
: context.Translate(methodCall.Arguments.ElementAtOrDefault(1));
6969

7070
return methodSubject + "." + subjectMethod.Name;
@@ -76,7 +76,7 @@ private static string TranslateCast(Expression expression, TranslationContext co
7676
private static string TranslateMethodConversion(UnaryExpression cast, TranslationContext context)
7777
{
7878
return MethodCallExpressionTranslator.GetMethodCall(
79-
cast.Method.DeclaringType.GetFriendlyName(),
79+
cast.Method.DeclaringType.GetFriendlyName(context.Settings),
8080
new BclMethodInfoWrapper(cast.Method),
8181
new[] { cast.Operand },
8282
cast,
@@ -96,7 +96,7 @@ public static string Translate(
9696
Type resultType,
9797
TranslationContext context)
9898
{
99-
var typeName = resultType.GetFriendlyName();
99+
var typeName = resultType.GetFriendlyName(context.Settings);
100100
var subject = context.Translate(castValue);
101101

102102
if (castValue.NodeType == ExpressionType.Assign)
@@ -115,7 +115,7 @@ public static string Translate(
115115
private static string TranslateTypeAs(Expression expression, TranslationContext context)
116116
{
117117
var typeAs = (UnaryExpression)expression;
118-
var typeName = typeAs.Type.GetFriendlyName();
118+
var typeName = typeAs.Type.GetFriendlyName(context.Settings);
119119
var subject = context.Translate(typeAs.Operand);
120120

121121
return $"({subject} as {typeName})";
@@ -124,7 +124,7 @@ private static string TranslateTypeAs(Expression expression, TranslationContext
124124
private static string TranslateTypeIs(Expression expression, TranslationContext context)
125125
{
126126
var typeIs = (TypeBinaryExpression)expression;
127-
var typeName = typeIs.TypeOperand.GetFriendlyName();
127+
var typeName = typeIs.TypeOperand.GetFriendlyName(context.Settings);
128128
var subject = context.Translate(typeIs.Expression);
129129

130130
return $"({subject} is {typeName})";

ReadableExpressions/Translators/ConstantExpressionTranslator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public string Translate(Expression expression, TranslationContext context)
3333

3434
if (constant.Type.IsEnum())
3535
{
36-
return constant.Type.GetFriendlyName() + "." + constant.Value;
36+
return constant.Type.GetFriendlyName(context.Settings) + "." + constant.Value;
3737
}
3838

3939
if (TryTranslateByTypeCode(constant, context, out var translation))
@@ -48,7 +48,7 @@ public string Translate(Expression expression, TranslationContext context)
4848
return constant.Value.ToString();
4949
}
5050

51-
return valueType.GetFriendlyName();
51+
return valueType.GetFriendlyName(context.Settings);
5252
}
5353

5454
private static bool TryTranslateByTypeCode(
@@ -92,7 +92,7 @@ private static bool TryTranslateByTypeCode(
9292
return true;
9393

9494
case NetStandardTypeCode.Object:
95-
if (IsType(constant, out translation) ||
95+
if (IsType(constant, context.Settings, out translation) ||
9696
IsLambda(constant, context, out translation) ||
9797
IsFunc(constant, out translation) ||
9898
IsRegex(constant, out translation) ||
@@ -243,11 +243,12 @@ private static string FormatNumeric(float value)
243243
return (value % 1).Equals(0) ? value.ToString("0") : value.ToString(CultureInfo.CurrentCulture);
244244
}
245245

246-
private static bool IsType(ConstantExpression constant, out string translation)
246+
private static bool IsType(ConstantExpression constant,
247+
TranslationSettings translationSettings, out string translation)
247248
{
248249
if (constant.Type.IsAssignableTo(typeof(Type)))
249250
{
250-
translation = $"typeof({((Type)constant.Value).GetFriendlyName()})";
251+
translation = $"typeof({((Type)constant.Value).GetFriendlyName(translationSettings)})";
251252
return true;
252253
}
253254

ReadableExpressions/Translators/DefaultExpressionTranslator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ public string Translate(Expression expression, TranslationContext context)
2828

2929
return defaultExpression.Type.CanBeNull()
3030
? "null"
31-
: Translate(defaultExpression);
31+
: Translate(defaultExpression, context.Settings);
3232
}
3333

34-
internal static string Translate(DefaultExpression defaultExpression)
34+
internal static string Translate(DefaultExpression defaultExpression, TranslationSettings translationSettings)
3535
{
3636
if (defaultExpression.Type == typeof(string))
3737
{
3838
return "null";
3939
}
4040

41-
var typeName = defaultExpression.Type.GetFriendlyName();
41+
var typeName = defaultExpression.Type.GetFriendlyName(translationSettings);
4242

4343
return $"default({typeName})";
4444
}

ReadableExpressions/Translators/InitialisationExpressionTranslator.Helpers.Array.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ protected override string GetNewExpressionString(NewArrayExpression initialisati
2323
{
2424
if (initialisation.Expressions.Count == 0)
2525
{
26-
return "new " + GetExplicitArrayType(initialisation) + "[0]";
26+
return "new " + GetExplicitArrayType(initialisation, context.Settings) + "[0]";
2727
}
2828

29-
var explicitType = GetExplicitArrayTypeIfRequired(initialisation);
29+
var explicitType = GetExplicitArrayTypeIfRequired(initialisation, context.Settings);
3030

3131
return "new" + explicitType + "[]";
3232
}
3333

34-
private static string GetExplicitArrayType(Expression initialisation)
34+
private static string GetExplicitArrayType(Expression initialisation,
35+
TranslationSettings translationSettings)
3536
{
36-
return initialisation.Type.GetElementType().GetFriendlyName();
37+
return initialisation.Type.GetElementType().GetFriendlyName(translationSettings);
3738
}
3839

39-
private static string GetExplicitArrayTypeIfRequired(NewArrayExpression initialisation)
40+
private static string GetExplicitArrayTypeIfRequired(NewArrayExpression initialisation,
41+
TranslationSettings translationSettings)
4042
{
4143
var expressionTypes = initialisation
4244
.Expressions
@@ -49,7 +51,7 @@ private static string GetExplicitArrayTypeIfRequired(NewArrayExpression initiali
4951
return null;
5052
}
5153

52-
return " " + GetExplicitArrayType(initialisation);
54+
return " " + GetExplicitArrayType(initialisation, translationSettings);
5355
}
5456

5557
protected override IEnumerable<string> GetMemberInitialisations(

ReadableExpressions/Translators/MemberAccessExpressionTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static string GetSubject(MemberExpression memberExpression, TranslationC
3030
{
3131
return (memberExpression.Expression != null)
3232
? GetInstanceMemberSubject(memberExpression, context)
33-
: memberExpression.Member.DeclaringType.GetFriendlyName();
33+
: memberExpression.Member.DeclaringType.GetFriendlyName(context.Settings);
3434
}
3535

3636
private static string GetInstanceMemberSubject(MemberExpression memberExpression, TranslationContext context)

0 commit comments

Comments
 (0)