コード例 #1
0
        public override string GetEnumMemberName(string member)
        {
            string retVal = SwiftNameHelper.convertToValidSwiftTypeName(base.GetEnumMemberName(member));

            retVal = retVal.Replace(" ", "");
            return(retVal);
        }
コード例 #2
0
        public string RequiredPropertiesSettersForInitParameters()
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertySwift>().ToList();

            if (BaseModelType != null)
            {
                indented.Append(((CompositeTypeSwift)BaseModelType).FieldWriter.RequiredPropertiesSettersForInitParameters());
            }

            foreach (var property in properties)
            {
                if (property.ModelType == null)
                {
                    continue;
                }

                if (property.IsPolymorphicDiscriminator)
                {
                    continue;
                }

                var propName  = property.VariableName;
                var modelType = property.ModelType;
                if (modelType is PrimaryTypeSwift)
                {
                    ((PrimaryTypeSwift)modelType).IsRequired = property.IsRequired;
                }

                var modelDeclaration = modelType.Name;
                if (modelType is PrimaryTypeSwift)
                {
                    modelDeclaration = SwiftNameHelper.getTypeName(modelType.Name, property.IsRequired);
                }
                else if (modelType is IVariableType &&
                         !string.IsNullOrEmpty(((IVariableType)modelType).VariableTypeDeclaration(property.IsRequired)))
                {
                    modelDeclaration = ((IVariableType)modelType).VariableTypeDeclaration(property.IsRequired);
                }

                if (property.IsRequired)
                {
                    indented.Append($"self.{propName} = {propName}\r\n");
                }
            }

            return(indented.ToString());
        }
コード例 #3
0
        public string FieldsForValidation()
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertySwift>().ToList();

            if (BaseModelType != null)
            {
                indented.Append(((CompositeTypeSwift)BaseModelType).FieldWriter.FieldsForValidation());
            }

            // Emit each property, except for named Enumerated types, as a pointer to the type
            foreach (var property in properties)
            {
                if (property.ModelType == null)
                {
                    continue;
                }

                if (property.IsPolymorphicDiscriminator)
                {
                    continue;
                }

                var propName         = SwiftNameHelper.convertToValidSwiftTypeName(property.Name.RawValue);
                var modelType        = property.ModelType;
                var modelDeclaration = modelType.Name;
                var serializeName    = SwiftNameHelper.convertToValidSwiftTypeName(property.SerializedName);
                if (modelType is IVariableType &&
                    !string.IsNullOrEmpty(((IVariableType)modelType).DecodeTypeDeclaration(property.IsRequired)))
                {
                }
                else
                {
                }
            }

            return(indented.ToString());
        }
コード例 #4
0
        public string FieldsAsString(bool forInterface = false)
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertySwift>().ToList();

            if (BaseModelType != null && !forInterface)
            {
                indented.Append(((CompositeTypeSwift)BaseModelType).FieldWriter.FieldsAsString(forInterface));
            }

            // Emit each property, except for named Enumerated types, as a pointer to the type
            foreach (var property in properties)
            {
                if (property.ModelType == null)
                {
                    continue;
                }

                var modelType = property.ModelType;
                if (property.IsPolymorphicDiscriminator)
                {
                    continue;
                }

                if (modelType is PrimaryTypeSwift)
                {
                    ((PrimaryTypeSwift)modelType).IsRequired = property.IsRequired;
                }

                var modelDeclaration = modelType.Name;
                if (modelType is PrimaryTypeSwift)
                {
                    modelDeclaration = SwiftNameHelper.getTypeName(modelType.Name, property.IsRequired);
                }
                else if (modelType is IVariableType)
                {
                    modelDeclaration = ((IVariableType)modelType).VariableTypeDeclaration(property.IsRequired);
                }

                var output   = string.Empty;
                var propName = property.VariableName;
                var modifier = forInterface ? "" : "public";
                //TODO: need to handle flatten property case.
                output = string.Format("{2} var {0}: {1}",
                                       propName,
                                       modelDeclaration,
                                       modifier);

                if (forInterface)
                {
                    output += " { get set }\n";
                }
                else
                {
                    output += "\n";
                }

                indented.Append(output);
            }

            return(indented.ToString());
        }
