Beispiel #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}");
            }
        }
        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.Type, 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));

                    if (!fieldName.IsKeyWord())
                    {
                        sb.AppendLine(!initProp
                            ? "var {0}:{1}?{2}".Fmt(fieldName, propType, defaultValue)
                            : "var {0}:{1} = {1}()".Fmt(fieldName, propType, defaultValue));
                    }
                    else
                    {
                        var originalName = fieldName;
                        fieldName = char.ToUpper(fieldName[0]) + fieldName.SafeSubstring(1);
                        sb.AppendLine(!initProp
                            ? "@SerializedName(\"{0}\") var {1}:{2}?{3}".Fmt(originalName, fieldName, propType,
                                                                             defaultValue)
                            : "@SerializedName(\"{0}\") var {1}:{2} = {2}()".Fmt(originalName, fieldName, propType));
                    }
                }
            }

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

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine("var {0}:ResponseStatus?{1}".Fmt(typeof(ResponseStatus).Name.PropertyStyle(), defaultValue));
            }
        }
Beispiel #3
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.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()));
            }
        }
Beispiel #4
0
        private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections)
        {
            //Swift doesn't support extensions on same protocol used by Base and Sub types
            if (type.IsAbstract())
            {
                return;
            }

            var typeName = Type(type.Name, type.GenericArgs);

            var typeNameOnly = typeName.SplitOnFirst('<')[0];

            sbExt.AppendLine();
            sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();

            sbExt.AppendLine("public static var typeName:String {{ return \"{0}\" }}".Fmt(typeName));

            //func typeConfig()

            var isGenericType = !type.GenericArgs.IsEmpty();

            if (!isGenericType)
            {
                sbExt.AppendLine("public static var metadata = Metadata.create([");
                sbExt = sbExt.Indent();
            }
            else
            {
                //Swift 2.0 doesn't allow stored static properties on generic types yet
                sbExt.AppendLine("public static var metadata:Metadata {");
                sbExt = sbExt.Indent();
                sbExt.AppendLine("return Metadata.create([");
            }

            sbExt = sbExt.Indent();

            foreach (var prop in GetPropertes(type))
            {
                var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs);
                if (propType.IsInterface() ||
                    IgnorePropertyTypeNames.Contains(prop.Type) ||
                    IgnorePropertyNames.Contains(prop.Name))
                {
                    continue;
                }

                var fnName = "property";
                if (prop.IsArray() || ArrayTypes.Contains(prop.Type))
                {
                    fnName = initCollections
                        ? "arrayProperty"
                        : "optionalArrayProperty";
                }
                else if (DictionaryTypes.Contains(prop.Type))
                {
                    fnName = initCollections
                        ? "objectProperty"
                        : "optionalObjectProperty";
                }
                else
                {
                    if (propType != null && !propType.IsEnum.GetValueOrDefault())
                    {
                        fnName = "optionalObjectProperty";
                    }
                    else
                    {
                        fnName = "optionalProperty";
                    }
                }

                sbExt.AppendLine("Type<{0}>.{1}(\"{2}\", get: {{ $0.{2} }}, set: {{ $0.{2} = $1 }}),".Fmt(
                                     typeName, fnName, prop.Name.SafeToken().PropertyStyle()));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("])");
            sbExt = sbExt.UnIndent();

            if (isGenericType)
            {
                sbExt.AppendLine("}");
            }

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");
        }
