Example #1
0
        private void AddConstuctor(StringBuilderWrapper sb, MetadataType type, CreateTypeOptions options)
        {
            if (type.IsInterface())
            {
                return;
            }
            var initCollections = feature.ShouldInitializeCollections(type, Config.InitializeCollections);

            if (Config.AddImplicitVersion == null && !initCollections)
            {
                return;
            }

            var collectionProps = new List <MetadataPropertyType>();

            if (type.Properties != null && initCollections)
            {
                collectionProps = type.Properties.Where(x => x.IsCollection()).ToList();
            }

            var addVersionInfo = Config.AddImplicitVersion != null && options.IsRequest;

            if (!addVersionInfo && collectionProps.Count <= 0)
            {
                return;
            }

            if (addVersionInfo)
            {
                var @virtual = Config.MakeVirtual ? "Overridable " : "";
                sb.AppendLine("Public {0}Property Version As Integer".Fmt(@virtual));
                sb.AppendLine();
            }

            sb.AppendLine("Public Sub New()".Fmt(NameOnly(type.Name)));
            //sb.AppendLine("{");
            sb = sb.Indent();

            if (addVersionInfo)
            {
                sb.AppendLine("Version = {0}".Fmt(Config.AddImplicitVersion));
            }

            foreach (var prop in collectionProps)
            {
                var suffix = prop.IsArray() ? "{}" : "";
                sb.AppendLine("{0} = New {1}{2}".Fmt(
                                  prop.Name.SafeToken(),
                                  Type(prop.Type, prop.GenericArgs, includeNested: true),
                                  suffix));
            }

            sb = sb.UnIndent();
            sb.AppendLine("End Sub");
            sb.AppendLine();
        }
Example #2
0
        private void AddConstuctor(StringBuilderWrapper sb, MetadataType type, CreateTypeOptions options)
        {
            if (type.IsInterface())
            {
                return;
            }

            var initCollections = feature.ShouldInitializeCollections(type, Config.InitializeCollections);

            if (Config.AddImplicitVersion == null && !initCollections)
            {
                return;
            }

            var collectionProps = new List <MetadataPropertyType>();

            if (type.Properties != null && initCollections)
            {
                collectionProps = type.Properties.Where(x => x.IsCollection()).ToList();
            }

            var addVersionInfo = Config.AddImplicitVersion != null && options.IsRequest;

            if (!addVersionInfo && collectionProps.Count <= 0)
            {
                return;
            }

            if (addVersionInfo)
            {
                var virt = Config.MakeVirtual ? "virtual " : "";
                sb.AppendLine($"public {virt}int Version {{ get; set; }}");
                sb.AppendLine();
            }

            sb.AppendLine($"public {NameOnly(type.Name)}()");
            sb.AppendLine("{");
            sb = sb.Indent();

            if (addVersionInfo)
            {
                sb.AppendLine($"Version = {Config.AddImplicitVersion};");
            }

            foreach (var prop in collectionProps)
            {
                sb.AppendLine($"{prop.Name.SafeToken()} = new {Type(prop.GetTypeName(Config, allTypes), prop.GenericArgs,includeNested:true)}{{}};");
            }

            sb = sb.UnIndent();
            sb.AppendLine("}");
            sb.AppendLine();
        }