コード例 #5
0
        public string RequiredPropertiesForInitParameters(bool forMethodCall = false)
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertySwift>().ToList();
            var seperator  = "";

            if (BaseModelType != null)
            {
                indented.Append(((CompositeTypeSwift)BaseModelType).FieldWriter.RequiredPropertiesForInitParameters(forMethodCall));
                if (indented.ToString().Length > 0)
                {
                    seperator = ", ";
                }
            }

            // Emit each property, except for named Enumerated types, as a pointer to the type
            foreach (var property in properties)
            {
                if (property.ModelType == null)
                {
                    continue;
                }

                if (property.IsPolymorphicDiscriminator)
                {
                    continue;
                }

                var modelType = property.ModelType;
                if (modelType is PrimaryTypeSwift)
                {
                    ((PrimaryTypeSwift)modelType).IsRequired = property.IsRequired;
                }

                var modelDeclaration = modelType.Name;
                if (modelType is PrimaryTypeSwift)
                {
                    modelDeclaration = SwiftNameHelper.getTypeName(modelType.Name, property.IsRequired);
                }
                else if (modelType is IVariableType &&
                         !string.IsNullOrEmpty(((IVariableType)modelType).VariableTypeDeclaration(property.IsRequired)))
                {
                    modelDeclaration = ((IVariableType)modelType).VariableTypeDeclaration(property.IsRequired);
                }

                var output   = string.Empty;
                var propName = property.VariableName;

                if (property.IsRequired)
                {
                    if (forMethodCall)
                    {
                        indented.Append($"{seperator}{propName}: {propName}");
                    }
                    else
                    {
                        indented.Append($"{seperator}{propName}: {modelDeclaration}");
                    }

                    seperator = ", ";
                }
            }

            return(indented.ToString());
        }
コード例 #6
0
        public string FieldDecodingString()
        {
            var indented   = new IndentedStringBuilder("    ");
            var properties = Properties.Cast <PropertySwift>().ToList();

            if (BaseModelType != null)
            {
                indented.Append(((CompositeTypeSwift)BaseModelType).FieldWriter.FieldDecodingString());
            }

            // Emit each property, except for named Enumerated types, as a pointer to the type
            foreach (var property in properties)
            {
                if (property.ModelType == null)
                {
                    continue;
                }

                if (property.IsPolymorphicDiscriminator)
                {
                    continue;
                }

                var propName  = property.VariableName;
                var modelType = property.ModelType;
                if (modelType is PrimaryTypeSwift)
                {
                    ((PrimaryTypeSwift)modelType).IsRequired = property.IsRequired;
                }

                var modelDeclaration = modelType.Name;
                if (modelType is PrimaryTypeSwift)
                {
                    modelDeclaration = SwiftNameHelper.getTypeName(modelType.Name, property.IsRequired);
                }
                else if (modelType is IVariableType &&
                         !string.IsNullOrEmpty(((IVariableType)modelType).DecodeTypeDeclaration(property.IsRequired)))
                {
                    modelDeclaration = ((IVariableType)modelType).DecodeTypeDeclaration(property.IsRequired);
                }

                if (property.IsRequired)
                {
                    if (modelType is PrimaryTypeSwift &&
                        "Date".Equals(modelType.Name))
                    {
                        this.compositeType.SetDecodeDate(propName, property, indented);
                    }
                    else
                    {
                        indented.Append($"self.{propName} = try container.decode({modelDeclaration}.self, forKey: .{propName})\r\n");
                    }
                }
                else
                {
                    indented.Append($"if container.contains(.{propName}) {{\r\n");
                    if (modelType is PrimaryTypeSwift &&
                        "Date".Equals(modelType.Name))
                    {
                        this.compositeType.SetDecodeDate(propName, property, indented);
                    }
                    else
                    {
                        indented.Append($"    self.{propName} = try container.decode({modelDeclaration}.self, forKey: .{propName})\r\n");
                    }
                    indented.Append($"}}\r\n");
                }
            }

            return(indented.ToString());
        }
