Ejemplo n.º 1
0
        public void AddProperties(StringBuilderWrapper sb, MetadataType type,
                                  bool initCollections, bool includeResponseStatus)
        {
            var wasAdded = false;

            var defaultValue = type.IsInterface != true ? " = null" : "";

            var dataMemberIndex = 1;

            if (type.Properties != null)
            {
                foreach (var prop in type.Properties)
                {
                    if (wasAdded)
                    {
                        sb.AppendLine();
                    }

                    var propType = Type(prop.GetTypeName(Config, allTypes), prop.GenericArgs);

                    var fieldName = prop.Name.SafeToken().PropertyStyle();

                    wasAdded = AppendComments(sb, prop.Description);
                    wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
                    wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;

                    var initProp = initCollections && !prop.GenericArgs.IsEmpty() &&
                                   (ArrayTypes.Contains(prop.Type) || DictionaryTypes.Contains(prop.Type));

                    sb.Emit(prop, Lang.Kotlin);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    if (!fieldName.IsKeyWord())
                    {
                        sb.AppendLine(!initProp
                            ? $"var {fieldName}:{propType}?{defaultValue}"
                            : $"var {fieldName}:{propType} = {propType}()");
                    }
                    else
                    {
                        var originalName = fieldName;
                        fieldName = char.ToUpper(fieldName[0]) + fieldName.SafeSubstring(1);
                        sb.AppendLine(!initProp
                            ? $"@SerializedName(\"{originalName}\") var {fieldName}:{propType}?{defaultValue}"
                            : $"@SerializedName(\"{originalName}\") var {fieldName}:{propType} = {propType}()");
                    }
                    PostPropertyFilter?.Invoke(sb, prop, type);
                }
            }

            if (includeResponseStatus)
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine($"var {nameof(ResponseStatus).PropertyStyle()}:ResponseStatus?{defaultValue}");
            }
        }
Ejemplo n.º 2
0
        public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool includeResponseStatus)
        {
            var wasAdded = false;
            var isClass  = Config.ExportAsTypes && !type.IsInterface.GetValueOrDefault();
            var modifier = isClass ? "public " : "";

            var dataMemberIndex = 1;

            if (type.Properties != null)
            {
                foreach (var prop in type.Properties)
                {
                    if (wasAdded)
                    {
                        sb.AppendLine();
                    }

                    var propType = GetPropertyType(prop, out var optionalProperty);
                    propType = PropertyTypeFilter?.Invoke(this, type, prop) ?? propType;

                    var optional = IsPropertyOptional(this, type, prop) ?? optionalProperty
                        ? "?"
                        : "";

                    wasAdded = AppendComments(sb, prop.Description);
                    wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
                    wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;

                    sb.Emit(prop, Lang.TypeScript);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    sb.AppendLine(modifier + "{1}{2}: {0};".Fmt(propType, prop.Name.SafeToken().PropertyStyle(), optional));
                    PostPropertyFilter?.Invoke(sb, prop, type);
                }
            }

            if (includeResponseStatus)
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine(modifier + "{0}{1}: ResponseStatus;".Fmt(
                                  nameof(ResponseStatus).PropertyStyle(), Config.ExportAsTypes ? "" : "?"));
            }
        }
Ejemplo n.º 3
0
        public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool includeResponseStatus)
        {
            var makeExtensible = Config.MakeDataContractsExtensible && type.Inherits == null;

            var virt     = Config.MakeVirtual && !type.IsInterface() ? "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.GetTypeName(Config, allTypes), 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.Emit(prop, Lang.CSharp);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    sb.AppendLine($"{visibility}{virt}{propType} {prop.Name.SafeToken()} {{ get; set; }}");
                    PostPropertyFilter?.Invoke(sb, prop, type);
                }
            }

            if (type.IsInterface())
            {
                return;
            }

            if (includeResponseStatus)
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }
                wasAdded = true;

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine($"public {virt}ResponseStatus ResponseStatus {{ get; set; }}");
            }

            if (makeExtensible &&
                (type.Properties == null ||
                 type.Properties.All(x => x.Name != "ExtensionData")))
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }
                wasAdded = true;

                sb.AppendLine($"public {virt}ExtensionDataObject ExtensionData {{ get; set; }}");
            }
        }