Example #3
0
        private string GetDefaultLiteral(MetadataPropertyType prop, MetadataType type)
        {
            var propType = Type(prop.Type, prop.GenericArgs);

            var initCollections = feature.ShouldInitializeCollections(type, Config.InitializeCollections);

            if (initCollections && prop.IsCollection())
            {
                return(prop.IsArray()
                    ? "[||]"
                    : "new {0}()".Fmt(propType));
            }
            return(prop.IsValueType.GetValueOrDefault()
                ? "new {0}()".Fmt(propType)
                : "null");
        }
        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);

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

            if (type.IsEnum.GetValueOrDefault())
            {
                var hasIntValue     = type.EnumNames.Count == (type.EnumValues?.Count ?? 0);
                var enumConstructor = hasIntValue ? "(val value:Int)" : "";

                sb.AppendLine($"enum class {typeName}{enumConstructor}");
                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 = hasIntValue ? type.EnumValues[i] : null;

                        var serializeAs = JsConfig.TreatEnumAsInteger || type.Attributes.Safe().Any(x => x.Name == "Flags")
                            ? $"@SerializedName(\"{value}\") "
                            : "";

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

                    //if (hasIntValue)
                    //{
                    //    sb.AppendLine();
                    //    sb.AppendLine("private final int value;");
                    //    sb.AppendLine("{0}(final int intValue) {{ value = intValue; }}".Fmt(typeName));
                    //    sb.AppendLine("public int getValue() { return value; }");
                    //}
                }

                sb = sb.UnIndent();
                sb.AppendLine("}");
            }
            else
            {
                var defType = type.IsInterface()
                    ? "interface"
                    : "class";
                var extends = new List <string>();

                //: BaseClass, Interfaces
                if (type.Inherits != null)
                {
                    extends.Add(Type(type.Inherits).InheritedType());
                }

                string responseTypeExpression = null;

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

                        if (implStr.StartsWith("IReturn<"))
                        {
                            var types      = implStr.RightPart('<');
                            var returnType = types.Substring(0, types.Length - 1);

                            //Can't get .class from Generic Type definition
                            responseTypeExpression = returnType.Contains("<")
                                ? $"object : TypeToken<{returnType}>(){{}}.type"
                                : $"{returnType}::class.java";
                        }
                    }
                    if (!type.Implements.IsEmpty())
                    {
                        foreach (var interfaceRef in type.Implements)
                        {
                            interfaces.Add(Type(interfaceRef));
                        }
                    }
                }

                var extend = extends.Count > 0
                    ? " : " + extends[0] + "()"
                    : "";

                if (interfaces.Count > 0)
                {
                    extend += (extend.IsNullOrEmpty() ? " : " : ", ") + string.Join(", ", interfaces.ToArray());
                }

                sb.AppendLine($"open {defType} {typeName}{extend}");
                sb.AppendLine("{");

                sb = sb.Indent();

                var addVersionInfo = Config.AddImplicitVersion != null && options.IsRequest;
                if (addVersionInfo)
                {
                    sb.AppendLine($"val {"Version".PropertyStyle()}:Int = {Config.AddImplicitVersion}");
                }

                var initCollections = feature.ShouldInitializeCollections(type, Config.InitializeCollections);
                AddProperties(sb, type,
                              initCollections: !type.IsInterface() && initCollections,
                              includeResponseStatus: Config.AddResponseStatus && options.IsResponse &&
                              type.Properties.Safe().All(x => x.Name != typeof(ResponseStatus).Name));

                if (responseTypeExpression != null)
                {
                    sb.AppendLine($"companion object {{ private val responseType = {responseTypeExpression} }}");
                    sb.AppendLine($"override fun getResponseType(): Any? = {typeName}.responseType");
                }

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

            return(lastNS);
        }
        private string AppendType(ref StringBuilderWrapper sb, ref StringBuilderWrapper sbExt, MetadataType type, string lastNS,
                                  CreateTypeOptions options)
        {
            //sb = sb.Indent();

            var hasGenericBaseType = type.Inherits != null && !type.Inherits.GenericArgs.IsEmpty();

            if (Config.ExcludeGenericBaseTypes && hasGenericBaseType)
            {
                sb.AppendLine("//Excluded {0} : {1}<{2}>".Fmt(type.Name, type.Inherits.Name.LeftPart('`'), string.Join(",", type.Inherits.GenericArgs)));
                return(lastNS);
            }

            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("public enum {0} : Int".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
                            ? "case {0}".Fmt(name)
                            : "case {0} = {1}".Fmt(name, value));
                    }
                }

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

                AddEnumExtension(ref sbExt, type);
            }
            else
            {
                var defType  = "class";
                var typeName = Type(type.Name, type.GenericArgs).AddGenericConstraints();
                var extends  = new List <string>();

                //: BaseClass, Interfaces
                if (type.Inherits != null)
                {
                    var baseType = Type(type.Inherits).InheritedType();

                    //Swift requires re-declaring base type generics definition on super type
                    var genericDefPos = baseType.IndexOf("<");
                    if (genericDefPos >= 0)
                    {
                        //Need to declare BaseType is JsonSerializable
                        var subBaseType = baseType.Substring(genericDefPos)
                                          .AddGenericConstraints();

                        typeName += subBaseType;
                    }

                    extends.Add(baseType);
                }
                else if (Config.BaseClass != null && !type.IsInterface())
                {
                    extends.Add(Config.BaseClass);
                }

                var typeAliases = new List <string>();

                if (options.ImplementsFn != null)
                {
                    //Swift doesn't support Generic Interfaces like IReturn<T>
                    //Converting them into protocols with typealiases instead
                    ExtractTypeAliases(options, typeAliases, extends, ref sbExt);

                    if (!type.Implements.IsEmpty())
                    {
                        type.Implements.Each(x => extends.Add(Type(x)));
                    }
                }

                if (type.IsInterface())
                {
                    defType = "protocol";

                    //Extract Protocol Arguments into different typealiases
                    if (!type.GenericArgs.IsEmpty())
                    {
                        typeName = Type(type.Name, null);
                        foreach (var arg in type.GenericArgs)
                        {
                            typeAliases.Add("public typealias {0} = {0}".Fmt(arg));
                        }
                    }
                }

                var extend = extends.Count > 0
                    ? " : " + (string.Join(", ", extends.ToArray()))
                    : "";

                sb.AppendLine("public {0} {1}{2}".Fmt(defType, typeName, extend));
                sb.AppendLine("{");

                sb = sb.Indent();

                if (typeAliases.Count > 0)
                {
                    foreach (var typeAlias in typeAliases)
                    {
                        sb.AppendLine(typeAlias);
                    }
                    sb.AppendLine();
                }

                if (!type.IsInterface())
                {
                    if (extends.Count > 0 && OverrideInitForBaseClasses.Contains(extends[0]))
                    {
                        sb.AppendLine("required public override init(){}");
                    }
                    else
                    {
                        sb.AppendLine("required public init(){}");
                    }
                }

                var addVersionInfo = Config.AddImplicitVersion != null && options.IsRequest;
                if (addVersionInfo)
                {
                    sb.AppendLine("public var {0}:Int = {1}".Fmt("Version".PropertyStyle(), Config.AddImplicitVersion));
                }

                var initCollections = feature.ShouldInitializeCollections(type, Config.InitializeCollections);
                AddProperties(sb, type,
                              initCollections: !type.IsInterface() && initCollections,
                              includeResponseStatus: Config.AddResponseStatus && options.IsResponse &&
                              type.Properties.Safe().All(x => x.Name != typeof(ResponseStatus).Name));

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

                if (!type.IsInterface())
                {
                    AddTypeExtension(ref sbExt, type,
                                     initCollections: Config.InitializeCollections);
                }
            }

            //sb = sb.UnIndent();

            return(lastNS);
        }