示例#1
0
        public static string Parse(this ITemplateParser parser, string contengt, IDictionary <string, string> arguments)
        {
            var context = new ParseContext(new TemplateString(contengt, TemplateUtilities.GetVariables(contengt)), arguments);

            parser.Parse(context);

            return(context.Result);
        }
示例#2
0
        public static IList <MethodDescriptor> Build(GoModel model)
        {
            var descriptors = new List <MethodDescriptor>();

            foreach (var typeModel in model.Types)
            {
                var goAttribute = typeModel.Attributes.OfType <GoAttribute>().SingleOrDefault();
                if (goAttribute == null)
                {
                    continue;
                }
                foreach (var methodModel in typeModel.Methods)
                {
                    var goRequestAttribute = methodModel.Attributes.OfType <GoRequestAttribute>().SingleOrDefault();
                    if (goRequestAttribute == null)
                    {
                        continue;
                    }

                    var interceptorDescriptors = model
                                                 .Interceptors
                                                 .Concat(typeModel.Interceptors)
                                                 .Concat(methodModel.Interceptors)
                                                 .Select(i => new InterceptorDescriptor(i))
                                                 .OrderBy(i => i.Order)
                                                 .ToArray();

                    var baseUrl = goAttribute.Url;
                    var path    = goRequestAttribute.Path;

                    if (baseUrl.EndsWith("/") && path.StartsWith("/"))
                    {
                        baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
                    }
                    if (!baseUrl.EndsWith("/") && !path.StartsWith("/"))
                    {
                        path = path.Insert(0, "/");
                    }

                    var uri = baseUrl + path;

                    var returnType = methodModel.Method.ReturnType;

                    if (returnType.IsGenericType && typeof(Task).IsAssignableFrom(returnType))
                    {
                        returnType = returnType.GenericTypeArguments[0];
                    }

                    var methodDescriptor = new MethodDescriptor
                    {
                        ClienType = typeModel.Type,
                        Codec     = methodModel.Codec,
                        InterceptorDescriptors = interceptorDescriptors,
                        Method      = goRequestAttribute.Method,
                        MethodInfo  = methodModel.Method,
                        ReturnType  = returnType,
                        UrlTemplate = new TemplateString(uri, TemplateUtilities.GetVariables(uri))
                    };

                    var parameterModels = new List <ParameterDescriptor>(methodModel.Parameters.Count);
                    foreach (var parameterModel in methodModel.Parameters)
                    {
                        var parameterTarget     = GetParameterTarget(methodDescriptor, parameterModel);
                        var parameterDescriptor = new ParameterDescriptor
                        {
                            Name           = parameterModel.ParameterName,
                            ParameterType  = parameterModel.ParameterInfo.ParameterType,
                            FormattingInfo = new ParameterFormattingInfo
                            {
                                FormatterName = GetFormatterName(parameterModel),
                                FormatterType = parameterModel.Attributes.OfType <CustomFormatterAttribute>().LastOrDefault()?.FormatterType,
                                Target        = parameterTarget
                            }
                        };

                        parameterModels.Add(parameterDescriptor);
                    }

                    methodDescriptor.Parameters = parameterModels;

                    descriptors.Add(methodDescriptor);
                }
            }

            return(descriptors);
        }
示例#3
0
 public TemplateString(string template)
 {
     Template  = template;
     Variables = TemplateUtilities.GetVariables(template);
     NeedParse = Variables != null && Variables.Any();
 }