本文整理汇总了C#中System.Text.StringBuilderWrapper类的典型用法代码示例。如果您正苦于以下问题:C# StringBuilderWrapper类的具体用法?C# StringBuilderWrapper怎么用?C# StringBuilderWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringBuilderWrapper类属于System.Text命名空间,在下文中一共展示了StringBuilderWrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddProperties
public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool includeResponseStatus)
{
var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
var wasAdded = false;
var dataMemberIndex = 1;
if (type.Properties != null)
{
foreach (var prop in type.Properties)
{
if (wasAdded) sb.AppendLine();
var propType = Type(prop.Type, prop.GenericArgs);
wasAdded = AppendComments(sb, prop.Description);
wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;
if (!type.IsInterface())
{
sb.AppendLine("member val {1}:{0} = {2} with get,set".Fmt(
propType, prop.Name.SafeToken(), GetDefaultLiteral(prop, type)));
}
else
{
sb.AppendLine("abstract {1}:{0} with get,set".Fmt(
propType, prop.Name.SafeToken()));
}
}
}
if (type.IsInterface())
return;
if (includeResponseStatus)
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
AppendDataMember(sb, null, dataMemberIndex++);
sb.AppendLine("member val ResponseStatus:ResponseStatus = null with get,set");
}
if (makeExtensible
&& (type.Properties == null
|| type.Properties.All(x => x.Name != "ExtensionData")))
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
sb.AppendLine("member val ExtensionData:ExtensionDataObject = null with get,set");
}
}
示例2: AddProperties
public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool includeResponseStatus)
{
var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
var @virtual = Config.MakeVirtual && !type.IsInterface() ? "Overridable " : "";
var wasAdded = false;
var dataMemberIndex = 1;
if (type.Properties != null)
{
foreach (var prop in type.Properties)
{
if (wasAdded) sb.AppendLine();
var propType = Type(prop.Type, prop.GenericArgs, includeNested:true);
wasAdded = AppendComments(sb, prop.Description);
wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;
var visibility = type.IsInterface() ? "" : "Public ";
sb.AppendLine("{0}{1}Property {2} As {3}".Fmt(
visibility,
@virtual,
EscapeKeyword(prop.Name).SafeToken(),
propType));
}
}
if (type.IsInterface())
return;
if (includeResponseStatus)
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
wasAdded = AppendDataMember(sb, null, dataMemberIndex++);
sb.AppendLine("Public {0}Property ResponseStatus As ResponseStatus".Fmt(@virtual));
}
if (makeExtensible
&& (type.Properties == null
|| type.Properties.All(x => x.Name != "ExtensionData")))
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
sb.AppendLine("Public {0}Property ExtensionData As ExtensionDataObject Implements IExtensibleDataObject.ExtensionData".Fmt(@virtual));
}
}
示例3: AddProperties
public void AddProperties(StringBuilderWrapper sb, MetadataType type)
{
var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
var @virtual = Config.MakeVirtual ? "virtual " : "";
var wasAdded = false;
var dataMemberIndex = 1;
if (type.Properties != null)
{
foreach (var prop in type.Properties)
{
if (wasAdded) sb.AppendLine();
var propType = Type(prop.Type, prop.GenericArgs);
wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++);
wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;
sb.AppendLine("public {0}{1} {2} {{ get; set; }}".Fmt(@virtual, propType, prop.Name.SafeToken()));
}
}
if (Config.AddResponseStatus
&& (type.Properties == null
|| type.Properties.All(x => x.Name != "ResponseStatus")))
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
AppendDataMember(sb, null, dataMemberIndex++);
sb.AppendLine("public {0}ResponseStatus ResponseStatus {{ get; set; }}".Fmt(@virtual));
}
if (makeExtensible
&& (type.Properties == null
|| type.Properties.All(x => x.Name != "ExtensionData")))
{
if (wasAdded) sb.AppendLine();
wasAdded = true;
sb.AppendLine("public {0}ExtensionDataObject ExtensionData {{ get; set; }}".Fmt(@virtual));
}
}
示例4: GetCode
public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMetadata nativeTypes)
{
var typeNamespaces = new HashSet<string>();
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))
{
if (!defaultImports.Contains(GSonAnnotationsNamespace))
defaultImports.Add(GSonAnnotationsNamespace);
if (!defaultImports.Contains(GSonReflectNamespace))
defaultImports.Add(GSonReflectNamespace);
}
var defaultNamespace = Config.GlobalNamespace ?? DefaultGlobalNamespace;
Func<string, string> defaultValue = 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.AddRange(requestTypes);
AllTypes.AddRange(responseTypes);
AllTypes.AddRange(types);
//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();
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))
{
//.........这里部分代码省略.........
示例5: AppendDataContract
public void AppendDataContract(StringBuilderWrapper sb, MetadataDataContract dcMeta)
{
if (dcMeta == null)
{
if (Config.AddDataContractAttributes)
sb.AppendLine("@DataContract()");
return;
}
var dcArgs = "";
if (dcMeta.Name != null || dcMeta.Namespace != null)
{
if (dcMeta.Name != null)
dcArgs = "Name={0}".Fmt(dcMeta.Name.QuotedSafeValue());
if (dcMeta.Namespace != null)
{
if (dcArgs.Length > 0)
dcArgs += ", ";
dcArgs += "Namespace={0}".Fmt(dcMeta.Namespace.QuotedSafeValue());
}
dcArgs = "({0})".Fmt(dcArgs);
}
sb.AppendLine("@DataContract{0}".Fmt(dcArgs));
}
示例6: AppendAttributes
public bool AppendAttributes(StringBuilderWrapper sb, List<MetadataAttribute> attributes)
{
if (attributes == null || attributes.Count == 0) return false;
var existingAttrs = new HashSet<string>();
foreach (var attr in attributes)
{
//Java 7 doesn't allow repeating attrs
var prefix = existingAttrs.Contains(attr.Name)
? "// "
: "";
existingAttrs.Add(attr.Name);
if ((attr.Args == null || attr.Args.Count == 0)
&& (attr.ConstructorArgs == null || attr.ConstructorArgs.Count == 0))
{
sb.AppendLine(prefix + "@{0}()".Fmt(attr.Name));
}
else
{
var args = StringBuilderCacheAlt.Allocate();
if (attr.ConstructorArgs != null)
{
if (attr.ConstructorArgs.Count > 1)
prefix = "// ";
foreach (var ctorArg in attr.ConstructorArgs)
{
if (args.Length > 0)
args.Append(", ");
args.Append("{0}".Fmt(TypeValue(ctorArg.Type, ctorArg.Value)));
}
}
else if (attr.Args != null)
{
foreach (var attrArg in attr.Args)
{
if (args.Length > 0)
args.Append(", ");
args.Append("{0}={1}".Fmt(attrArg.Name, TypeValue(attrArg.Type, attrArg.Value)));
}
}
sb.AppendLine(prefix + "@{0}({1})".Fmt(attr.Name, StringBuilderCacheAlt.ReturnAndFree(args)));
}
}
return true;
}
示例7: AppendComments
public void AppendComments(StringBuilderWrapper sb, string desc)
{
if (desc == null) return;
sb.AppendLine("/**");
sb.AppendLine("* {0}".Fmt(desc.SafeComment()));
sb.AppendLine("*/");
}
示例8: AddProperties
public void AddProperties(StringBuilderWrapper sb, MetadataType type,
bool initCollections, bool includeResponseStatus)
{
var wasAdded = false;
var dataMemberIndex = 1;
foreach (var prop in type.Properties.Safe())
{
if (wasAdded) sb.AppendLine();
var propTypeName = Type(prop.Type, prop.GenericArgs);
var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs);
var optional = "";
var defaultValue = "";
if (propTypeName.EndsWith("?"))
{
propTypeName = propTypeName.Substring(0, propTypeName.Length - 1);
optional = "?";
}
if (Config.MakePropertiesOptional)
{
optional = "?";
}
if (prop.Attributes.Safe().FirstOrDefault(x => x.Name == "Required") != null)
{
optional = "?"; //always use optional
}
if (prop.IsArray())
{
optional = "";
defaultValue = " = []";
}
else if (initCollections && !prop.GenericArgs.IsEmpty())
{
if (ArrayTypes.Contains(prop.Type))
{
optional = "";
defaultValue = " = []";
}
if (DictionaryTypes.Contains(prop.Type))
{
optional = "";
defaultValue = " = [:]";
}
}
if (propType.IsInterface() || IgnorePropertyNames.Contains(prop.Name))
{
sb.AppendLine("//{0}:{1} ignored. Swift doesn't support interface properties"
.Fmt(prop.Name.SafeToken().PropertyStyle(), propTypeName));
continue;
}
else if (IgnorePropertyTypeNames.Contains(propTypeName))
{
sb.AppendLine("//{0}:{1} ignored. Type could not be extended in Swift"
.Fmt(prop.Name.SafeToken().PropertyStyle(), propTypeName));
continue;
}
wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++);
wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;
if (type.IsInterface())
{
sb.AppendLine("var {0}:{1}{2} {{ get set }}".Fmt(
prop.Name.SafeToken().PropertyStyle(), propTypeName, optional));
}
else
{
sb.AppendLine("public var {0}:{1}{2}{3}".Fmt(
prop.Name.SafeToken().PropertyStyle(), propTypeName, optional, defaultValue));
}
}
if (includeResponseStatus)
{
if (wasAdded) sb.AppendLine();
AppendDataMember(sb, null, dataMemberIndex++);
sb.AppendLine("public var {0}:ResponseStatus?".Fmt(typeof(ResponseStatus).Name.PropertyStyle()));
}
}
示例9: AddEnumExtension
private void AddEnumExtension(ref StringBuilderWrapper sbExt, MetadataType type)
{
if (type.EnumNames == null) return;
sbExt.AppendLine();
var typeName = Type(type.Name, type.GenericArgs);
sbExt.AppendLine("extension {0} : StringSerializable".Fmt(typeName));
sbExt.AppendLine("{");
sbExt = sbExt.Indent();
sbExt.AppendLine("public static var typeName:String {{ return \"{0}\" }}".Fmt(typeName));
//toJson()
sbExt.AppendLine("public func toJson() -> String {");
sbExt = sbExt.Indent();
sbExt.AppendLine("return jsonStringRaw(toString())");
sbExt = sbExt.UnIndent();
sbExt.AppendLine("}");
//toString()
sbExt.AppendLine("public func toString() -> String {");
sbExt = sbExt.Indent();
sbExt.AppendLine("switch self {");
foreach (var name in type.EnumNames)
{
sbExt.AppendLine("case .{0}: return \"{0}\"".Fmt(name));
}
sbExt.AppendLine("}");
sbExt = sbExt.UnIndent();
sbExt.AppendLine("}");
//fromString()
sbExt.AppendLine("public static func fromString(strValue:String) -> {0}? {{".Fmt(typeName));
sbExt = sbExt.Indent();
sbExt.AppendLine("switch strValue {");
foreach (var name in type.EnumNames)
{
sbExt.AppendLine("case \"{0}\": return .{0}".Fmt(name));
}
sbExt.AppendLine("default: return nil");
sbExt.AppendLine("}");
sbExt = sbExt.UnIndent();
sbExt.AppendLine("}");
//fromObject()
sbExt.AppendLine("public static func fromObject(any:AnyObject) -> {0}? {{".Fmt(typeName));
sbExt = sbExt.Indent();
sbExt.AppendLine("switch any {");
sbExt.AppendLine("case let i as Int: return {0}(rawValue: i)".Fmt(typeName));
sbExt.AppendLine("case let s as String: return fromString(s)");
sbExt.AppendLine("default: return nil");
sbExt.AppendLine("}");
sbExt = sbExt.UnIndent();
sbExt.AppendLine("}");
sbExt = sbExt.UnIndent();
sbExt.AppendLine("}");
}
示例10: AppendType
private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string lastNS,
CreateTypeOptions options)
{
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 (type.IsEnum.GetValueOrDefault())
{
sb.AppendLine("enum {0}".Fmt(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 != null ? type.EnumValues[i] : null;
sb.AppendLine(value == null
? "{0},".Fmt(name.PropertyStyle())
: "{0} = {1},".Fmt(name.PropertyStyle(), value));
}
}
sb = sb.UnIndent();
sb.AppendLine("}");
}
else
{
var extends = new List<string>();
//: BaseClass, Interfaces
if (type.Inherits != null)
extends.Add(Type(type.Inherits).InheritedType());
if (options.ImplementsFn != null)
{
var implStr = options.ImplementsFn();
if (!string.IsNullOrEmpty(implStr))
extends.Add(implStr);
}
var extend = extends.Count > 0
? " extends " + (string.Join(", ", extends.ToArray()))
: "";
sb.AppendLine("interface {0}{1}".Fmt(Type(type.Name, type.GenericArgs), extend));
sb.AppendLine("{");
sb = sb.Indent();
var addVersionInfo = Config.AddImplicitVersion != null && options.IsRequest;
if (addVersionInfo)
{
sb.AppendLine("{0}?: number; //{1}".Fmt("Version".PropertyStyle(), Config.AddImplicitVersion));
}
AddProperties(sb, type,
includeResponseStatus: Config.AddResponseStatus && options.IsResponse
&& type.Properties.Safe().All(x => x.Name != typeof(ResponseStatus).Name));
sb = sb.UnIndent();
sb.AppendLine("}");
}
sb = sb.UnIndent();
return lastNS;
}
示例11: AppendType
private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string lastNS,
CreateTypeOptions options)
{
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 (type.IsEnum.GetValueOrDefault())
{
if (type.IsEnumInt.GetValueOrDefault() || type.EnumNames.IsEmpty())
{
var typeDeclaration = !Config.ExportAsTypes
? "enum"
: "export enum";
sb.AppendLine("{0} {1}".Fmt(typeDeclaration, 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 != null ? type.EnumValues[i] : null;
sb.AppendLine(value == null //Enum Value's are not impacted by JS Style
? "{0},".Fmt(name)
: "{0} = {1},".Fmt(name, value));
}
}
sb = sb.UnIndent();
sb.AppendLine("}");
}
else
{
var sbType = StringBuilderCache.Allocate();
var typeDeclaration = !Config.ExportAsTypes
? "type"
: "export type";
sbType.Append("{0} {1} = ".Fmt(typeDeclaration, Type(type.Name, type.GenericArgs)));
for (var i = 0; i < type.EnumNames.Count; i++)
{
if (i > 0)
sbType.Append(" | ");
sbType.Append('"').Append(type.EnumNames[i]).Append('"');
}
sbType.Append(";");
sb.AppendLine(StringBuilderCache.ReturnAndFree(sbType));
}
}
else
{
var extends = new List<string>();
//: BaseClass, Interfaces
if (type.Inherits != null)
extends.Add(Type(type.Inherits).InDeclarationType());
string responseTypeExpression = null;
var interfaces = new List<string>();
var implStr = options.ImplementsFn?.Invoke();
if (!string.IsNullOrEmpty(implStr))
{
interfaces.Add(implStr);
if (implStr.StartsWith("IReturn<"))
{
var types = implStr.RightPart('<');
var returnType = types.Substring(0, types.Length - 1);
if (returnType == "any")
returnType = "Object";
// This is to avoid invalid syntax such as "return new string()"
string replaceReturnType;
if (primitiveDefaultValues.TryGetValue(returnType, out replaceReturnType))
returnType = replaceReturnType;
responseTypeExpression = replaceReturnType == null ?
"createResponse() {{ return new {0}(); }}".Fmt(returnType) :
"createResponse() {{ return {0}; }}".Fmt(returnType);
}
else if (implStr == "IReturnVoid")
{
responseTypeExpression = "createResponse() {}";
}
//.........这里部分代码省略.........
示例12: GetCode
public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMetadata nativeTypes)
{
var typeNamespaces = new HashSet<string>();
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;
Func<string, string> defaultValue = 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}GlobalNamespace: {1}".Fmt(defaultValue("GlobalNamespace"), Config.GlobalNamespace));
//sb.AppendLine("{0}ExportAsTypes: {1}".Fmt(defaultValue("ExportAsTypes"), Config.ExportAsTypes));
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();
// Base Types need to be written first
var types = CreateSortedTypeList(metadata.Types);
allTypes = new List<MetadataType>();
allTypes.AddRange(types);
allTypes.AddRange(responseTypes);
allTypes.AddRange(requestTypes);
allTypes.RemoveAll(x => x.IgnoreType(Config));
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)));
if (!string.IsNullOrEmpty(globalNamespace))
{
var moduleDef = Config.ExportAsTypes ? "" : "declare ";
sb.AppendLine();
sb.AppendLine("{0}module {1}".Fmt(moduleDef, globalNamespace.SafeToken()));
sb.AppendLine("{");
sb = sb.Indent();
}
//ServiceStack core interfaces
foreach (var type in allTypes)
{
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
{
//.........这里部分代码省略.........
示例13: GetCode
public string GetCode(MetadataTypes metadata, IRequest request)
{
var namespaces = new HashSet<string>();
Config.DefaultNamespaces.Each(x => namespaces.Add(x));
var typeNamespaces = new HashSet<string>();
metadata.Types.Each(x => typeNamespaces.Add(x.Namespace));
metadata.Operations.Each(x => typeNamespaces.Add(x.Request.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 sb = new StringBuilderWrapper(new StringBuilder());
sb.AppendLine("(* Options:");
sb.AppendLine("Date: {0}".Fmt(DateTime.Now.ToString("s").Replace("T", " ")));
sb.AppendLine("Version: {0}".Fmt(metadata.Version));
sb.AppendLine("BaseUrl: {0}".Fmt(Config.BaseUrl));
sb.AppendLine();
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}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}AddDefaultXmlNamespace: {1}".Fmt(defaultValue("AddDefaultXmlNamespace"), Config.AddDefaultXmlNamespace));
//sb.AppendLine("{0}DefaultNamespaces: {1}".Fmt(defaultValue("DefaultNamespaces"), Config.DefaultNamespaces.ToArray().Join(", ")));
sb.AppendLine("*)");
sb.AppendLine();
//if (Config.AddDataContractAttributes
// && Config.AddDefaultXmlNamespace != null)
//{
// sb.AppendLine();
// var list = namespaces.Where(x => !Config.DefaultNamespaces.Contains(x)).ToList();
// list.ForEach(x =>
// sb.AppendLine("[<assembly: ContractNamespace(\"{0}\", ClrNamespace=\"{1}\")>]"
// .Fmt(Config.AddDefaultXmlNamespace, x)));
// if (list.Count > 0)
// {
// sb.AppendLine("do()"); //http://scottseely.com/2009/01/23/f-assembly-level-attributes-assemblyinfo-fs-and-do/
// }
//}
//sb.AppendLine();
string lastNS = null;
var existingOps = 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();
var allTypes = new List<MetadataType>();
allTypes.AddRange(types);
allTypes.AddRange(responseTypes);
allTypes.AddRange(requestTypes);
var orderedTypes = allTypes.OrderTypesByDeps();
sb.AppendLine("namespace {0}".Fmt(globalNamespace.SafeToken()));
sb.AppendLine();
foreach (var ns in namespaces)
{
sb.AppendLine("open " + ns);
}
foreach (var type in orderedTypes)
{
var fullTypeName = type.GetFullName();
if (requestTypes.Contains(type))
{
if (!existingOps.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
//.........这里部分代码省略.........
示例14: AppendType
private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string lastNS,
CreateTypeOptions options)
{
if (type == null || (type.Namespace != null && type.Namespace.StartsWith("System")))
return lastNS;
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 (type.IsEnum.GetValueOrDefault())
{
sb.AppendLine("type {0} =".Fmt(Type(type.Name, type.GenericArgs)));
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 != null ? type.EnumValues[i] : i.ToString();
sb.AppendLine("| {0} = {1}".Fmt(name, value));
}
}
sb = sb.UnIndent();
}
else
{
//sb.AppendLine("[<CLIMutable>]"); // only for Record Types
sb.AppendLine("[<AllowNullLiteral>]");
sb.AppendLine("type {0}() = ".Fmt(Type(type.Name, type.GenericArgs)));
sb = sb.Indent();
var startLen = sb.Length;
//: BaseClass, Interfaces
if (type.Inherits != null)
sb.AppendLine("inherit {0}()".Fmt(Type(type.Inherits)));
if (options.ImplementsFn != null)
{
var implStr = options.ImplementsFn();
if (!string.IsNullOrEmpty(implStr))
sb.AppendLine("interface {0}".Fmt(implStr));
}
var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
if (makeExtensible)
{
sb.AppendLine("interface IExtensibleDataObject with");
sb.AppendLine(" member val ExtensionData:ExtensionDataObject = null with get, set");
sb.AppendLine("end");
}
var addVersionInfo = Config.AddImplicitVersion != null && options.IsOperation;
if (addVersionInfo)
{
sb.AppendLine("member val Version:int = {0} with get, set".Fmt(Config.AddImplicitVersion));
}
AddProperties(sb, type);
if (sb.Length == startLen)
sb.AppendLine("class end");
sb = sb.UnIndent();
}
sb = sb.UnIndent();
return lastNS;
}
示例15: AppendDataMember
public bool AppendDataMember(StringBuilderWrapper sb, MetadataDataMember dmMeta, int dataMemberIndex)
{
if (dmMeta == null)
{
if (Config.AddDataContractAttributes)
{
sb.AppendLine(Config.AddIndexesToDataMembers
? $"[DataMember(Order={dataMemberIndex})]"
: "[DataMember]");
return true;
}
return false;
}
var dmArgs = "";
if (dmMeta.Name != null
|| dmMeta.Order != null
|| dmMeta.IsRequired != null
|| dmMeta.EmitDefaultValue != null
|| Config.AddIndexesToDataMembers)
{
if (dmMeta.Name != null)
dmArgs = $"Name={dmMeta.Name.QuotedSafeValue()}";
if (dmMeta.Order != null || Config.AddIndexesToDataMembers)
{
if (dmArgs.Length > 0)
dmArgs += ", ";
dmArgs += $"Order={dmMeta.Order ?? dataMemberIndex}";
}
if (dmMeta.IsRequired != null)
{
if (dmArgs.Length > 0)
dmArgs += ", ";
dmArgs += $"IsRequired={dmMeta.IsRequired.ToString().ToLower()}";
}
if (dmMeta.EmitDefaultValue != null)
{
if (dmArgs.Length > 0)
dmArgs += ", ";
dmArgs += $"EmitDefaultValue={dmMeta.EmitDefaultValue.ToString().ToLower()}";
}
dmArgs = $"({dmArgs})";
}
sb.AppendLine($"[DataMember{dmArgs}]");
return true;
}