Skip to content

Commit

Permalink
Merge pull request #23 from CloudConstruct/extension/code-generation/…
Browse files Browse the repository at this point in the history
…add-predefined-list-handling

Add support for Predefined List in Code Generation extension
  • Loading branch information
Piedone authored Mar 9, 2022
2 parents 583e36e + dd7160f commit 691b966
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions Extensions/CodeGeneration/CodeGenerationDisplayDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void GenerateCodeForPartsWithFields(
}
}

private string ConvertJToken(JToken jToken)
private string ConvertJToken(JToken jToken, int indentationDepth)
{
switch (jToken)
{
Expand All @@ -131,22 +131,65 @@ private string ConvertJToken(JToken jToken)
string => $"\"{value}\"",
_ => value?.ToString()?.Replace(',', '.'), // Replace decimal commas.
};

case JArray jArray:
return $"new[] {{ {string.Join(", ", jArray.Select(ConvertJToken))} }}";
var indentation = new string(' ', indentationDepth + 4);

var items = jArray.Select(item => ConvertJToken(item, indentationDepth + 8)).ToList();

// If the items are formatted (for ListValueOption) then don't inject line-by-line formatting.
if (items.Any(item => item.ContainsOrdinalIgnoreCase(Environment.NewLine)))
{
var token = string.Join(string.Empty, items);
return $"new[]\n{indentation}{{\n{token}{indentation}}}";
}

// Otherwise, make sure that we have proper formatting for string arrays.
var stringArrayCodeBuilder = new StringBuilder("new[]");
stringArrayCodeBuilder.AppendLine();
stringArrayCodeBuilder.AppendLine($"{indentation}{{");

var itemIndentation = new string(' ', indentationDepth + 8);

foreach (var item in items)
{
stringArrayCodeBuilder.AppendLine($"{itemIndentation}{item},");
}

stringArrayCodeBuilder.Append($"{indentation}}}");

return stringArrayCodeBuilder.ToString();

case JObject jObject:
var braceIndentation = new string(' ', indentationDepth);
var propertyIndentation = new string(' ', indentationDepth + 4);
if (jObject["name"] != null && jObject["value"] != null)
{
var objectCodeBuilder = new StringBuilder();
objectCodeBuilder.AppendLine($"{braceIndentation}new ListValueOption");
objectCodeBuilder.AppendLine($"{braceIndentation}{{");
objectCodeBuilder.AppendLine($"{propertyIndentation}Name = \"{jObject["name"]}\",");
objectCodeBuilder.AppendLine($"{propertyIndentation}Value = \"{jObject["value"]}\",");
objectCodeBuilder.AppendLine($"{braceIndentation}}},");

return objectCodeBuilder.ToString();
}

// Using a quoted string so it doesn't mess up the syntax highlighting of the rest of the code.
return T["\"FIX ME! Couldn't determine the actual type to instantiate.\" {0}", jObject.ToString()];

default:
throw new NotSupportedException($"Settings values of type {jToken.GetType()} are not supported.");
}
}

private void AddSettingsWithout<T>(StringBuilder codeBuilder, JObject settings, int indentationDepth)
{
var indentation = string.Join(string.Empty, Enumerable.Repeat(" ", indentationDepth));
var indentation = new string(' ', indentationDepth);

var filteredSettings = ((IEnumerable<KeyValuePair<string, JToken>>)settings)
.Where(setting => setting.Key != typeof(T).Name);

foreach (var setting in filteredSettings)
{
var properties = setting.Value.Where(property => property is JProperty).Cast<JProperty>().ToArray();
Expand All @@ -161,16 +204,10 @@ private void AddSettingsWithout<T>(StringBuilder codeBuilder, JObject settings,
for (int i = 0; i < properties.Length; i++)
{
var property = properties[i];
var propertyValue = ConvertJToken(property.Value);

if (propertyValue == null)
{
propertyValue = "\"\"";
}
else if (propertyValue.Contains(Environment.NewLine, StringComparison.OrdinalIgnoreCase))
{
propertyValue = "@" + propertyValue;
}
var propertyValue = ConvertJToken(property.Value, indentationDepth);

propertyValue ??= "\"\"";

codeBuilder.AppendLine($"{indentation} {property.Name} = {propertyValue},");
}
Expand Down

0 comments on commit 691b966

Please sign in to comment.