public static string GetMethodReturnType(this Result result) { if (result.GenericName != ResultGenericName.ClientResult) { throw new ArgumentOutOfRangeException(nameof(result.GenericName), result.GenericName, "Unsupported method result generic name"); } if (result.Type != ParamType.Generic) { throw new ArgumentOutOfRangeException(nameof(Result.Type), result.Type, "Unsupported method result type"); } if (result.GenericArgs.Length != 1) { throw new ArgumentException("Result should contains only one generic argument"); } GenericArg genericArg = result.GenericArgs[0]; switch (genericArg.Type) { case GenericArgType.None: return(null); case GenericArgType.Ref: return(NamingConventions.Normalize(genericArg.RefName)); default: throw new ArgumentOutOfRangeException(nameof(Type), genericArg.Type, "Unsupported generic type"); } }
TypeReference GetGenericArg(GenericArg arg, GenericContext context) { TypeReference type = GetTypeRefFromSig(arg.Type, context); if (arg.CustomMods != null && arg.CustomMods.Length > 0) { type = GetModifierType(arg.CustomMods, type); } return(type); }
/// <summary> /// Returns the interface to be exposed. /// </summary> /// <param name="containerType">Type of the object in which our instance will be composed. /// This is not relevant for our purpose.</param> /// <returns>The content of the <see cref="InterfaceType"/> property.</returns> protected override Type[] GetPublicInterfaces(Type containerType) { return(new [] { GenericArg.Map(this.InterfaceType, containerType.GetGenericArguments(), null) }); }
/// <summary> /// Creates the composed object. /// </summary> /// <param name="eventArgs">Event arguments.</param> /// <returns>A new instance of the type specified in the <see cref="ImplementationType"/> property.</returns> public override object CreateImplementationObject(AdviceArgs eventArgs) { return (Activator.CreateInstance( GenericArg.Map(this.ImplementationType, eventArgs.Instance.GetType().GetGenericArguments(), null))); }
private static MemberDeclarationSyntax GetMethodDeclaration(Module module, Function function, bool withBody) { var responseType = function.Result.GetMethodReturnType(); var responseDeclaration = responseType == null ? "Task" : $"Task<{responseType}>"; var requestParam = new { name = default(string), type = default(string) }; var callbackParam = new { name = default(string), nameWithNull = default(string), type = default(string) }; foreach (Param param in function.Params) { if (param.Type == ParamType.Generic && param.GenericName == ParamGenericName.Arc) { GenericArg arcArg = param.GenericArgs[0]; if (arcArg.Type == GenericArgType.Ref && arcArg.RefName == "Request") { var name = StringUtils.EscapeReserved(function.Params[2].Name.GetEnumMemberValueOrString()); callbackParam = new { name, nameWithNull = $"{name} = null", type = string.Equals(module.Name, "net", StringComparison.OrdinalIgnoreCase) ? "JsonElement" : NamingConventions.EventFormatter(module.Name) }; } } if (param.Type == ParamType.Generic && param.GenericName == ParamGenericName.AppObject) { callbackParam = new { name = "appObject", nameWithNull = "appObject = null", type = "JsonElement" } } ; if (param.Name == Name.Params) { requestParam = new { name = StringUtils.EscapeReserved(function.Params[1].Name.GetEnumMemberValueOrString()), type = GetParamType(function.Params[1]) } } ; } var functionSummary = function.Summary + (function.Description != null ? $"\n{function.Description}" : null); var modifiers = new List <SyntaxToken> { Token(SyntaxKind.PublicKeyword).WithLeadingTrivia(CommentsHelpers.BuildCommentTrivia(functionSummary)) }; if (withBody) { modifiers.Add(Token(SyntaxKind.AsyncKeyword)); } var @params = new List <ParameterSyntax>(); var methodDeclarationParams = new List <ParameterSyntax>(); if (requestParam.name != default) { ParameterSyntax param = Parameter(Identifier(requestParam.name)).WithType(IdentifierName(requestParam.type)); methodDeclarationParams.Add(param); @params.Add(param); } if (callbackParam.name != default) { methodDeclarationParams.Add(Parameter(Identifier(callbackParam.nameWithNull)).WithType(IdentifierName($"Action<{callbackParam.type},uint>"))); @params.Add(Parameter(Identifier(callbackParam.name)).WithType(IdentifierName($"Action<{callbackParam.type},uint>"))); } MethodDeclarationSyntax method = MethodDeclaration(ParseTypeName(responseDeclaration), NamingConventions.Normalize(function.Name)) .AddParameterListParameters(methodDeclarationParams.ToArray()) .AddParameterListParameters(Parameter(Identifier("cancellationToken")) .WithType(IdentifierName(nameof(CancellationToken))) .WithDefault(EqualsValueClause(IdentifierName("default")))) .AddModifiers(modifiers.ToArray()); if (withBody) { var arguments = new List <ArgumentSyntax> { Argument(IdentifierName($"\"{module.Name}.{function.Name}\"")) }; arguments.AddRange( @params .Select(p => Argument(IdentifierName(p.Identifier.Text)))); arguments.Add(Argument(IdentifierName("cancellationToken"))); var genericParametersDeclaration = StringUtils.GetGenericParametersDeclaration(requestParam?.type, responseType, callbackParam?.type); AwaitExpressionSyntax awaitExpression = AwaitExpression( InvocationExpression(IdentifierName($"_tonClientAdapter.Request{genericParametersDeclaration}")) .AddArgumentListArguments(arguments.ToArray())); StatementSyntax ex = responseType == null ? ExpressionStatement(awaitExpression) : ReturnStatement(awaitExpression); BlockSyntax blockSyntax = Block(ex); return(method.WithBody(blockSyntax)); } return(method.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); }