Beispiel #5
0
        private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections)
        {
            //Swift doesn't support extensions on same protocol used by Base and Sub types
            if (type.IsAbstract())
            {
                return;
            }

            var typeName = Type(type.Name, type.GenericArgs);

            var typeNameOnly = typeName.SplitOnFirst('<')[0];

            sbExt.AppendLine();
            sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();

            sbExt.AppendLine("public class var typeName:String {{ return \"{0}\" }}".Fmt(typeName));

            //func typeConfig()
            sbExt.AppendLine("public class func reflect() -> Type<{0}> {{".Fmt(typeName));
            sbExt = sbExt.Indent();
            sbExt.AppendLine(
                "return TypeConfig.config() ?? TypeConfig.configure(Type<{0}>(".Fmt(typeName));
            sbExt = sbExt.Indent();

            sbExt.AppendLine("properties: [".Fmt(typeName));
            sbExt = sbExt.Indent();

            foreach (var prop in GetPropertes(type))
            {
                var propType = FindType(prop.Type, prop.TypeNamespace, prop.GenericArgs);
                if (propType.IsInterface() ||
                    IgnorePropertyTypeNames.Contains(prop.Type) ||
                    IgnorePropertyNames.Contains(prop.Name))
                {
                    continue;
                }

                var fnName = "property";
                if (prop.IsArray() || ArrayTypes.Contains(prop.Type))
                {
                    fnName = initCollections
                        ? "arrayProperty"
                        : "optionalArrayProperty";
                }
                else if (DictionaryTypes.Contains(prop.Type))
                {
                    fnName = initCollections
                        ? "objectProperty"
                        : "optionalObjectProperty";
                }
                else
                {
                    if (propType != null && !propType.IsEnum.GetValueOrDefault())
                    {
                        fnName = "optionalObjectProperty";
                    }
                    else
                    {
                        fnName = "optionalProperty";
                    }
                }

                sbExt.AppendLine("Type<{0}>.{1}(\"{2}\", get: {{ $0.{2} }}, set: {{ $0.{2} = $1 }}),".Fmt(
                                     typeName, fnName, prop.Name.SafeToken().PropertyStyle()));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("]))");

            sbExt = sbExt.UnIndent();
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");


            //toJson()
            sbExt.AppendLine("public func toJson() -> String {");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return {0}.reflect().toJson(self)".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromJson()
            sbExt.AppendLine("public class func fromJson(json:String) -> {0}? {{".Fmt(typeName));
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return {0}.reflect().fromJson({0}(), json: json)".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromObject()
            sbExt.AppendLine("public class func fromObject(any:AnyObject) -> {0}? {{".Fmt(typeName));
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return {0}.reflect().fromObject({0}(), any:any)".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //toString()
            sbExt.AppendLine("public func toString() -> String {");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return {0}.reflect().toString(self)".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromString()
            sbExt.AppendLine("public class func fromString(string:String) -> {0}? {{".Fmt(typeName));
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return {0}.reflect().fromString({0}(), string: string)".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");
        }
Beispiel #6
0
        public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool initCollections)
        {
            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);
                    var optional     = "";
                    var defaultValue = "";
                    if (propType.EndsWith("?"))
                    {
                        propType = propType.Substring(0, propType.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 = " = [:]";
                        }
                    }

                    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(), propType, optional));
                    }
                    else
                    {
                        sb.AppendLine("public var {0}:{1}{2}{3}".Fmt(
                                          prop.Name.SafeToken().PropertyStyle(), propType, optional, defaultValue));
                    }
                }
            }

            if (Config.AddResponseStatus &&
                (type.Properties == null ||
                 type.Properties.All(x => x.Name != "ResponseStatus")))
            {
                if (wasAdded)
                {
                    sb.AppendLine();
                }

                AppendDataMember(sb, null, dataMemberIndex++);
                sb.AppendLine("public var {0}:ResponseStatus".Fmt("ResponseStatus".PropertyStyle()));
            }
        }
