Example #1
0
        public void GenerateSchema(Class schema, Workspace workspace)
        {
            var builder = new StringBuilder();

            if (schema is EnumClass)
            {
                builder.AppendLine("using System.Runtime.Serialization;");
            }

            builder.AppendLine("using Newtonsoft.Json;")
            .AppendLine();

            if (Options.SchemaNamespace != null)
            {
                builder.AI().AppendLine($"namespace {Options.SchemaNamespace}")
                .AI().AppendLine("{")
                .Indent();
            }

            if (schema is SchemaClass cls)
            {
                builder.AI().AppendLine($"public class {cls.Name}")
                .AI().AppendLine("{")
                .Indent();


                foreach (var field in cls.Fields)
                {
                    string name = field.Name.Dehumanize();

                    if (name == cls.Name)
                    {
                        name = name[0] + name;
                    }

                    builder.AI().AppendLine($"[JsonProperty(\"{field.Name}\")]")
                    .AI().AppendLine($"public {GetCSType(field.Type)} {name};");
                }


                builder.Unindent()
                .AI().AppendLine("}");
            }
            else if (schema is EnumClass @enum)
            {
                builder.AI().AppendLine("[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]")
                .AI().AppendLine($"public enum {@enum.Name}")
                .AI().AppendLine("{")
                .Indent();

                foreach (var field in @enum.ItemNames)
                {
                    builder.AI().AppendLine($"[EnumMember(Value = \"{field}\")]")
                    .AI().AppendLine($"{field.Dehumanize()},");
                }


                builder.Unindent()
                .AI().AppendLine("}");
            }


            if (Options.SchemaNamespace != null)
            {
                builder.Unindent()
                .AI().AppendLine("}");
            }

            workspace.WriteToFile("Schemas/" + schema.Name + ".cs", builder.ToString());
        }
Example #2
0
        public void GenerateInterface(PathsClass paths, Workspace workspace)
        {
            var builder = new StringBuilder();

            builder.AppendLine("using System.Threading.Tasks;")
            .AppendLine($"using static {(string.IsNullOrEmpty(Options.IncludedFileNamespace) ? "" : Options.IncludedFileNamespace + ".")}GenerationUtils;")
            .AppendLine();

            if (Options.SchemaNamespace != null)
            {
                builder.AI().AppendLine($"namespace {Options.SchemaNamespace}")
                .AI().AppendLine("{")
                .Indent();
            }

            builder.AI().AppendLine($"public class {paths.Name}")
            .AI().AppendLine("{");

            builder.Indent();

            foreach (var method in paths.Methods)
            {
                var paramNames = method.Parameters.Keys.ToDictionary(o => o, o => o.Prettify());

                if (method.Documentation != null)
                {
                    builder.AI().AppendLine("///<summary>")
                    .AI().AppendLine($"///{method.Documentation.Summary}")
                    .AI().AppendLine("///</summary>");
                }

                foreach (var param in method.Parameters)
                {
                    if (param.Value.Documentation != null)
                    {
                        builder.AI().AppendLine($"///<param name=\"{paramNames[param.Key]}\">{param.Value.Documentation.Summary}</param>");
                    }
                }

                builder.AI().Append($"public Task{(method.ResponseType == null ? "" : $"<{GetCSType(method.ResponseType)}>")} {method.Name}(");

                if (method.RequestType != null)
                {
                    builder.Append($"{GetCSType(method.RequestType)} body");

                    if (method.Parameters.Count > 0)
                    {
                        builder.Append(", ");
                    }
                }

                foreach (var param in method.Parameters)
                {
                    builder.Append($"{GetCSType(param.Value.Type)} {paramNames[param.Key]}");

                    if (param.Key != method.Parameters.Keys.Last())
                    {
                        builder.Append(", ");
                    }
                }

                builder.AppendLine(")")
                .Indent();

                builder.AI().Append("=> Sender.Request");

                if (method.ResponseType != null)
                {
                    builder.Append($"<{GetCSType(method.ResponseType)}>");
                }

                string path = PathParamsRegex.Replace(method.Path, o => paramNames[o.Value]);

                builder.Append($"(\"{method.HttpMethod.ToString().ToLower()}\", $\"{path}");

                bool isFirstQueryParam = true;
                foreach (var param in method.Parameters.Where(o => o.Value.Position == ParameterPosition.Query))
                {
                    if (isFirstQueryParam)
                    {
                        builder.Append("?");
                    }
                    else
                    {
                        builder.Append("&");
                    }

                    builder.Append($"{param.Key}={{{paramNames[param.Key]}}}");

                    isFirstQueryParam = false;
                }

                //TODO Header parameters

                builder.Append("\"");

                if (method.RequestType != null)
                {
                    builder.Append(", body");
                }

                builder.AppendLine(");");

                builder.Unindent();
            }

            builder.Unindent();

            builder.AI().AppendLine("}");

            if (Options.SchemaNamespace != null)
            {
                builder.Unindent()
                .AI().AppendLine("}");
            }

            workspace.WriteToFile("Interfaces/" + paths.Name + ".cs", builder.ToString());
        }