private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string lastNS, List<MetadataType> allTypes, CreateTypeOptions options)
{
if (type.IsNested.GetValueOrDefault() && !options.IsNestedType)
return lastNS;
if (!Config.ExcludeNamespace)
{
var ns = Config.GlobalNamespace ?? type.Namespace;
if (ns != lastNS)
{
if (lastNS != null)
sb.AppendLine("}");
lastNS = ns;
sb.AppendLine();
sb.AppendLine($"namespace {ns.SafeToken()}");
sb.AppendLine("{");
}
sb = sb.Indent();
}
sb.AppendLine();
AppendComments(sb, type.Description);
if (type.Routes != null)
{
AppendAttributes(sb, type.Routes.ConvertAll(x => x.ToMetadataAttribute()));
}
AppendAttributes(sb, type.Attributes);
AppendDataContract(sb, type.DataContract);
if (Config.AddGeneratedCodeAttributes)
sb.AppendLine($"[GeneratedCode(\"AddServiceStackReference\", \"{Env.VersionString}\")]");
var typeAccessor = !Config.MakeInternal ? "public" : "internal";
if (type.IsEnum.GetValueOrDefault())
{
sb.AppendLine($"{typeAccessor} enum {Type(type.Name, type.GenericArgs)}");
sb.AppendLine("{");
sb = sb.Indent();
if (type.EnumNames != null)
{
for (var i = 0; i < type.EnumNames.Count; i++)
{
var name = type.EnumNames[i];
var value = type.EnumValues?[i];
sb.AppendLine(value == null
? $"{name},"
: $"{name} = {value},");
}
}
sb = sb.UnIndent();
sb.AppendLine("}");
}
else
{
var partial = Config.MakePartial ? "partial " : "";
var defType = type.IsInterface() ? "interface" : "class";
sb.AppendLine($"{typeAccessor} {partial}{defType} {Type(type.Name, type.GenericArgs)}");
//: BaseClass, Interfaces
var inheritsList = new List<string>();
if (type.Inherits != null)
{
inheritsList.Add(Type(type.Inherits, includeNested:true));
}
if (options.ImplementsFn != null)
{
var implStr = options.ImplementsFn();
if (!string.IsNullOrEmpty(implStr))
inheritsList.Add(implStr);
if (!type.Implements.IsEmpty())
type.Implements.Each(x => inheritsList.Add(Type(x)));
}
var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
if (makeExtensible)
inheritsList.Add("IExtensibleDataObject");
if (inheritsList.Count > 0)
sb.AppendLine($" : {string.Join(", ", inheritsList.ToArray())}");
sb.AppendLine("{");
sb = sb.Indent();
AddConstuctor(sb, type, options);
AddProperties(sb, type,
includeResponseStatus: Config.AddResponseStatus && options.IsResponse
&& type.Properties.Safe().All(x => x.Name != typeof(ResponseStatus).Name));
foreach (var innerTypeRef in type.InnerTypes.Safe())
{
var innerType = allTypes.FirstOrDefault(x => x.Name == innerTypeRef.Name);
if (innerType == null)
continue;
sb = sb.UnIndent();
AppendType(ref sb, innerType, lastNS, allTypes,
new CreateTypeOptions { IsNestedType = true });
sb = sb.Indent();
}
sb = sb.UnIndent();
sb.AppendLine("}");
}
if (!Config.ExcludeNamespace)
{
sb = sb.UnIndent();
}
return lastNS;
}