in src/JetBrains.Space.Generator/CodeGeneration/CSharp/Generators/CSharpPartialExtensionsGenerator.cs [39:118]
private string GenerateExtensionMethodsFor(
string currentDtoType,
string currentPartialType,
ApiField apiField)
{
var indent = new Indent();
var builder = new CSharpBuilder();
var propertyName = apiField.ToCSharpPropertyName(currentDtoType);
var apiFieldName = apiField.Name;
// Field
ApiDocumentationUtilities.RenderCSharpDocumentation(apiField.Description, apiField.Experimental, output =>
{
builder.Append(indent.Wrap(output));
});
if (apiField.Deprecation != null)
{
builder.AppendLine($"{indent}{apiField.Deprecation.ToCSharpDeprecation()}");
}
else if (apiField.Experimental != null && FeatureFlags.GenerateExperimentalAnnotations)
{
builder.AppendLine($"{indent}{apiField.Experimental.ToCSharpExperimental()}");
}
builder.AppendLine($"{indent}public static {currentPartialType} With{propertyName}(this {currentPartialType} it)");
indent.Increment();
builder.AppendLine($"{indent}=> it.AddFieldName(\"{apiFieldName}\");");
indent.Decrement();
builder.AppendLine($"{indent}");
var currentFieldInnerType = GenerateCSharpTypeFrom(apiField.Type);
var isPrimitiveOrObject = apiField.Type is ApiFieldType.Primitive or ApiFieldType.Object;
var isArrayOfPrimitive = apiField.Type is ApiFieldType.Array { ElementType: ApiFieldType.Primitive };
if (!isPrimitiveOrObject && !isArrayOfPrimitive && !string.IsNullOrEmpty(currentFieldInnerType))
{
// Recursive field?
if (currentDtoType == currentFieldInnerType)
{
ApiDocumentationUtilities.RenderCSharpDocumentation(apiField.Description, apiField.Experimental, output =>
{
builder.Append(indent.Wrap(output));
});
if (apiField.Deprecation != null)
{
builder.AppendLine($"{indent}{apiField.Deprecation.ToCSharpDeprecation()}");
}
else if (apiField.Experimental != null && FeatureFlags.GenerateExperimentalAnnotations)
{
builder.AppendLine($"{indent}{apiField.Experimental.ToCSharpExperimental()}");
}
builder.AppendLine($"{indent}public static {currentPartialType} With{propertyName}Recursive(this {currentPartialType} it)");
indent.Increment();
builder.AppendLine($"{indent}=> it.AddFieldName(\"{apiFieldName}!\");");
indent.Decrement();
builder.AppendLine($"{indent}");
}
// Field with partial builder
ApiDocumentationUtilities.RenderCSharpDocumentation(apiField.Description, apiField.Experimental, output =>
{
builder.Append(indent.Wrap(output));
});
if (apiField.Deprecation != null)
{
builder.AppendLine($"{indent}{apiField.Deprecation.ToCSharpDeprecation()}");
}
else if (apiField.Experimental != null && FeatureFlags.GenerateExperimentalAnnotations)
{
builder.AppendLine($"{indent}{apiField.Experimental.ToCSharpExperimental()}");
}
builder.AppendLine($"{indent}public static {currentPartialType} With{propertyName}(this {currentPartialType} it, Func<Partial<{currentFieldInnerType}>, Partial<{currentFieldInnerType}>> partialBuilder)");
indent.Increment();
builder.AppendLine($"{indent}=> it.AddFieldName(\"{apiFieldName}\", partialBuilder(new Partial<{currentFieldInnerType}>(it)));");
indent.Decrement();
builder.AppendLine($"{indent}");
}
return builder.ToString();
}