2022-08-13 13:01:47 -07:00

161 lines
6.4 KiB
C#

namespace Json2CSharpCodeGenerator.Lib.CodeWriters;
public class VisualBasicCodeWriter : ICodeWriter
{
public string FileExtension => ".vb";
public string DisplayName => "Visual Basic .NET";
private const string _NoRenameAttribute = "<Obfuscation(Feature:=\"renaming\", Exclude:=true)>";
private const string _NoPruneAttribute = "<Obfuscation(Feature:=\"trigger\", Exclude:=false)>";
public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config)
{
return type.Type switch
{
JsonTypeEnum.Anything => "Object",
JsonTypeEnum.Array => GetCollectionTypeName(GetTypeName(type.InternalType, config), config.CollectionType),
JsonTypeEnum.Dictionary => "Dictionary(Of String, " + GetTypeName(type.InternalType, config) + ")",
JsonTypeEnum.Boolean => "Boolean",
JsonTypeEnum.Float => "Double",
JsonTypeEnum.Integer => "Integer",
JsonTypeEnum.Long => "Long",
JsonTypeEnum.Date => "DateTime",
JsonTypeEnum.NonConstrained => "Object",
JsonTypeEnum.NullableBoolean => "Boolean?",
JsonTypeEnum.NullableFloat => "Double?",
JsonTypeEnum.NullableInteger => "Integer?",
JsonTypeEnum.NullableLong => "Long?",
JsonTypeEnum.NullableDate => "DateTime?",
JsonTypeEnum.NullableSomething => "Object",
JsonTypeEnum.Object => type.AssignedName,
JsonTypeEnum.String => "String",
_ => throw new NotSupportedException("Unsupported json type"),
};
}
private static string GetCollectionTypeName(string elementTypeName, OutputCollectionType type)
{
return type switch
{
OutputCollectionType.Array => elementTypeName + "()",
OutputCollectionType.MutableList => "List(Of" + elementTypeName + ")",
OutputCollectionType.IReadOnlyList => "IReadOnlyList(Of" + elementTypeName + ")",
OutputCollectionType.ImmutableArray => "ImmutableArray(Of" + elementTypeName + ")",
_ => throw new ArgumentOutOfRangeException(paramName: nameof(type), actualValue: type, message: "Invalid " + nameof(OutputCollectionType) + " enum value."),
};
}
private bool ShouldApplyNoRenamingAttribute(IJsonClassGeneratorConfig config) => config.ApplyObfuscationAttributes && !config.UsePascalCase;
private bool ShouldApplyNoPruneAttribute(IJsonClassGeneratorConfig config) => config.ApplyObfuscationAttributes && config.OutputType == OutputTypes.MutableClass && config.MutableClasses.Members == OutputMembers.AsPublicFields;
public void WriteClass(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type)
{
string visibility = config.InternalVisibility ? "Friend" : "Public";
if (config.UseNestedClasses)
{
sw.WriteLine(" {0} Partial Class {1}", visibility, config.MainClass);
if (!type.IsRoot)
{
if (ShouldApplyNoRenamingAttribute(config))
sw.WriteLine(" " + _NoRenameAttribute);
if (ShouldApplyNoPruneAttribute(config))
sw.WriteLine(" " + _NoPruneAttribute);
sw.WriteLine(" {0} Class {1}", visibility, type.AssignedName);
}
}
else
{
if (ShouldApplyNoRenamingAttribute(config))
sw.WriteLine(" " + _NoRenameAttribute);
if (ShouldApplyNoPruneAttribute(config))
sw.WriteLine(" " + _NoPruneAttribute);
sw.WriteLine(" {0} Class {1}", visibility, type.AssignedName);
}
string prefix = config.UseNestedClasses && !type.IsRoot ? " " : " ";
WriteClassMembers(config, sw, type, prefix);
if (config.UseNestedClasses && !type.IsRoot)
sw.WriteLine(" End Class");
sw.WriteLine(" End Class");
sw.WriteLine();
}
private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type, string prefix)
{
foreach (FieldInfo field in type.Fields)
{
if (config.UsePascalCase || config.ExamplesInDocumentation)
sw.WriteLine();
if (config.ExamplesInDocumentation)
{
sw.WriteLine(prefix + "''' <summary>");
sw.WriteLine(prefix + "''' Examples: " + field.GetExamplesText());
sw.WriteLine(prefix + "''' </summary>");
}
if (config.UsePascalCase)
{
sw.WriteLine(prefix + "<JsonProperty(\"{0}\")>", field.JsonMemberName);
}
if (config.MutableClasses.Members == OutputMembers.AsProperties)
{
sw.WriteLine(prefix + "Public Property {1} As {0}", field.Type.GetTypeName(), field.MemberName);
}
else
{
sw.WriteLine(prefix + "Public {1} As {0}", field.Type.GetTypeName(), field.MemberName);
}
}
}
public void WriteFileStart(IJsonClassGeneratorConfig config, TextWriter sw)
{
sw.WriteLine();
sw.WriteLine("Imports System");
sw.WriteLine("Imports System.Collections.Generic");
if (ShouldApplyNoRenamingAttribute(config) || ShouldApplyNoPruneAttribute(config))
{
sw.WriteLine("Imports System.Reflection");
}
if (config.AttributeLibrary == JsonLibrary.NewtonsoftJson)
{
sw.WriteLine("Imports Newtonsoft.Json");
sw.WriteLine("Imports Newtonsoft.Json.Linq");
}
else if (config.AttributeLibrary == JsonLibrary.SystemTextJson)
{
sw.WriteLine("Imports System.Text.Json");
}
if (!string.IsNullOrWhiteSpace(config.SecondaryNamespace) && !config.UseNestedClasses)
{
sw.WriteLine("Imports {0}", config.SecondaryNamespace);
}
}
public void WriteFileEnd(IJsonClassGeneratorConfig config, TextWriter sw)
{
}
public void WriteNamespaceStart(IJsonClassGeneratorConfig config, TextWriter sw, bool root)
{
sw.WriteLine();
sw.WriteLine("Namespace Global.{0}", root && !config.UseNestedClasses ? config.Namespace : (config.SecondaryNamespace ?? config.Namespace));
sw.WriteLine();
}
public void WriteNamespaceEnd(IJsonClassGeneratorConfig config, TextWriter sw, bool root) => sw.WriteLine("End Namespace");
}