Exemplo n.º 1
0
        /// <summary>
        /// Generates a slice literal of the specified type name and optional values.
        /// </summary>
        /// <param name="typeName">The fully qualified slice type name.</param>
        /// <param name="values">Optional slice values.  Pass null to omit values.</param>
        /// <returns>The root node in this slice literal AST.</returns>
        public static Node Generate(string typeName, IEnumerable <Node> values)
        {
            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw new ArgumentException(nameof(typeName));
            }

            if (values != null && !values.Any())
            {
                throw new ArgumentException("pass null for no slice elements");
            }

            var openBracket  = new OpenDelimiter(BinaryDelimiterType.Bracket);
            var closeBracket = openBracket.AddClosingDelimiter();

            var id = new Identifier(typeName);

            closeBracket.AddChild(id);

            var openBrace = new OpenDelimiter(BinaryDelimiterType.Brace);

            if (values != null)
            {
                var seq = DelimitedSequence.Generate(UnaryDelimiterType.Comma, values.ToList());
                openBrace.AddChild(seq);
            }

            openBrace.AddClosingDelimiter();
            id.AddChild(openBrace);

            return(openBracket);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates a function signature from the specified parameters.
        /// </summary>
        /// <param name="receiver">Optional signature for a receiver.</param>
        /// <param name="name">The name of the function.</param>
        /// <param name="parameters">Optional list of parameters.  Pass null if there are no parameters.</param>
        /// <param name="returns">Optional list of return values.  Pass null if there are no return values.</param>
        /// <returns>The root node in the function signature AST.</returns>
        public static Node Generate(FuncParamSig receiver, string name, IReadOnlyList <FuncParamSig> parameters, IReadOnlyList <FuncReturnSig> returns)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(nameof(name));
            }

            var func = new Func();

            if (receiver != null)
            {
                var recvOpenParen = new OpenDelimiter(BinaryDelimiterType.Paren);
                recvOpenParen.AddChild(receiver);
                recvOpenParen.AddClosingDelimiter();
                func.AddChild(recvOpenParen);
            }

            func.AddChild(new Identifier(name));

            // build parameters
            var paramsStart = new OpenDelimiter(BinaryDelimiterType.Paren);

            if (parameters != null)
            {
                paramsStart.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, parameters));
            }

            paramsStart.AddClosingDelimiter();
            func.AddChild(paramsStart);

            if (returns != null)
            {
                // multiple return values or a single return value
                // that has a name must be enclosed in parentheses
                if (returns.Count == 1)
                {
                    var ret = returns[0];
                    if (ret.IsNamed)
                    {
                        var returnOpenParen = new OpenDelimiter(BinaryDelimiterType.Paren);
                        returnOpenParen.AddChild(ret);
                        returnOpenParen.AddClosingDelimiter();
                        func.AddChild(returnOpenParen);
                    }
                    else
                    {
                        func.AddChild(ret);
                    }
                }
                else
                {
                    var returnOpenParen = new OpenDelimiter(BinaryDelimiterType.Paren);
                    returnOpenParen.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, returns));
                    returnOpenParen.AddClosingDelimiter();
                    func.AddChild(returnOpenParen);
                }
            }

            return(func);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates a standard error checking sequence.
        /// E.g. if err != nil return var1, var2, varN
        /// </summary>
        /// <param name="errorVar">The name of the error variable.</param>
        /// <param name="toReturn">
        /// List of variables to return.  All variables in
        /// this list will be returned in the specified order.
        /// </param>
        /// <returns>The root node of this error checking AST.</returns>
        public static Node Generate(string errorVar, IReadOnlyList <Node> toReturn)
        {
            var ret = new Return();

            ret.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, toReturn));

            var node = IfBlock.Generate(BinaryOpSequence.Generate(BinaryOperatorType.NotEqualTo, new Identifier(errorVar), new Nil()), new[]
            {
                ret
            });

            return(node);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates a function call for the specified values.
        /// Example: package.Func(one, &two, three)
        /// </summary>
        /// <param name="name">The fully qualified name of the function to call.</param>
        /// <param name="parameters">The optional list of parameters.  Pass null if there are no parameters.</param>
        /// <returns>The root node for this function call AST.</returns>
        public static Node Generate(string name, IReadOnlyList <FuncCallParam> parameters)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(nameof(name));
            }

            if (parameters != null && parameters.Count == 0)
            {
                throw new ArgumentException("pass null if there are no arguments");
            }

            var funcCall = new Identifier(name);

            var paramsStart = new OpenDelimiter(BinaryDelimiterType.Paren);

            funcCall.AddChild(paramsStart);

            if (parameters != null)
            {
                var seq = new Node[parameters.Count];

                // build a comma separated list of params
                for (int i = 0; i < parameters.Count; ++i)
                {
                    var param = parameters[i];

                    Node p = null;
                    if (param.Pass == TypeModifier.ByReference)
                    {
                        p = UnaryOpSequence.Generate(UnaryOperatorType.Ampersand, param.Param);
                    }
                    else if (param.Pass == TypeModifier.ByValue)
                    {
                        p = param.Param;
                    }

                    seq[i] = p;
                }

                paramsStart.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, seq));
            }

            paramsStart.AddClosingDelimiter();

            return(funcCall);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Generates a return statement with optional list of variables.
        /// </summary>
        /// <param name="varNames">Optional list of variables to be returned.  Pass null if there are no variables to return.</param>
        /// <returns>Root node in this return statement AST.</returns>
        public static Node Generate(IReadOnlyList <string> varNames)
        {
            if (varNames != null && varNames.Count == 0)
            {
                throw new ArgumentException("pass null for no arguments to return");
            }

            var ret = new Return();

            if (varNames != null)
            {
                Node[] returnVars = new Node[varNames.Count];
                for (int i = 0; i < returnVars.Length; ++i)
                {
                    returnVars[i] = new Identifier(varNames[i]);
                }

                ret.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, returnVars));
            }

            return(ret);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates a struct literal of the specified type name and optional field values.
        /// </summary>
        /// <param name="structTypeName">The fully qualified struct type name.</param>
        /// <param name="values">Optional field values.  Pass null to omit field initialization.</param>
        /// <returns>The root node in this struct literal AST.</returns>
        public static Node Generate(string structTypeName, IEnumerable <StructField> values)
        {
            if (string.IsNullOrWhiteSpace(structTypeName))
            {
                throw new ArgumentException(nameof(structTypeName));
            }

            if (values != null && !values.Any())
            {
                throw new ArgumentException("pass null to omit struct field initializers");
            }

            var id        = new Identifier(structTypeName);
            var openBrace = new OpenDelimiter(BinaryDelimiterType.Brace);

            if (values != null)
            {
                var initList = new List <Node>();
                foreach (var val in values)
                {
                    var root = DelimitedSequence.Generate(UnaryDelimiterType.Colon, new[]
                    {
                        new Identifier(val.FieldName),
                        val.Value
                    });

                    initList.Add(root);
                }

                var seqRoot = DelimitedSequence.Generate(UnaryDelimiterType.Comma, initList, true);
                openBrace.AddChild(seqRoot);
            }

            openBrace.AddClosingDelimiter();
            id.AddChild(openBrace);

            return(id);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Generates a test case function for the specified method.
        /// </summary>
        /// <param name="mg">The method for which a test case will be generated.</param>
        /// <param name="rpcTypeName">The type name use for RPC dispatch.</param>
        /// <returns>The root node in this test case function AST.</returns>
        public static Tuple <Node, string> Generate(MethodGo mg, string rpcTypeName, string replyTypeName, string replyConvFuncName, string loggerVarName)
        {
            if (mg == null)
            {
                throw new ArgumentNullException(nameof(mg));
            }

            if (string.IsNullOrWhiteSpace(replyTypeName))
            {
                throw new ArgumentException(nameof(replyTypeName));
            }

            if (string.IsNullOrWhiteSpace(replyConvFuncName))
            {
                throw new ArgumentException(nameof(replyConvFuncName));
            }

            var errorVar     = "err";
            var jsonParamVar = "parameters";
            var replyVar     = "reply";

            var operationId = mg.SerializedName.ToString();

            if (!char.IsUpper(operationId[0]))
            {
                operationId = $"{char.ToUpperInvariant(operationId[0])}{operationId.Substring(1)}";
            }

            var rpcTypeVar = char.ToLowerInvariant(rpcTypeName[0]).ToString();

            var func = FunctionSignature.Generate(new FuncParamSig(rpcTypeVar, TypeModifier.ByReference, rpcTypeName),
                                                  operationId, new[]
            {
                new FuncParamSig(jsonParamVar, TypeModifier.ByValue, "json.RawMessage"),
                new FuncParamSig(replyVar, TypeModifier.ByReference, replyTypeName)
            }, new[]
            {
                new FuncReturnSig(null, TypeModifier.ByValue, "error")
            });

            var funcBody = new OpenDelimiter(BinaryDelimiterType.Brace);

            // generate a type used to unmarshal the parameters
            string unmarshalVar  = null;
            var    unmarshalType = GenerateTypeForParamsUnmarshal(mg);

            funcBody.AddChild(unmarshalType.Item1);
            funcBody.AddChild(new Terminal());

            unmarshalVar = unmarshalType.Item2.ToLowerInvariant();

            // do the unmarshaling and return on failure
            funcBody.AddChild(VariableDecl.Generate(unmarshalVar, unmarshalType.Item2));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.DeclareAndAssign, new Identifier(errorVar),
                                                        FunctionCall.Generate("json.Unmarshal", new[]
            {
                new FuncCallParam(new Identifier(jsonParamVar), TypeModifier.ByValue),
                new FuncCallParam(new Identifier(unmarshalVar), TypeModifier.ByReference)
            })));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(ErrorCheck.Generate(errorVar, OnError.ReturnError));
            funcBody.AddChild(new Terminal());

            var authVar = "auth";

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.DeclareAndAssign,
                                                        DelimitedSequence.Generate(UnaryDelimiterType.Comma, new[]
            {
                new Identifier(authVar),
                new Identifier(errorVar)
            }),
                                                        FunctionCall.Generate($"{unmarshalVar}.{ReservedParamsFieldName}.Credentials.CreateBearerAuthorizer", null)));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(ErrorCheck.Generate(errorVar, OnError.ReturnError));
            funcBody.AddChild(new Terminal());

            // create the client and call the target function
            var clientVar = "client";

            // check if the client ctor has any parameters
            List <FuncCallParam> clientParams = null;

            if (!string.IsNullOrWhiteSpace(((MethodGroupGo)mg.MethodGroup).GlobalParameters))
            {
                clientParams = new List <FuncCallParam>();
                var globalParams = ((MethodGroupGo)mg.MethodGroup).GlobalParameters.Split(new[] { ',' });

                // each param is the var name followed by the type name, e.g. "subscriptionID string"
                foreach (var globalParam in globalParams)
                {
                    var paramName = globalParam.Substring(0, globalParam.IndexOf(' '));
                    clientParams.Add(new FuncCallParam(new Identifier($"{unmarshalVar}.{paramName.Capitalize()}"), TypeModifier.ByValue));
                }
            }

            funcBody.AddChild(FunctionCall.Generate($"{loggerVarName}.Println", new[]
            {
                new FuncCallParam(new Literal <string>(operationId), TypeModifier.ByValue)
            }));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(FunctionCall.Generate($"{loggerVarName}.Printf", new[]
            {
                new FuncCallParam(new Literal <string>("received %v\\n"), TypeModifier.ByValue),
                new FuncCallParam(new Identifier(unmarshalVar), TypeModifier.ByValue)
            }));
            funcBody.AddChild(new Terminal());

            // the client creation function name prefix is hard-coded in the Go generator
            var clientCtorName = $"{mg.CodeModel.Namespace}.New";

            if (!string.IsNullOrWhiteSpace(mg.MethodGroup.Name))
            {
                clientCtorName = $"{clientCtorName}{((MethodGroupGo)mg.MethodGroup).ClientName}";
            }

            funcBody.AddChild(BinaryOpSequence.Generate(
                                  BinaryOperatorType.DeclareAndAssign,
                                  new Identifier(clientVar),
                                  FunctionCall.Generate(clientCtorName, clientParams)));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.Assignment,
                                                        new Identifier($"{clientVar}.Authorizer"), new Identifier(authVar)));
            funcBody.AddChild(new Terminal());

            var rChanVar       = "rChan";
            var eChanVar       = "eChan";
            var responseVar    = "resp";
            var rawResponseVar = "rawResp";

            var isLro = mg.IsLongRunningOperation() && !mg.IsPageable;

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.DeclareAndAssign,
                                                        DelimitedSequence.Generate(UnaryDelimiterType.Comma, new[]
            {
                new Identifier(isLro ? rChanVar : responseVar),
                new Identifier(isLro ? eChanVar : errorVar)
            }),
                                                        FunctionCall.Generate($"{clientVar}.{mg.Name}", GenerateApiCallParams(mg, unmarshalVar))));
            funcBody.AddChild(new Terminal());

            if (isLro)
            {
                funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.Assignment,
                                                            new Identifier(errorVar), new Identifier($"<-{eChanVar}")));
                funcBody.AddChild(new Terminal());
            }

            funcBody.AddChild(ErrorCheck.Generate(errorVar, OnError.ReturnError));
            funcBody.AddChild(new Terminal());

            if (isLro)
            {
                funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.DeclareAndAssign,
                                                            new Identifier(responseVar), new Identifier($"<-{rChanVar}")));
                funcBody.AddChild(new Terminal());
            }

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.DeclareAndAssign,
                                                        DelimitedSequence.Generate(UnaryDelimiterType.Comma, new[]
            {
                new Identifier(rawResponseVar),
                new Identifier(errorVar)
            }),
                                                        FunctionCall.Generate(replyConvFuncName, new[]
            {
                new FuncCallParam(new Identifier(mg.HasReturnValue() ? $"{responseVar}.Response" : $"{responseVar}"), TypeModifier.ByReference),
                new FuncCallParam(mg.HasReturnValue() ? (Node) new Identifier(responseVar) : new Nil(), TypeModifier.ByValue)
            })));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(FunctionCall.Generate($"{loggerVarName}.Printf", new[]
            {
                new FuncCallParam(new Literal <string>("response %v\\n"), TypeModifier.ByValue),
                new FuncCallParam(new Identifier($"*{rawResponseVar}"), TypeModifier.ByValue)
            }));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(BinaryOpSequence.Generate(BinaryOperatorType.Assignment,
                                                        new Identifier($"*{replyVar}"), new Identifier($"*{rawResponseVar}")));
            funcBody.AddChild(new Terminal());

            funcBody.AddChild(ReturnStatement.Generate(new[]
            {
                errorVar
            }));

            funcBody.AddClosingDelimiter();
            func.AddChild(funcBody);
            return(new Tuple <Node, string>(func, operationId));
        }