Ejemplo n.º 1
0
        public static string Populate(string template, Schema.Column columnInTemplate, Schema.Column column)
        {
            // Populate by replacing default values.
            List <Tuple <string, string> > replacements = new List <Tuple <string, string> >();

            if (columnInTemplate.UnderlyingType != null)
            {
                replacements.Add(new Tuple <string, string>(columnInTemplate.UnderlyingType, column.UnderlyingType));
            }

            if (columnInTemplate.Default != null)
            {
                replacements.Add(new Tuple <string, string>(columnInTemplate.Default, column.Default ?? "default"));
            }

            if (columnInTemplate.ReferencedTableName != null)
            {
                replacements.Add(new Tuple <string, string>(columnInTemplate.ReferencedTableName, column.ReferencedTableName));
            }

            replacements.Add(new Tuple <string, string>(columnInTemplate.Type, column.Type));
            replacements.Add(new Tuple <string, string>(columnInTemplate.Type.ToPascalCase(), column.Type.ToPascalCase()));
            replacements.Add(new Tuple <string, string>(columnInTemplate.Name, column.Name));
            replacements.Add(new Tuple <string, string>(columnInTemplate.Name.ToCamelCase(), column.Name.ToCamelCase()));

            // Sort by length descending
            replacements.Sort((left, right) => - left.Item1.Length.CompareTo(right.Item1.Length));

            // Turn template into a format string
            string escaped = template.Replace("{", "{{").Replace("}", "}}");

            for (int i = 0; i < replacements.Count; ++i)
            {
                escaped = escaped.Replace(replacements[i].Item1, $"{{{i}}}");
            }

            string populated = string.Format(escaped, replacements.Select(tuple => tuple.Item2).ToArray());

            return(populated);
        }
Ejemplo n.º 2
0
        public static string Populate(Dictionary <string, string> templates, string templateName, Schema.Column column)
        {
            string template;

            Schema.Column columnInTemplate;

            // Use a category-specific template, if found
            if (templates.TryGetValue($"{column.Category}{templateName}", out template))
            {
                if (!TemplateDefaults.Columns.TryGetValue(column.Category, out columnInTemplate))
                {
                    throw new NotImplementedException($"Populate not implemented with defaults to replace for category {column.Category} ({column.Name})");
                }

                return(Populate(template, columnInTemplate, column));
            }

            // Otherwise, use a non-specific template (using the Simple column defaults)
            if (templates.TryGetValue(templateName, out template))
            {
                return(Populate(template, TemplateDefaults.Columns[Schema.ColumnTypeCategory.Simple], column));
            }

            throw new NotSupportedException($"Could not find template '{column.Category}{templateName}' or '{templateName}' in collection: ({string.Join(", ", templates.Keys)}");
        }
Ejemplo n.º 3
0
        static SColumn ToColumn(string tableName, string columnName, JsonSchema schema)
        {
            SchemaType type         = schema.SafeGetType();
            string     defaultValue = schema.Default?.ToString();

            if (NameRenames.TryGetValue(columnName, out string columnRename))
            {
                columnName = columnRename;
            }

            if (columnName == "Properties")
            {
                return(SColumn.Simple("Properties", "IDictionary<String, SerializedPropertyInfo>"));
            }

            switch (type)
            {
            case SchemaType.Boolean:
                return(SColumn.Simple(columnName, "bool", defaultValue?.ToLowerInvariant()));

            case SchemaType.Integer:
                return(SColumn.Simple(columnName, "int", defaultValue));

            case SchemaType.Number:
                return(SColumn.Simple(columnName, "double", defaultValue));

            case SchemaType.String:
                if (schema.Format == "uri" || schema.Format == "uri-reference")
                {
                    return(SColumn.Simple(columnName, "Uri"));
                }
                else if (schema.Format == "date-time")
                {
                    return(SColumn.Simple(columnName, "DateTime"));
                }
                else
                {
                    if (defaultValue != null)
                    {
                        defaultValue = "\"" + defaultValue + "\"";
                    }
                    return(SColumn.Simple(columnName, "String", defaultValue));
                }

            case SchemaType.Object:
                if (schema.Reference != null)
                {
                    string refType = schema.Reference.GetDefinitionName().ToPascalCase();
                    if (TypeRenames.TryGetValue(refType, out string renamed))
                    {
                        refType = renamed;
                    }
                    return(SColumn.Ref(columnName, refType));
                }
                else
                {
                    string valueType = ToColumn(tableName, columnName, schema.AdditionalProperties.Schema).Type;
                    return(SColumn.Simple(columnName, $"IDictionary<String, {valueType}>"));
                }

            case SchemaType.Array:
                SColumn itemType = ToColumn(tableName, columnName, schema.Items.Schema);

                if (itemType.Category == ColumnTypeCategory.Enum)
                {
                    // NOTE: DefaultValue not translated for FlagsEnum
                    string enumType = itemType.Type;
                    return(SColumn.Enum(columnName, enumType, "int", $"default({enumType})"));
                }
                else if (itemType.ReferencedTableName != null)
                {
                    return(SColumn.RefList(columnName, itemType.ReferencedTableName));
                }
                else
                {
                    return(SColumn.Simple(columnName, $"IList<{itemType.Type}>"));
                }

            case SchemaType.None:
                if (schema.Enum != null)
                {
                    string enumType = $"{tableName}{columnName}";
                    if (TypeRenames.TryGetValue(enumType, out string renamedType))
                    {
                        enumType = renamedType;
                    }

                    if (defaultValue == null)
                    {
                        defaultValue = $"default({enumType})";
                    }
                    else
                    {
                        defaultValue = $"{enumType}.{defaultValue.ToPascalCase()}";
                    }

                    return(SColumn.Enum(columnName, enumType, "int", defaultValue));
                }

                break;
            }

            throw new NotImplementedException($"Type translation for {tableName}.{columnName} of type {type} not available.");
        }