Example #1
0
        public static ClassModel Generate(Schema schema, string ns, string className)
        {
            ClassModel classModel = new ClassModel
            {
                Usings =
                {
                    "System.Text",
                    "Newtonsoft.Json",
                    "Newtonsoft.Json.Linq",
                    "System",
                    "System.Collections.Generic"
                },
                Name      = className,
                Namespace = ns
            };

            if (schema.Items is null)
            {
                SerializeObject(schema, classModel);
            }
            else
            {
                if (schema.Items.Properties != null &&
                    schema.Items.Title is null)
                {
                    SerializeObject(schema.Items as Schema, classModel);
                }
                else
                {
                    var itemsSchema    = schema.Items as Schema;
                    var arrayItemClass = ClassNameFactory.Create(itemsSchema);

                    if (arrayItemClass.Contains("null"))
                    {
                        return(null);
                    }

                    classModel.BaseClass = new ClassModel
                    {
                        Name     = "List",
                        Generics =
                        {
                            arrayItemClass
                        }
                    };
                }
            }

            return(classModel);
        }
        static async Task Main(string[] args)
        {
            var groups = await RemoteJsonFile.GetJsonFile <IEnumerable <GroupModel> >("groups");

            IDictionary <string, EndpointResponseSchemaModel> allSchemas = new Dictionary <string, EndpointResponseSchemaModel>();

            foreach (var grp in groups)
            {
                var alreadySnakeCase = new Regex("/^[a-z_]*$/").IsMatch(grp.Name);

                string gelatoGroup = alreadySnakeCase ?
                                     grp.Name :
                                     grp.Name.Replace(" ", "-").ToLower();

                var endpointModels = await RemoteJsonFile.GetJsonFile <IEnumerable <EndpointModel> >(gelatoGroup);

                var schemas = GetSchemasFromEndpointModels(endpointModels);

                foreach (var r in schemas)
                {
                    foreach (var ns in GetNestedSchemas(r))
                    {
                        string name = ClassNameFactory.Create(ns);

                        if (string.IsNullOrEmpty(name))
                        {
                            continue;
                        }

                        if (name.ToLower().StartsWith("customfield"))
                        {
                            continue;
                        }

                        allSchemas.TryAdd(name, ns);
                    }
                }
            }

            foreach (var s in allSchemas)
            {
                await GenerateCode(s.Value, s.Key);
            }
        }
        public static string Generate(EndpointResponseSchemaModel schema, string ns, string className)
        {
            StringBuilder codeBuilder = new StringBuilder();

            codeBuilder
            .AppendLine("using System;")
            .AppendLine("using System.Text;")
            .AppendLine("using Newtonsoft.Json;")
            .AppendLine("using Newtonsoft.Json.Linq;")
            .AppendLine("using System.Collections.Generic;")
            .AppendLine($"namespace {ns} {{")
            .Append($"\tpublic class {className}");

            if (schema.Items is null)
            {
                SerializeObject(schema, codeBuilder);
            }
            else
            {
                if (schema.Items.Properties != null &&
                    schema.Items.Title is null)
                {
                    SerializeObject(schema.Items, codeBuilder);
                }
                else
                {
                    string arrayItemClass = ClassNameFactory.Create(schema.Items)
                                            ?? schema.Items.Type?.ToString().CleanForCode();

                    if (arrayItemClass.Contains("null"))
                    {
                        return(null);
                    }

                    codeBuilder.AppendLine($" : List<{arrayItemClass}> {{");
                }
            }

            codeBuilder.AppendLine("\t}");
            codeBuilder.AppendLine("}");

            return(codeBuilder.ToString());
        }
Example #4
0
        private static void SerializeObject(Schema schema, ClassModel classModel)
        {
            if (schema.Properties is null)
            {
                return;
            }

            foreach (Schema p in schema.Properties.Cast <Schema>())
            {
                if (string.IsNullOrEmpty(p.Type.Name) ||
                    p.Type.Name == "null")
                {
                    continue;
                }

                PropertyModel pm = new PropertyModel
                {
                    Name       = p.Field.CleanForCode(),
                    Attributes =
                    {
                        $"JsonProperty(\"{p.Field}\")"
                    },
                    Comment = p.Description
                };

                switch (p.Type.Name)
                {
                case "integer":
                    if (pm.Name == "Id" ||
                        pm.Name.EndsWith("Id"))
                    {
                        pm.Type = "long";
                    }
                    else
                    {
                        pm.Type = "int";
                    }

                    break;

                case "boolean":
                    pm.Type = "bool";
                    break;

                case "string":
                    if (p.Format == "date-time")
                    {
                        pm.Type = "DateTimeOffset";
                    }
                    else
                    {
                        pm.Type = "string";
                    }

                    break;

                case "object":
                    string objClassName = ClassNameFactory.Create(p);

                    if (objClassName.ToLower() == "customfields")
                    {
                        continue;
                    }

                    pm.Type = objClassName;
                    break;

                case "array":
                    if (p.Items is Schema itemsSchema)
                    {
                        string arrayItemName = ClassNameFactory.Create(itemsSchema);

                        if (string.IsNullOrEmpty(arrayItemName))
                        {
                            arrayItemName = ClassNameFactory.Create(p);
                        }

                        pm.Type = $"List<{arrayItemName}>";
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }

                    break;

                case "number":
                    pm.Type = "decimal";
                    break;

                default:
                    throw new NotImplementedException();
                }

                if (p.Type.IsNullable ||
                    p.Type.Name == "number" ||
                    (p.Type.Name == "integer" && p.Field != "id") ||
                    (p.Format == "date-time" && (!p.Field.StartsWith("created") && !p.Field.StartsWith("updated"))))
                {
                    pm.IsNullable = true;
                }

                classModel.Properties.Add(pm);
            }
        }