Ejemplo n.º 4
0
        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.GetTypeName(Config, allTypes), prop.GenericArgs);
                    wasAdded = AppendComments(sb, prop.Description);
                    wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
                    wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;

                    sb.Emit(prop, Lang.FSharp);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    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()));
                    }
                    PostPropertyFilter?.Invoke(sb, prop, type);
                }
            }

            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");
            }
        }
Ejemplo n.º 5
0
        public void AddProperties(StringBuilderWrapper sb, MetadataType type,
                                  bool includeResponseStatus,
                                  bool addPropertyAccessors,
                                  string settersReturnType)
        {
            var wasAdded = false;

            var sbInner     = StringBuilderCacheAlt.Allocate();
            var sbAccessors = new StringBuilderWrapper(sbInner);

            if (addPropertyAccessors)
            {
                sbAccessors.AppendLine();
                sbAccessors = sbAccessors.Indent().Indent();
            }

            var dataMemberIndex = 1;

            if (type.Properties != null)
            {
                foreach (var prop in type.Properties)
                {
                    if (wasAdded)
                    {
                        sb.AppendLine();
                    }

                    var propType = Type(prop.GetTypeName(Config, allTypes), prop.GenericArgs);

                    var fieldName    = prop.Name.SafeToken().PropertyStyle();
                    var accessorName = fieldName.ToPascalCase();

                    wasAdded = AppendComments(sb, prop.Description);
                    wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
                    wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;

                    sb.Emit(prop, Lang.Java);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    if (!fieldName.IsKeyWord())
                    {
                        sb.AppendLine("public {0} {1} = null;".Fmt(propType, fieldName));
                    }
                    else
                    {
                        var originalName = fieldName;
                        fieldName = char.ToUpper(fieldName[0]) + fieldName.SafeSubstring(1);
                        sb.AppendLine("@SerializedName(\"{0}\") public {1} {2} = null;".Fmt(originalName, propType, fieldName));
                    }
                    PostPropertyFilter?.Invoke(sb, prop, type);

                    if (addPropertyAccessors)
                    {
                        sbAccessors.AppendPropertyAccessor(propType, fieldName, accessorName, settersReturnType);
                    }
                }
            }

            if (includeResponseStatus)
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine("public ResponseStatus {0} = null;".Fmt(nameof(ResponseStatus).PropertyStyle()));

                if (addPropertyAccessors)
                {
                    sbAccessors.AppendPropertyAccessor("ResponseStatus", "ResponseStatus", settersReturnType);
                }
            }

            if (sbAccessors.Length > 0)
            {
                sb.AppendLine(StringBuilderCacheAlt.ReturnAndFree(sbInner).TrimEnd()); //remove last \n
            }
        }
Ejemplo n.º 6
0
        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.GetTypeName(Config, allTypes), 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.Emit(prop, Lang.Vb);
                    PrePropertyFilter?.Invoke(sb, prop, type);
                    sb.AppendLine("{0}{1}Property {2} As {3}".Fmt(
                                      visibility,
                                      @virtual,
                                      EscapeKeyword(prop.Name).SafeToken(),
                                      propType));
                    PostPropertyFilter?.Invoke(sb, prop, type);
                }
            }

            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));
            }
        }
Ejemplo n.º 7
0
        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.GetTypeName(Config, allTypes), 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 = AppendComments(sb, prop.Description);
                wasAdded = AppendDataMember(sb, prop.DataMember, dataMemberIndex++) || wasAdded;
                wasAdded = AppendAttributes(sb, prop.Attributes) || wasAdded;

                sb.Emit(prop, Lang.Swift);
                PrePropertyFilter?.Invoke(sb, prop, type);
                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));
                }
                PostPropertyFilter?.Invoke(sb, prop, type);
            }

            if (includeResponseStatus)
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine("public var {0}:ResponseStatus?".Fmt(nameof(ResponseStatus).PropertyStyle()));
            }
        }