public static IEnumerable <EndpointResponseSchemaModel> GetNestedSchemas(EndpointResponseSchemaModel schemaModel)
        {
            if (schemaModel.Type?.ToString() == "object" ||
                schemaModel.Type?.ToString() == "array")
            {
                yield return(schemaModel);
            }

            if (schemaModel.Properties != null)
            {
                foreach (var s in schemaModel.Properties)
                {
                    foreach (var ns in GetNestedSchemas(s))
                    {
                        yield return(ns);
                    }
                }
            }

            if (schemaModel.Items != null)
            {
                foreach (var s in GetNestedSchemas(schemaModel.Items))
                {
                    yield return(s);
                }
            }
        }
        public static async Task GenerateCode(EndpointResponseSchemaModel schemaModel, string className)
        {
            string generatedCode = CSGen.Generate(schemaModel, "MAD.API.Procore.Models", className);

            if (string.IsNullOrEmpty(generatedCode))
            {
                return;
            }

            string fileName = className + ".cs";

            string dirToWriteFiles = Path.Combine(Globals.OutputPath, "Gen");

            Directory.CreateDirectory(dirToWriteFiles);

            File.WriteAllText(Path.Combine(dirToWriteFiles, fileName), generatedCode);
        }
Esempio n. 3
0
        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());
        }
        public static string Create(EndpointResponseSchemaModel schemaModel)
        {
            string name;

            if (!string.IsNullOrEmpty(schemaModel.Title))
            {
                name = schemaModel.Title;
            }
            else if (!string.IsNullOrEmpty(schemaModel.Field))
            {
                name = schemaModel.Field;
            }
            else if (!string.IsNullOrEmpty(schemaModel.Description))
            {
                name = schemaModel.Description;
            }
            else
            {
                return(null);
            }

            return(name.CleanForCode());
        }
Esempio n. 5
0
        private static void SerializeObject(EndpointResponseSchemaModel schema, StringBuilder codeBuilder)
        {
            codeBuilder.AppendLine(" {");

            if (schema.Properties is null)
            {
                return;
            }

            foreach (var p in schema.Properties)
            {
                string type;
                bool   isNullable;

                if (p.Type is JArray jArray)
                {
                    type       = jArray.First?.ToString();
                    isNullable = jArray.Last?.ToString() == "null";
                }
                else
                {
                    type       = p.Type?.ToString();
                    isNullable = false;
                }

                if (string.IsNullOrEmpty(type) ||
                    type == "null")
                {
                    continue;
                }

                codeBuilder.AppendLine($"\t\t[JsonProperty(\"{p.Field}\")]");
                codeBuilder.Append($"\t\tpublic ");

                switch (type)
                {
                case "integer":
                    codeBuilder.Append("int");
                    break;

                case "boolean":
                    codeBuilder.Append("bool");
                    break;

                case "string":

                    if (p.Format == "date-time")
                    {
                        codeBuilder.Append("DateTimeOffset");
                    }
                    else
                    {
                        codeBuilder.Append("string");
                    }

                    break;

                case "object":
                    if (string.IsNullOrEmpty(p.Title))
                    {
                        codeBuilder.Append("JToken");
                    }
                    else
                    {
                        codeBuilder.Append(p.Title.CleanForCode());
                    }
                    break;

                case "array":
                    if (!string.IsNullOrEmpty(p.Items.Title))
                    {
                        codeBuilder
                        .Append(p.Items.Title.CleanForCode())
                        .Append("[]");
                    }
                    else
                    {
                        codeBuilder.Append("JToken");
                    }

                    break;

                case "number":
                    codeBuilder.Append("decimal");
                    break;

                default:
                    throw new NotImplementedException();
                }

                if (isNullable)
                {
                    codeBuilder.Append("?");
                }

                codeBuilder.AppendLine($" {p.Field.CleanForCode()} {{ get; set; }}");
            }
        }