Beispiel #1
0
        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:={MetadataExtensions.QuotedSafeValue(dmMeta.Name)}";
                }

                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);
        }
Beispiel #2
0
        private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string lastNS, List <MetadataType> allTypes, CreateTypeOptions options)
        {
            if (type.IsNested.GetValueOrDefault() && !options.IsNestedType)
            {
                return(lastNS);
            }

            var ns = Config.GlobalNamespace ?? type.Namespace;

            if (ns != lastNS)
            {
                if (lastNS != null)
                {
                    sb.AppendLine("End Namespace");
                }

                lastNS = ns;

                sb.AppendLine();
                sb.AppendLine($"Namespace {MetadataExtensions.SafeToken(ns)}");
                //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}\")>");
            }

            PreTypeFilter?.Invoke(sb, type);

            if (type.IsEnum.GetValueOrDefault())
            {
                sb.AppendLine("Public Enum {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?[i];
                        if (KeyWords.Contains(name))
                        {
                            name = $"[{name}]";
                        }

                        sb.AppendLine(value == null
                            ? name
                            : $"{name} = {value}");
                    }
                }

                sb = sb.UnIndent();
                sb.AppendLine("End Enum");
            }
            else
            {
                var partial = Config.MakePartial && !type.IsInterface() ? "Partial " : "";
                var defType = type.IsInterface() ? "Interface" : "Class";
                sb.AppendLine("Public {0}{1} {2}".Fmt(partial, defType, Type(type.Name, type.GenericArgs)));

                //: BaseClass, Interfaces
                if (type.Inherits != null)
                {
                    sb.AppendLine($"    Inherits {Type(type.Inherits, includeNested: true)}");
                }

                var implements = new List <string>();
                if (options.ImplementsFn != null)
                {
                    var implStr = options.ImplementsFn();
                    if (!string.IsNullOrEmpty(implStr))
                    {
                        implements.Add(implStr);
                    }
                }

                type.Implements.Each(x => implements.Add(Type(x)));

                var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;
                if (makeExtensible)
                {
                    implements.Add("IExtensibleDataObject");
                }

                if (implements.Count > 0)
                {
                    foreach (var x in implements)
                    {
                        sb.AppendLine($"    Implements {x}");
                    }
                }

                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(type.IsInterface() ? "End Interface" : "End Class");
            }

            PostTypeFilter?.Invoke(sb, type);

            sb = sb.UnIndent();
            return(lastNS);
        }