public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMetadata nativeTypes) { var typeNamespaces = new HashSet <string>(); var includeList = RemoveIgnoredTypes(metadata); metadata.Types.Each(x => typeNamespaces.Add(x.Namespace)); metadata.Operations.Each(x => typeNamespaces.Add(x.Request.Namespace)); var defaultImports = new List <string>(DefaultImports); if (!Config.DefaultImports.IsEmpty()) { defaultImports = Config.DefaultImports; } else { if (ReferencesGson(metadata)) { defaultImports.AddIfNotExists(GSonAnnotationsNamespace); defaultImports.AddIfNotExists(GSonReflectNamespace); } if (ReferencesStream(metadata)) { defaultImports.AddIfNotExists(JavaIoNamespace); } } var defaultNamespace = Config.GlobalNamespace ?? DefaultGlobalNamespace; string DefaultValue(string k) => request.QueryString[k].IsNullOrEmpty() ? "//" : ""; var sbInner = StringBuilderCache.Allocate(); var sb = new StringBuilderWrapper(sbInner); sb.AppendLine("/* Options:"); sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " "))); sb.AppendLine("Version: {0}".Fmt(Env.ServiceStackVersion)); sb.AppendLine("Tip: {0}".Fmt(HelpMessages.NativeTypesDtoOptionsTip.Fmt("//"))); sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl)); sb.AppendLine(); sb.AppendLine("{0}Package: {1}".Fmt(DefaultValue("Package"), Config.Package)); sb.AppendLine("{0}GlobalNamespace: {1}".Fmt(DefaultValue("GlobalNamespace"), defaultNamespace)); sb.AppendLine("{0}AddPropertyAccessors: {1}".Fmt(DefaultValue("AddPropertyAccessors"), Config.AddPropertyAccessors)); sb.AppendLine("{0}SettersReturnThis: {1}".Fmt(DefaultValue("SettersReturnThis"), Config.SettersReturnThis)); sb.AppendLine("{0}AddServiceStackTypes: {1}".Fmt(DefaultValue("AddServiceStackTypes"), Config.AddServiceStackTypes)); sb.AppendLine("{0}AddResponseStatus: {1}".Fmt(DefaultValue("AddResponseStatus"), Config.AddResponseStatus)); sb.AppendLine("{0}AddDescriptionAsComments: {1}".Fmt(DefaultValue("AddDescriptionAsComments"), Config.AddDescriptionAsComments)); sb.AppendLine("{0}AddImplicitVersion: {1}".Fmt(DefaultValue("AddImplicitVersion"), Config.AddImplicitVersion)); sb.AppendLine("{0}IncludeTypes: {1}".Fmt(DefaultValue("IncludeTypes"), Config.IncludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeTypes: {1}".Fmt(DefaultValue("ExcludeTypes"), Config.ExcludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}TreatTypesAsStrings: {1}".Fmt(DefaultValue("TreatTypesAsStrings"), Config.TreatTypesAsStrings.Safe().ToArray().Join(","))); sb.AppendLine("{0}DefaultImports: {1}".Fmt(DefaultValue("DefaultImports"), defaultImports.Join(","))); sb.AppendLine("*/"); sb.AppendLine(); foreach (var typeName in Config.TreatTypesAsStrings.Safe()) { TypeAliases[typeName] = "String"; } if (Config.Package != null) { sb.AppendLine("package {0};".Fmt(Config.Package)); sb.AppendLine(); } string lastNS = null; var existingTypes = new HashSet <string>(); var requestTypes = metadata.Operations.Select(x => x.Request).ToHashSet(); var requestTypesMap = metadata.Operations.ToSafeDictionary(x => x.Request); var responseTypes = metadata.Operations .Where(x => x.Response != null) .Select(x => x.Response).ToHashSet(); var types = metadata.Types.ToHashSet(); allTypes = new List <MetadataType>(); allTypes.AddRange(requestTypes); allTypes.AddRange(responseTypes); allTypes.AddRange(types); allTypes = FilterTypes(allTypes); //TypeScript doesn't support reusing same type name with different generic airity var conflictPartialNames = allTypes.Map(x => x.Name).Distinct() .GroupBy(g => g.LeftPart('`')) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); this.conflictTypeNames = allTypes .Where(x => conflictPartialNames.Any(name => x.Name.StartsWith(name))) .Map(x => x.Name); defaultImports.Each(x => sb.AppendLine("import {0};".Fmt(x))); sb.AppendLine(); var insertCode = InsertCodeFilter?.Invoke(allTypes, Config); if (insertCode != null) { sb.AppendLine(insertCode); } sb.AppendLine("public class {0}".Fmt(defaultNamespace.SafeToken())); sb.AppendLine("{"); //ServiceStack core interfaces foreach (var type in allTypes) { var fullTypeName = type.GetFullName(); if (requestTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName)) { MetadataType response = null; if (requestTypesMap.TryGetValue(type, out var operation)) { response = operation.Response; } lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { ImplementsFn = () => { if (!Config.AddReturnMarker && !type.ReturnVoidMarker && type.ReturnMarkerTypeName == null) { return(null); } if (type.ReturnVoidMarker) { return("IReturnVoid"); } if (type.ReturnMarkerTypeName != null) { return(Type("IReturn`1", new[] { Type(type.ReturnMarkerTypeName) })); } return(response != null ? Type("IReturn`1", new[] { Type(response.Name, response.GenericArgs) }) : null); }, IsRequest = true, }); existingTypes.Add(fullTypeName); } } else if (responseTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName) && !Config.IgnoreTypesInNamespaces.Contains(type.Namespace)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsResponse = true, }); existingTypes.Add(fullTypeName); } } else if (types.Contains(type) && !existingTypes.Contains(fullTypeName)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsType = true }); existingTypes.Add(fullTypeName); } } sb.AppendLine(); sb.AppendLine("}"); var addCode = AddCodeFilter?.Invoke(allTypes, Config); if (addCode != null) { sb.AppendLine(addCode); } return(StringBuilderCache.ReturnAndFree(sbInner)); }
public string GetCode(MetadataTypes metadata, IRequest request) { var namespaces = Config.GetDefaultNamespaces(metadata); var typeNamespaces = new HashSet <string>(); metadata.RemoveIgnoredTypesForNet(Config); metadata.Types.Each(x => typeNamespaces.Add(x.Namespace)); metadata.Operations.Each(x => { typeNamespaces.Add(x.Request.Namespace); if (x.Response != null) { typeNamespaces.Add(x.Response.Namespace); } }); // Look first for shortest Namespace ending with `ServiceModel` convention, else shortest ns var globalNamespace = Config.GlobalNamespace ?? typeNamespaces.Where(x => x.EndsWith("ServiceModel")) .OrderBy(x => x).FirstOrDefault() ?? typeNamespaces.OrderBy(x => x).First(); Func <string, string> defaultValue = k => request.QueryString[k].IsNullOrEmpty() ? "//" : ""; var sbInner = new StringBuilder(); var sb = new StringBuilderWrapper(sbInner); sb.AppendLine("(* Options:"); sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " "))); sb.AppendLine("Version: {0}".Fmt(Env.VersionString)); sb.AppendLine("Tip: {0}".Fmt(HelpMessages.NativeTypesDtoOptionsTip.Fmt("//"))); sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl)); if (Config.UsePath != null) { sb.AppendLine("UsePath: {0}".Fmt(Config.UsePath)); } sb.AppendLine(); sb.AppendLine("{0}GlobalNamespace: {1}".Fmt(defaultValue("GlobalNamespace"), Config.GlobalNamespace)); sb.AppendLine("{0}MakeDataContractsExtensible: {1}".Fmt(defaultValue("MakeDataContractsExtensible"), Config.MakeDataContractsExtensible)); sb.AppendLine("{0}AddReturnMarker: {1}".Fmt(defaultValue("AddReturnMarker"), Config.AddReturnMarker)); sb.AppendLine("{0}AddDescriptionAsComments: {1}".Fmt(defaultValue("AddDescriptionAsComments"), Config.AddDescriptionAsComments)); sb.AppendLine("{0}AddDataContractAttributes: {1}".Fmt(defaultValue("AddDataContractAttributes"), Config.AddDataContractAttributes)); sb.AppendLine("{0}AddIndexesToDataMembers: {1}".Fmt(defaultValue("AddIndexesToDataMembers"), Config.AddIndexesToDataMembers)); sb.AppendLine("{0}AddGeneratedCodeAttributes: {1}".Fmt(defaultValue("AddGeneratedCodeAttributes"), Config.AddGeneratedCodeAttributes)); sb.AppendLine("{0}AddResponseStatus: {1}".Fmt(defaultValue("AddResponseStatus"), Config.AddResponseStatus)); sb.AppendLine("{0}AddImplicitVersion: {1}".Fmt(defaultValue("AddImplicitVersion"), Config.AddImplicitVersion)); sb.AppendLine("{0}ExportValueTypes: {1}".Fmt(defaultValue("ExportValueTypes"), Config.ExportValueTypes)); sb.AppendLine("{0}IncludeTypes: {1}".Fmt(defaultValue("IncludeTypes"), Config.IncludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeTypes: {1}".Fmt(defaultValue("ExcludeTypes"), Config.ExcludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}InitializeCollections: {1}".Fmt(defaultValue("InitializeCollections"), Config.InitializeCollections)); //sb.AppendLine("{0}AddDefaultXmlNamespace: {1}".Fmt(defaultValue("AddDefaultXmlNamespace"), Config.AddDefaultXmlNamespace)); sb.AppendLine("{0}AddNamespaces: {1}".Fmt(defaultValue("AddNamespaces"), Config.AddNamespaces.Safe().ToArray().Join(","))); sb.AppendLine("*)"); sb.AppendLine(); string lastNS = null; var existingTypes = new HashSet <string>(); var requestTypes = metadata.Operations.Select(x => x.Request).ToHashSet(); var requestTypesMap = metadata.Operations.ToSafeDictionary(x => x.Request); var responseTypes = metadata.Operations .Where(x => x.Response != null) .Select(x => x.Response).ToHashSet(); var types = metadata.Types.ToHashSet(); allTypes = new List <MetadataType>(); allTypes.AddRange(types); allTypes.AddRange(responseTypes); allTypes.AddRange(requestTypes); var orderedTypes = FilterTypes(allTypes); sb.AppendLine("namespace {0}".Fmt(globalNamespace.SafeToken())); sb.AppendLine(); foreach (var ns in namespaces.Where(x => !string.IsNullOrEmpty(x))) { sb.AppendLine("open " + ns); } if (Config.AddGeneratedCodeAttributes) { sb.AppendLine("open System.CodeDom.Compiler"); } var insertCode = InsertCodeFilter?.Invoke(allTypes, Config); if (insertCode != null) { sb.AppendLine(insertCode); } foreach (var type in orderedTypes) { var fullTypeName = type.GetFullName(); if (requestTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName)) { MetadataType response = null; MetadataOperationType operation; if (requestTypesMap.TryGetValue(type, out operation)) { response = operation.Response; } lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { ImplementsFn = () => { if (!Config.AddReturnMarker && !type.ReturnVoidMarker && type.ReturnMarkerTypeName == null) { return(null); } if (type.ReturnVoidMarker) { return("IReturnVoid"); } if (type.ReturnMarkerTypeName != null) { return(Type("IReturn`1", new[] { Type(type.ReturnMarkerTypeName) })); } return(response != null ? Type("IReturn`1", new[] { Type(response.Name, response.GenericArgs) }) : null); }, IsRequest = true, }); existingTypes.Add(fullTypeName); } } else if (responseTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName) && !Config.IgnoreTypesInNamespaces.Contains(type.Namespace)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsResponse = true, }); existingTypes.Add(fullTypeName); } } else if (types.Contains(type) && !existingTypes.Contains(fullTypeName)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsType = true }); existingTypes.Add(fullTypeName); } } var addCode = AddCodeFilter?.Invoke(allTypes, Config); if (addCode != null) { sb.AppendLine(addCode); } sb.AppendLine(); return(StringBuilderCache.ReturnAndFree(sbInner)); }
public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMetadata nativeTypes) { var typeNamespaces = new HashSet <string>(); var includeList = metadata.RemoveIgnoredTypes(Config); metadata.Types.Each(x => typeNamespaces.Add(x.Namespace)); metadata.Operations.Each(x => typeNamespaces.Add(x.Request.Namespace)); var defaultImports = !Config.DefaultImports.IsEmpty() ? Config.DefaultImports : DefaultImports; var globalNamespace = Config.GlobalNamespace; string defaultValue(string k) => request.QueryString[k].IsNullOrEmpty() ? "//" : ""; var sbInner = StringBuilderCache.Allocate(); var sb = new StringBuilderWrapper(sbInner); sb.AppendLine("/* Options:"); sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " "))); sb.AppendLine("Version: {0}".Fmt(Env.VersionString)); sb.AppendLine("Tip: {0}".Fmt(HelpMessages.NativeTypesDtoOptionsTip.Fmt("//"))); sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl)); if (Config.UsePath != null) { sb.AppendLine("UsePath: {0}".Fmt(Config.UsePath)); } sb.AppendLine(); sb.AppendLine("{0}GlobalNamespace: {1}".Fmt(defaultValue("GlobalNamespace"), Config.GlobalNamespace)); if (!Config.ExportAsTypes) // Optional properties only on interfaces { sb.AppendLine("{0}MakePropertiesOptional: {1}".Fmt(defaultValue("MakePropertiesOptional"), Config.MakePropertiesOptional)); } sb.AppendLine("{0}AddServiceStackTypes: {1}".Fmt(defaultValue("AddServiceStackTypes"), Config.AddServiceStackTypes)); sb.AppendLine("{0}AddResponseStatus: {1}".Fmt(defaultValue("AddResponseStatus"), Config.AddResponseStatus)); sb.AppendLine("{0}AddImplicitVersion: {1}".Fmt(defaultValue("AddImplicitVersion"), Config.AddImplicitVersion)); sb.AppendLine("{0}AddDescriptionAsComments: {1}".Fmt(defaultValue("AddDescriptionAsComments"), Config.AddDescriptionAsComments)); sb.AppendLine("{0}IncludeTypes: {1}".Fmt(defaultValue("IncludeTypes"), Config.IncludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeTypes: {1}".Fmt(defaultValue("ExcludeTypes"), Config.ExcludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}DefaultImports: {1}".Fmt(defaultValue("DefaultImports"), defaultImports.Join(","))); sb.AppendLine("*/"); sb.AppendLine(); string lastNS = null; var existingTypes = new HashSet <string>(); var requestTypes = metadata.Operations.Select(x => x.Request).ToHashSet(); var requestTypesMap = metadata.Operations.ToSafeDictionary(x => x.Request); var responseTypes = metadata.Operations .Where(x => x.Response != null) .Select(x => x.Response).ToHashSet(); var types = metadata.Types.CreateSortedTypeList(); allTypes = metadata.GetAllTypesOrdered(); allTypes.RemoveAll(x => x.IgnoreType(Config, includeList)); allTypes = FilterTypes(allTypes); //TypeScript doesn't support reusing same type name with different generic airity var conflictPartialNames = allTypes.Map(x => x.Name).Distinct() .GroupBy(g => g.LeftPart('`')) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); this.conflictTypeNames = allTypes .Where(x => conflictPartialNames.Any(name => x.Name.StartsWith(name))) .Map(x => x.Name); foreach (var import in defaultImports) { var pos = import.IndexOf(':'); sb.AppendLine(pos == -1 ? $"import {import};" : $"import {{ {import.Substring(0, pos)} }} from \"{import.Substring(pos + 1).StripQuotes()}\";"); } if (!string.IsNullOrEmpty(globalNamespace)) { var moduleDef = Config.ExportAsTypes ? "export " : "declare "; sb.AppendLine(); sb.AppendLine("{0}module {1}".Fmt(moduleDef, globalNamespace.SafeToken())); sb.AppendLine("{"); sb = sb.Indent(); } var insertCode = InsertCodeFilter?.Invoke(allTypes, Config); if (insertCode != null) { sb.AppendLine(insertCode); } //ServiceStack core interfaces foreach (var type in allTypes) { var fullTypeName = type.GetFullName(); if (requestTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName)) { MetadataType response = null; if (requestTypesMap.TryGetValue(type, out var operation)) { response = operation.Response; } lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { ImplementsFn = () => { if (!Config.AddReturnMarker && !type.ReturnVoidMarker && type.ReturnMarkerTypeName == null) { return(null); } if (type.ReturnVoidMarker) { return("IReturnVoid"); } if (type.ReturnMarkerTypeName != null) { return(Type("IReturn`1", new[] { Type(type.ReturnMarkerTypeName).InReturnMarker() })); } return(response != null ? Type("IReturn`1", new[] { Type(response.Name, response.GenericArgs).InReturnMarker() }) : null); }, IsRequest = true, }); existingTypes.Add(fullTypeName); } } else if (responseTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName) && !Config.IgnoreTypesInNamespaces.Contains(type.Namespace)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsResponse = true, }); existingTypes.Add(fullTypeName); } } else if (types.Contains(type) && !existingTypes.Contains(fullTypeName)) { lastNS = AppendType(ref sb, type, lastNS, new CreateTypeOptions { IsType = true }); existingTypes.Add(fullTypeName); } } var addCode = AddCodeFilter?.Invoke(allTypes, Config); if (addCode != null) { sb.AppendLine(addCode); } if (!string.IsNullOrEmpty(globalNamespace)) { sb = sb.UnIndent(); sb.AppendLine(); sb.AppendLine("}"); } sb.AppendLine(); //tslint return(StringBuilderCache.ReturnAndFree(sbInner)); }
public string GetCode(MetadataTypes metadata, IRequest request) { var namespaces = Config.GetDefaultNamespaces(metadata); metadata.RemoveIgnoredTypesForNet(Config); if (!Config.ExcludeNamespace) { if (Config.GlobalNamespace == null) { metadata.Types.Each(x => namespaces.Add(x.Namespace)); metadata.Operations.Each(x => { namespaces.Add(x.Request.Namespace); if (x.Response != null) { namespaces.Add(x.Response.Namespace); } }); } else { namespaces.Add(Config.GlobalNamespace); } } string defaultValue(string k) => request.QueryString[k].IsNullOrEmpty() ? "//" : ""; var sbInner = StringBuilderCache.Allocate(); var sb = new StringBuilderWrapper(sbInner); sb.AppendLine("/* Options:"); sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " "))); sb.AppendLine("Version: {0}".Fmt(Env.VersionString)); sb.AppendLine("Tip: {0}".Fmt(HelpMessages.NativeTypesDtoOptionsTip.Fmt("//"))); sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl)); if (Config.UsePath != null) { sb.AppendLine("UsePath: {0}".Fmt(Config.UsePath)); } sb.AppendLine(); sb.AppendLine("{0}GlobalNamespace: {1}".Fmt(defaultValue("GlobalNamespace"), Config.GlobalNamespace)); sb.AppendLine("{0}MakePartial: {1}".Fmt(defaultValue("MakePartial"), Config.MakePartial)); sb.AppendLine("{0}MakeVirtual: {1}".Fmt(defaultValue("MakeVirtual"), Config.MakeVirtual)); sb.AppendLine("{0}MakeInternal: {1}".Fmt(defaultValue("MakeInternal"), Config.MakeInternal)); sb.AppendLine("{0}MakeDataContractsExtensible: {1}".Fmt(defaultValue("MakeDataContractsExtensible"), Config.MakeDataContractsExtensible)); sb.AppendLine("{0}AddReturnMarker: {1}".Fmt(defaultValue("AddReturnMarker"), Config.AddReturnMarker)); sb.AppendLine("{0}AddDescriptionAsComments: {1}".Fmt(defaultValue("AddDescriptionAsComments"), Config.AddDescriptionAsComments)); sb.AppendLine("{0}AddDataContractAttributes: {1}".Fmt(defaultValue("AddDataContractAttributes"), Config.AddDataContractAttributes)); sb.AppendLine("{0}AddIndexesToDataMembers: {1}".Fmt(defaultValue("AddIndexesToDataMembers"), Config.AddIndexesToDataMembers)); sb.AppendLine("{0}AddGeneratedCodeAttributes: {1}".Fmt(defaultValue("AddGeneratedCodeAttributes"), Config.AddGeneratedCodeAttributes)); sb.AppendLine("{0}AddResponseStatus: {1}".Fmt(defaultValue("AddResponseStatus"), Config.AddResponseStatus)); sb.AppendLine("{0}AddImplicitVersion: {1}".Fmt(defaultValue("AddImplicitVersion"), Config.AddImplicitVersion)); sb.AppendLine("{0}InitializeCollections: {1}".Fmt(defaultValue("InitializeCollections"), Config.InitializeCollections)); sb.AppendLine("{0}ExportValueTypes: {1}".Fmt(defaultValue("ExportValueTypes"), Config.ExportValueTypes)); sb.AppendLine("{0}IncludeTypes: {1}".Fmt(defaultValue("IncludeTypes"), Config.IncludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeTypes: {1}".Fmt(defaultValue("ExcludeTypes"), Config.ExcludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}AddNamespaces: {1}".Fmt(defaultValue("AddNamespaces"), Config.AddNamespaces.Safe().ToArray().Join(","))); sb.AppendLine("{0}AddDefaultXmlNamespace: {1}".Fmt(defaultValue("AddDefaultXmlNamespace"), Config.AddDefaultXmlNamespace)); sb.AppendLine("*/"); sb.AppendLine(); namespaces.Where(x => !string.IsNullOrEmpty(x)) .Each(x => sb.AppendLine($"using {x};")); if (Config.AddGeneratedCodeAttributes) { sb.AppendLine("using System.CodeDom.Compiler;"); } if (Config.AddDataContractAttributes && Config.AddDefaultXmlNamespace != null) { sb.AppendLine(); namespaces.Where(x => !Config.DefaultNamespaces.Contains(x)).ToList() .ForEach(x => sb.AppendLine( $"[assembly: ContractNamespace(\"{Config.AddDefaultXmlNamespace}\", ClrNamespace=\"{x}\")]")); } sb.AppendLine(); string lastNS = null; var existingTypes = new HashSet <string>(); var requestTypes = metadata.Operations.Select(x => x.Request).ToSet(); var requestTypesMap = metadata.Operations.ToSafeDictionary(x => x.Request); var responseTypes = metadata.Operations .Where(x => x.Response != null) .Select(x => x.Response).ToSet(); var types = metadata.Types.ToSet(); allTypes = new List <MetadataType>(); allTypes.AddRange(requestTypes); allTypes.AddRange(responseTypes); allTypes.AddRange(types); allTypes = FilterTypes(allTypes); var orderedTypes = allTypes .OrderBy(x => x.Namespace) .ThenBy(x => x.Name) .ToList(); var insertCode = InsertCodeFilter?.Invoke(orderedTypes, Config); if (insertCode != null) { sb.AppendLine(insertCode); } foreach (var type in orderedTypes) { var fullTypeName = type.GetFullName(); if (requestTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName)) { MetadataType response = null; if (requestTypesMap.TryGetValue(type, out var operation)) { response = operation.Response; } lastNS = AppendType(ref sb, type, lastNS, allTypes, new CreateTypeOptions { Routes = metadata.Operations.GetRoutes(type), ImplementsFn = () => { if (!Config.AddReturnMarker && operation?.ReturnsVoid != true && operation?.ReturnType == null) { return(null); } if (operation?.ReturnsVoid == true) { return(nameof(IReturnVoid)); } if (operation?.ReturnType != null) { return(Type("IReturn`1", new[] { Type(operation.ReturnType) })); } return(response != null ? Type("IReturn`1", new[] { Type(response.Name, response.GenericArgs) }) : null); }, IsRequest = true, }); existingTypes.Add(fullTypeName); } } else if (responseTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName) && !Config.IgnoreTypesInNamespaces.Contains(type.Namespace)) { lastNS = AppendType(ref sb, type, lastNS, allTypes, new CreateTypeOptions { IsResponse = true, }); existingTypes.Add(fullTypeName); } } else if (types.Contains(type) && !existingTypes.Contains(fullTypeName)) { lastNS = AppendType(ref sb, type, lastNS, allTypes, new CreateTypeOptions { IsType = true }); existingTypes.Add(fullTypeName); } } var addCode = AddCodeFilter?.Invoke(orderedTypes, Config); if (addCode != null) { sb.AppendLine(addCode); } if (lastNS != null) { sb.AppendLine("}"); } sb.AppendLine(); return(StringBuilderCache.ReturnAndFree(sbInner)); }
public string GetCode(MetadataTypes metadata, IRequest request) { var typeNamespaces = new HashSet <string>(); var includeList = RemoveIgnoredTypes(metadata); metadata.Types.Each(x => typeNamespaces.Add(x.Namespace)); metadata.Operations.Each(x => typeNamespaces.Add(x.Request.Namespace)); var defaultImports = !Config.DefaultImports.IsEmpty() ? Config.DefaultImports : DefaultImports; string DefaultValue(string k) => request.QueryString[k].IsNullOrEmpty() ? "//" : ""; var sbInner = StringBuilderCache.Allocate(); var sb = new StringBuilderWrapper(sbInner); var sbExt = new StringBuilderWrapper(new StringBuilder()); sb.AppendLine("/* Options:"); sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " "))); sb.AppendLine("SwiftVersion: 4.0"); sb.AppendLine("Version: {0}".Fmt(Env.VersionString)); sb.AppendLine("Tip: {0}".Fmt(HelpMessages.NativeTypesDtoOptionsTip.Fmt("//"))); sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl)); if (Config.UsePath != null) { sb.AppendLine("UsePath: {0}".Fmt(Config.UsePath)); } sb.AppendLine(); sb.AppendLine("{0}BaseClass: {1}".Fmt(DefaultValue("BaseClass"), Config.BaseClass)); sb.AppendLine("{0}AddModelExtensions: {1}".Fmt(DefaultValue("AddModelExtensions"), Config.AddModelExtensions)); sb.AppendLine("{0}AddServiceStackTypes: {1}".Fmt(DefaultValue("AddServiceStackTypes"), Config.AddServiceStackTypes)); sb.AppendLine("{0}IncludeTypes: {1}".Fmt(DefaultValue("IncludeTypes"), Config.IncludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeTypes: {1}".Fmt(DefaultValue("ExcludeTypes"), Config.ExcludeTypes.Safe().ToArray().Join(","))); sb.AppendLine("{0}ExcludeGenericBaseTypes: {1}".Fmt(DefaultValue("ExcludeGenericBaseTypes"), Config.ExcludeGenericBaseTypes)); sb.AppendLine("{0}AddResponseStatus: {1}".Fmt(DefaultValue("AddResponseStatus"), Config.AddResponseStatus)); sb.AppendLine("{0}AddImplicitVersion: {1}".Fmt(DefaultValue("AddImplicitVersion"), Config.AddImplicitVersion)); sb.AppendLine("{0}AddDescriptionAsComments: {1}".Fmt(DefaultValue("AddDescriptionAsComments"), Config.AddDescriptionAsComments)); sb.AppendLine("{0}InitializeCollections: {1}".Fmt(DefaultValue("InitializeCollections"), Config.InitializeCollections)); sb.AppendLine("{0}TreatTypesAsStrings: {1}".Fmt(DefaultValue("TreatTypesAsStrings"), Config.TreatTypesAsStrings.Safe().ToArray().Join(","))); sb.AppendLine("{0}DefaultImports: {1}".Fmt(DefaultValue("DefaultImports"), defaultImports.Join(","))); sb.AppendLine("*/"); sb.AppendLine(); foreach (var typeName in Config.TreatTypesAsStrings.Safe()) { TypeAliases[typeName] = "String"; } string lastNS = null; var existingTypes = new HashSet <string>(); var requestTypes = metadata.Operations.Select(x => x.Request).ToSet(); var requestTypesMap = metadata.Operations.ToSafeDictionary(x => x.Request); var responseTypes = metadata.Operations .Where(x => x.Response != null) .Select(x => x.Response).ToSet(); var types = metadata.Types.ToSet(); allTypes = new List <MetadataType>(); allTypes.AddRange(requestTypes); allTypes.AddRange(responseTypes); allTypes.AddRange(types); allTypes = FilterTypes(allTypes); //Swift doesn't support reusing same type name with different generic arity var conflictPartialNames = allTypes.Map(x => x.Name).Distinct() .GroupBy(g => g.LeftPart('`')) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); this.conflictTypeNames = allTypes .Where(x => conflictPartialNames.Any(name => x.Name.StartsWith(name))) .Map(x => x.Name); defaultImports.Each(x => sb.AppendLine($"import {x}")); var insertCode = InsertCodeFilter?.Invoke(allTypes, Config); if (insertCode != null) { sb.AppendLine(insertCode); } //ServiceStack core interfaces foreach (var type in allTypes) { var fullTypeName = type.GetFullName(); if (requestTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName)) { MetadataType response = null; if (requestTypesMap.TryGetValue(type, out var operation)) { response = operation.Response; } lastNS = AppendType(ref sb, ref sbExt, type, lastNS, new CreateTypeOptions { Routes = metadata.Operations.GetRoutes(type), ImplementsFn = () => { if (!Config.AddReturnMarker && operation?.ReturnsVoid != true && operation?.ReturnType == null) { return(null); } if (operation?.ReturnsVoid == true) { return(nameof(IReturnVoid)); } if (operation?.ReturnType != null) { return(Type("IReturn`1", new[] { Type(operation.ReturnType) })); } return(response != null ? Type("IReturn`1", new[] { Type(response.Name, response.GenericArgs) }) : null); }, IsRequest = true, }); existingTypes.Add(fullTypeName); } } else if (responseTypes.Contains(type)) { if (!existingTypes.Contains(fullTypeName) && !Config.IgnoreTypesInNamespaces.Contains(type.Namespace)) { lastNS = AppendType(ref sb, ref sbExt, type, lastNS, new CreateTypeOptions { IsResponse = true, }); existingTypes.Add(fullTypeName); } } else if (types.Contains(type) && !existingTypes.Contains(fullTypeName)) { lastNS = AppendType(ref sb, ref sbExt, type, lastNS, new CreateTypeOptions { IsType = true }); existingTypes.Add(fullTypeName); } } if (Config.AddModelExtensions) { sb.AppendLine(); sb.AppendLine(sbExt.ToString()); } var addCode = AddCodeFilter?.Invoke(allTypes, Config); if (addCode != null) { sb.AppendLine(addCode); } return(StringBuilderCache.ReturnAndFree(sbInner)); }