Beispiel #7
0
        private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections)
        {
            var typeName = Type(type.Name, type.GenericArgs);

            var typeNameOnly = typeName.SplitOnFirst('<')[0];

            sbExt.AppendLine();
            sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();

            //func typeConfig()
            sbExt.AppendLine("public class func typeConfig() -> JsConfigType<{0}>".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine(
                "return JsConfig.typeConfig() ?? JsConfig.configure(JsConfigType<{0}>(".Fmt(typeName));
            sbExt = sbExt.Indent();

            sbExt.AppendLine("writers: [");
            sbExt = sbExt.Indent();
            foreach (var prop in type.Properties.Safe())
            {
                var isOptional = !(initCollections &&
                                   (prop.IsArray() ||
                                    (!prop.GenericArgs.IsEmpty() &&
                                     (ArrayTypes.Contains(prop.Type) || DictionaryTypes.Contains(prop.Type)))
                                   ));
                var fn = isOptional ? "setOptionalValue" : "setValue";

                sbExt.AppendLine("(\"{1}\", {{ (x:{0}, map:NSDictionary) in {2}(&x.{1}, map, \"{1}\") }}),".Fmt(
                                     typeName, prop.Name.SafeToken().PropertyStyle(), fn));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("],");

            sbExt.AppendLine("readers: [");
            sbExt = sbExt.Indent();
            foreach (var prop in type.Properties.Safe())
            {
                sbExt.AppendLine("(\"{1}\", {{ (x:{0}) in x.{1} as Any }}),".Fmt(
                                     typeName,
                                     prop.Name.SafeToken().PropertyStyle()));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("]");

            sbExt = sbExt.UnIndent();

            sbExt.AppendLine("))");

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //toJson()
            sbExt.AppendLine();
            sbExt.AppendLine("public func toJson() -> String");
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return serializeToJson(self, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromDictionary()
            sbExt.AppendLine();
            sbExt.AppendLine("public class func fromDictionary(map:NSDictionary) -> {0}".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return populate({0}(), map, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromJson()
            sbExt.AppendLine();
            sbExt.AppendLine("public class func fromJson(json:String) -> {0}".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return populate({0}(), json, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");
        }
Beispiel #8
0
        private void AddTypeExtension(ref StringBuilderWrapper sbExt, MetadataType type, bool initCollections)
        {
            //Swift doesn't support extensions on same protocol used by Base and Sub types
            if (Config.FlattenAbstractTypes && type.IsAbstract())
            {
                return;
            }

            var typeProperties = type.Properties.Safe().ToList();

            if (Config.FlattenAbstractTypes)
            {
                var baseType = FindType(type.Inherits);
                if (baseType != null && baseType.IsAbstract())
                {
                    typeProperties.InsertRange(0, baseType.Properties.Safe());
                }
            }

            var typeName = Type(type.Name, type.GenericArgs);

            var typeNameOnly = typeName.SplitOnFirst('<')[0];

            sbExt.AppendLine();
            sbExt.AppendLine("extension {0} : JsonSerializable".Fmt(typeNameOnly));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();

            //func typeConfig()
            sbExt.AppendLine("public class func typeConfig() -> JsConfigType<{0}>".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine(
                "return JsConfig.typeConfig() ?? JsConfig.configure(JsConfigType<{0}>(".Fmt(typeName));
            sbExt = sbExt.Indent();

            sbExt.AppendLine("writers: [");
            sbExt = sbExt.Indent();

            foreach (var prop in typeProperties)
            {
                var isOptional = !(initCollections &&
                                   (prop.IsArray() ||
                                    (!prop.GenericArgs.IsEmpty() &&
                                     (ArrayTypes.Contains(prop.Type) || DictionaryTypes.Contains(prop.Type)))
                                   ));
                var fn = isOptional ? "setOptionalValue" : "setValue";

                sbExt.AppendLine("(\"{1}\", {{ (x:{0}, map:NSDictionary) in {2}(&x.{1}, map, \"{1}\") }}),".Fmt(
                                     typeName, prop.Name.SafeToken().PropertyStyle(), fn));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("],");

            sbExt.AppendLine("readers: [");
            sbExt = sbExt.Indent();
            foreach (var prop in typeProperties)
            {
                sbExt.AppendLine("(\"{1}\", Type<{0}>.value {{ $0.{1} }}),".Fmt(
                                     typeName,
                                     prop.Name.SafeToken().PropertyStyle()));
            }
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("]");

            sbExt = sbExt.UnIndent();

            sbExt.AppendLine("))");

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //toJson()
            sbExt.AppendLine();
            sbExt.AppendLine("public func toJson() -> String");
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return serializeToJson(self, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromDictionary()
            sbExt.AppendLine();
            sbExt.AppendLine("public class func fromDictionary(map:NSDictionary) -> {0}".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return populate({0}(), map, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            //fromJson()
            sbExt.AppendLine();
            sbExt.AppendLine("public class func fromJson(json:String) -> {0}".Fmt(typeName));
            sbExt.AppendLine("{");
            sbExt = sbExt.Indent();
            sbExt.AppendLine("return populate({0}(), json, {0}.typeConfig())".Fmt(typeName));
            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");

            sbExt = sbExt.UnIndent();
            sbExt.AppendLine("}");
        }
Beispiel #9
0
 /// <summary>
 /// Check if the type is a dictionary.
 /// </summary>
 public static bool IsDictionary(Type type)
 {
     return(DictionaryTypes.Contains(type));
 }