Пример #1
0
        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType() != this.GetType())
            {
                return(false);
            }

            SharkPath other = (SharkPath)obj;

            if (mMethods != other.mMethods)
            {
                return(false);
            }

            List <PathPart> otherParts = other.mParts;

            if (otherParts.Count != mParts.Count)
            {
                return(false);
            }

            for (int i = 0; i < mParts.Count; ++i)
            {
                if (!mParts[i].Equals(otherParts[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        private void BuildRouteMap()
        {
            foreach (MethodInfo info in typeof(T).GetMethods())
            {
                IEnumerable <PathAttribute> getAttributes = info.GetCustomAttributes <PathAttribute>();
                if (getAttributes.Count() > 0)
                {
                    ValidateParameters(info);

                    if (info.ReturnType != typeof(Response))
                    {
                        throw new ArgumentException($"Method {info.Name} doesn't return a Response.");
                    }
                }

                foreach (PathAttribute attribute in getAttributes)
                {
                    string     route       = attribute.Path;
                    HttpMethod httpMethods = 0;
                    foreach (string method in attribute.Methods)
                    {
                        if (method.Equals("get", StringComparison.OrdinalIgnoreCase))
                        {
                            httpMethods |= HttpMethod.GET;
                        }
                        else if (method.Equals("post", StringComparison.OrdinalIgnoreCase))
                        {
                            httpMethods |= HttpMethod.POST;
                        }
                        else if (method.Equals("put", StringComparison.OrdinalIgnoreCase))
                        {
                            httpMethods |= HttpMethod.PUT;
                        }
                        else if (method.Equals("patch", StringComparison.OrdinalIgnoreCase))
                        {
                            httpMethods |= HttpMethod.PATCH;
                        }
                        else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase))
                        {
                            httpMethods |= HttpMethod.DELETE;
                        }
                        else
                        {
                            throw new ArgumentException($"Invalid http method {method} in path attribute for method {info.Name}.");
                        }
                    }

                    SharkPath path = new SharkPath(route, httpMethods);
                    Dictionary <string, Type> variables  = path.GetVariableTypes();
                    ParameterInfo[]           paramInfos = info.GetParameters();

                    if (variables.Count != paramInfos.Length)
                    {
                        throw new ArgumentException($"Method {info.Name} has a mismatched number of parameters in the method signature and in the path.");
                    }

                    foreach (ParameterInfo param in paramInfos)
                    {
                        Type varType;
                        if (!variables.TryGetValue(param.Name, out varType))
                        {
                            throw new ArgumentException($"Parameter {param.Name} to method {info.Name} does not exist in the path.");
                        }

                        if (varType != param.ParameterType)
                        {
                            throw new ArgumentException($"Parameter {param.Name} of method {info.Name} has mismatched types with the path.");
                        }
                    }

                    mMethods.Add(path, info);
                }
            }
        }