Beispiel #1
0
        private HttpServiceInfo(ServiceInfo serviceInfo)
        {
            Service = serviceInfo;

            foreach (var parameter in GetHttpParameters(serviceInfo))
            {
                if (parameter.Name == "url")
                {
                    Url = parameter.Value;
                }
                else
                {
                    AddInvalidHttpParameterError(parameter);
                }
            }

            foreach (var descendant in serviceInfo.GetElementAndDescendants().OfType <ServiceElementWithAttributesInfo>())
            {
                var httpAttributes = descendant.GetAttributes("http");
                if (httpAttributes.Count != 0)
                {
                    if (!(descendant is ServiceInfo || descendant is ServiceMethodInfo || descendant is ServiceFieldInfo || descendant is ServiceErrorSetInfo || descendant is ServiceErrorInfo))
                    {
                        AddValidationError(ServiceDefinitionUtility.CreateUnexpectedAttributeError(httpAttributes[0]));
                    }
                    else if (httpAttributes.Count > 1)
                    {
                        AddValidationError(ServiceDefinitionUtility.CreateDuplicateAttributeError(httpAttributes[1]));
                    }
                }
            }

            Methods   = serviceInfo.Methods.Select(x => new HttpMethodInfo(x, serviceInfo)).ToList();
            ErrorSets = serviceInfo.ErrorSets.Select(x => new HttpErrorSetInfo(x)).ToList();

            var methodsByRoute = Methods.OrderBy(x => x, HttpMethodInfo.ByRouteComparer).ToList();

            for (int index = 1; index < methodsByRoute.Count; index++)
            {
                var left  = methodsByRoute[index - 1];
                var right = methodsByRoute[index];
                if (HttpMethodInfo.ByRouteComparer.Compare(left, right) == 0)
                {
                    AddValidationError(new ServiceDefinitionError($"Methods '{left.ServiceMethod.Name}' and '{right.ServiceMethod.Name}' have the same route: {right.Method} {right.Path}", right.ServiceMethod.Position));
                }
            }
        }
        private CSharpServiceInfo(ServiceInfo serviceInfo, out IReadOnlyList <ServiceDefinitionError> errors)
        {
            Service = serviceInfo;
            m_fieldPropertyNames = new Dictionary <ServiceFieldInfo, string>();

            var validationErrors = new List <ServiceDefinitionError>();

            foreach (var descendant in serviceInfo.GetElementAndDescendants().OfType <ServiceElementWithAttributesInfo>())
            {
                var csharpAttributes = descendant.GetAttributes("csharp");
                if (csharpAttributes.Count == 1)
                {
                    var csharpAttribute = csharpAttributes[0];
                    if (descendant is ServiceInfo || descendant is ServiceFieldInfo)
                    {
                        foreach (var parameter in csharpAttribute.Parameters)
                        {
                            if (parameter.Name == "namespace" && descendant is ServiceInfo)
                            {
                                m_namespace = parameter.Value;
                            }
                            else if (parameter.Name == "name" && descendant is ServiceFieldInfo field)
                            {
                                m_fieldPropertyNames[field] = parameter.Value;
                            }
                            else
                            {
                                validationErrors.Add(ServiceDefinitionUtility.CreateUnexpectedAttributeParameterError(csharpAttribute.Name, parameter));
                            }
                        }
                    }
                    else
                    {
                        validationErrors.Add(ServiceDefinitionUtility.CreateUnexpectedAttributeError(csharpAttribute));
                    }
                }
                else if (csharpAttributes.Count > 1)
                {
                    validationErrors.Add(ServiceDefinitionUtility.CreateDuplicateAttributeError(csharpAttributes[1]));
                }
            }

            var typeName = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
            {
                CSharpUtility.GetInterfaceName(serviceInfo)
            };

            void CheckTypeName(string name, ServiceDefinitionPosition?position)
            {
                if (!typeName !.Add(name))
                {
                    validationErrors !.Add(new ServiceDefinitionError($"Element generates duplicate C# type '{name}'.", position));
                }
            }

            foreach (var member in serviceInfo.Members)
            {
                if (member is ServiceMethodInfo method)
                {
                    CheckTypeName(CSharpUtility.GetRequestDtoName(method), method.Position);
                    CheckTypeName(CSharpUtility.GetResponseDtoName(method), method.Position);
                }
                else if (member is ServiceDtoInfo dto)
                {
                    CheckTypeName(CSharpUtility.GetDtoName(dto), dto.Position);
                }
                else if (member is ServiceEnumInfo @enum)
                {
                    CheckTypeName(CSharpUtility.GetEnumName(@enum), @enum.Position);
                }
                else if (member is ServiceErrorSetInfo errorSet)
                {
                    CheckTypeName(CSharpUtility.GetErrorSetName(errorSet), errorSet.Position);
                }
                else
                {
                    throw new InvalidOperationException($"Unknown member type {member.GetType().FullName}");
                }
            }

            errors = validationErrors;
        }