コード例 #7
0
        private void TransformEnumTypes(CodeModelSwift cmg)
        {
            // fix up any enum types that are missing a name.
            // NOTE: this must be done before the next code block
            foreach (var mt in cmg.ModelTypes)
            {
                foreach (var property in mt.Properties)
                {
                    if (property.ModelType is EnumTypeSwift)
                    {
                        var enumType = property.ModelType as EnumTypeSwift;

                        if (!enumType.IsNamed)
                        {
                            enumType.SetName(property.Name);
                        }
                    }
                }
            }

            // fix up any enum types that are missing a name.
            // NOTE: this must be done before the next code block
            foreach (var method in cmg.Methods)
            {
                foreach (var parameter in method.Parameters)
                {
                    if (parameter.ModelType is EnumTypeSwift)
                    {
                        var enumType = parameter.ModelType as EnumTypeSwift;

                        if (!enumType.IsNamed)
                        {
                            var typeName = SwiftNameHelper.convertToValidSwiftTypeName(parameter.Name);
                            enumType.SetName($"{method.Name}{typeName}");
                            enumType.UnNamedEnumRelatedType = new EnumTypeSwift(enumType);
                            cmg.Add(enumType.UnNamedEnumRelatedType);
                        }
                    }
                }
            }

            // And add any others with a defined name and value list (but not already located)
            foreach (var mt in cmg.ModelTypes)
            {
                var namedEnums = mt.Properties.Where(p => p.ModelType is EnumTypeSwift && (p.ModelType as EnumTypeSwift).IsNamed);
                foreach (var p in namedEnums)
                {
                    if (!cmg.EnumTypes.Any(etm => etm.Equals(p.ModelType)))
                    {
                        ((EnumTypeSwift)p.ModelType).UnNamedEnumRelatedType = new EnumTypeSwift(p.ModelType as EnumType);
                        cmg.Add(((EnumTypeSwift)p.ModelType).UnNamedEnumRelatedType);
                    }
                }
                ;
            }


            // now normalize the names
            // NOTE: this must be done after all enum types have been accounted for
            foreach (var enumType in cmg.EnumTypes)
            {
                enumType.SetName(CodeNamer.Instance.GetTypeName(enumType.Name.FixedValue));
                foreach (var v in enumType.Values)
                {
                    v.Name = CodeNamer.Instance.GetEnumMemberName(v.Name);
                }
            }

            // Ensure all enumerated type values have the simplest possible unique names
            // -- The code assumes that all public type names are unique within the client and that the values
            //    of an enumerated type are unique within that type. To safely promote the enumerated value name
            //    to the top-level, it must not conflict with other existing types. If it does, prepending the
            //    value name with the (assumed to be unique) enumerated type name will make it unique.

            // First, collect all type names (since these cannot change)
            var topLevelNames = new HashSet <string>();

            cmg.ModelTypes
            .ForEach(mt => topLevelNames.Add(mt.Name));

            // Then, note each enumerated type with one or more conflicting values and collect the values from
            // those enumerated types without conflicts.  do this on a sorted list to ensure consistent naming
            cmg.EnumTypes.Cast <EnumTypeSwift>().OrderBy(etg => etg.Name.Value)
            .ForEach(em =>
            {
                if (em.Values.Where(v => topLevelNames.Contains(v.Name) || CodeNamerSwift.Instance.UserDefinedNames.Contains(v.Name)).Count() > 0)
                {
                    em.HasUniqueNames = false;
                }
                else
                {
                    em.HasUniqueNames = true;
                    topLevelNames.UnionWith(em.Values.Select(ev => ev.Name).ToList());
                }
            });

            // add documentation comment if there aren't any
            cmg.EnumTypes.Cast <EnumTypeSwift>()
            .ForEach(em =>
            {
                if (em.Name.Equals("enum"))
                {
                    em.Name.FixedValue = em.ClassName;
                }

                if (string.IsNullOrEmpty(em.Documentation))
                {
                    em.Documentation = string.Format("{0} enumerates the values for {1}.", em.Name, em.Name.FixedValue.ToPhrase());
                }
            });
        }