zijianhuang / webapiclientgen

Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
MIT License
167 stars 38 forks source link

option for enum to become int even if starting from zero #71

Closed zijianhuang closed 5 years ago

zijianhuang commented 5 years ago

currently if enum is a sequence starting from zero, int is not exported.

        static string GetEnumMember(CodeMemberField member)
        {
            var initExpression = member.InitExpression as CodePrimitiveExpression;
            return (initExpression == null) ? $"{member.Name}" : $"{member.Name} = {initExpression.Value}";
        }

member might have a defaultExpression

zijianhuang commented 5 years ago

Implemented. However, found not needed. enum could be number at run time, and become index of array.

        static void WriteEnumMembersAndCloseBracing(CodeTypeDeclaration typeDeclaration, TextWriter w, CodeGeneratorOptions o)
        {
            var currentIndent = o.IndentString;
            o.IndentString += BasicIndent;

            var enumMembers = typeDeclaration.Members.OfType<CodeTypeMember>().ToList();
            bool anyMemberDocComment = enumMembers.Any(d => d.Comments.Count > 0);
            bool anyInit = enumMembers.Any(ctm =>
            {
                var codeMemberField = ctm as CodeMemberField;
                var initExpression = codeMemberField.InitExpression as CodePrimitiveExpression;
                return initExpression != null;
            });

            if (!anyInit)
            {
                var i = 0;
                enumMembers.ForEach(ctm =>
                {
                    var codeMemberField = ctm as CodeMemberField;
                    codeMemberField.InitExpression = new CodePrimitiveExpression(i);
                    i++;
                });
            }

            if (anyMemberDocComment)
            {
                var i = 0;
                w.WriteLine();
                enumMembers.ForEach(ctm =>
                {
                    if (i > 0)
                    {
                        w.WriteLine(",");
                    }

                    var codeMemberField = ctm as CodeMemberField;
                    System.Diagnostics.Trace.Assert(codeMemberField != null);

                    //Handle the comment of the member
                    WriteCodeCommentStatementCollection(ctm.Comments, w, o);//This will create a new line first
                    w.Write(o.IndentString);

                    var enumMemberText = GetEnumMember(codeMemberField);
                    w.Write(enumMemberText);
                    i++;
                });

                w.WriteLine();
                w.WriteLine(currentIndent + "}");
            }
            else
            {
                var i = 0;
                w.Write(" ");
                enumMembers.ForEach(ctm =>
                {
                    if (i > 0)
                    {
                        w.Write(", ");
                    }

                    var codeMemberField = ctm as CodeMemberField;
                    System.Diagnostics.Trace.Assert(codeMemberField != null);
                    var enumMemberText = GetEnumMember(codeMemberField);
                    w.Write(enumMemberText);
                    i++;
                });

                w.WriteLine(" }");
            }

            o.IndentString = currentIndent;
        }