예제 #1
0
        internal ThriftMethodMetadata(String serviceName, MethodInfo method, ThriftCatalog catalog)
        {
            Guard.ArgumentNullOrWhiteSpaceString(serviceName, nameof(serviceName));
            Guard.ArgumentNotNull(method, nameof(method));
            Guard.ArgumentNotNull(catalog, nameof(catalog));

            this.Order = (ThriftCatalog.GetMethodOrder(method) ?? int.MaxValue);

            ThriftMethodAttribute thriftMethod = method.GetCustomAttribute <ThriftMethodAttribute>();

            if (thriftMethod == null)
            {
                throw new ArgumentException($"Method '{method.DeclaringType.Name}.{method.Name}' is not annotated with {nameof(ThriftMethodAttribute)}.", nameof(method));
            }
            if (method.IsStatic)
            {
                throw new ArgumentException($"Method '{method.DeclaringType.Name}.{method.Name} is a static method.", nameof(method));
            }

            this.Name = String.IsNullOrWhiteSpace(thriftMethod.Name) ? method.Name : thriftMethod.Name.Trim();

            this.QualifiedName = GetQualifiedName(serviceName, Name);

            //this.QualifiedName = $"{serviceName}.{this.Name}";
            this.ReturnType = catalog.GetThriftType(method.ReturnType);

            var builder    = ImmutableList.CreateBuilder <ThriftFieldMetadata>();
            var parameters = method.GetParameters();
            int index      = 0;

            foreach (var p in parameters)
            {
                ThriftFieldMetadata fieldMetadata = CreateFieldMetadata(catalog, index, p);
                builder.Add(fieldMetadata);
                index++;
            }
            this.Parameters = builder.ToImmutableList();
            this.IsOneWay   = thriftMethod.OneWay;
            this.Method     = method;
            this.Exceptions = BuildExceptions(catalog, method);
        }
예제 #2
0
        private static ThriftFieldMetadata CreateFieldMetadata(ThriftCatalog catalog, int index, ParameterInfo parameterInfo)
        {
            ThriftFieldAttribute thriftField   = parameterInfo.GetCustomAttribute <ThriftFieldAttribute>();
            short        parameterId           = short.MinValue;
            String       parameterName         = parameterInfo.Name;
            Requiredness parameterRequiredness = Requiredness.Unspecified;

            if (thriftField != null)
            {
                parameterId           = thriftField.Id;
                parameterRequiredness = thriftField.Required;
                if (!String.IsNullOrWhiteSpace(thriftField.Name))
                {
                    parameterName = thriftField.Name.Trim();
                }
            }
            if (parameterId == short.MinValue)
            {
                parameterId = (short)(index + 1);
            }
            ThriftType thriftType         = catalog.GetThriftType(parameterInfo.ParameterType);
            var        parameterInjection = new ThriftParameterInjection(parameterId, parameterName, index, parameterInfo.ParameterType);

            if (parameterRequiredness == Requiredness.Unspecified)
            {
                // There is only one field injection used to build metadata for method parameters, and if a
                // single injection point has UNSPECIFIED requiredness, that resolves to NONE.
                parameterRequiredness = Requiredness.None;
            }
            ThriftFieldMetadata fieldMetadata = new ThriftFieldMetadata(
                parameterId,
                false /* recursiveness */,
                parameterRequiredness,
                new DefaultThriftTypeReference(thriftType),
                parameterName,
                FieldKind.ThriftField,
                new IThriftInjection[] { parameterInjection });

            return(fieldMetadata);
        }
예제 #3
0
        private IDictionary <short, ThriftType> BuildExceptions(ThriftCatalog catalog, MethodInfo method)
        {
            var            exceptions     = ImmutableDictionary.CreateBuilder <short, ThriftType>();
            HashSet <Type> exceptionTypes = new HashSet <Type>();

            var exceptionAttributes = method.GetCustomAttributes <ThriftExceptionAttribute>(true);

            foreach (var thriftException in exceptionAttributes)
            {
                if (!exceptionTypes.Add(thriftException.ExceptionType))
                {
                    throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} contains more than one value for {thriftException.ExceptionType} .");
                }
                if (exceptions.ContainsKey(thriftException.Id))
                {
                    throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} has duplicate id: {thriftException.Id} .");
                }
                exceptions.Add(thriftException.Id, catalog.GetThriftType(thriftException.ExceptionType));
            }

            foreach (var exceptionType in exceptionAttributes.Select(a => a.ExceptionType))
            {
                if (exceptionType.GetTypeInfo().IsAssignableFrom(typeof(TException)))
                {
                    // the built-in exception types don't need special treatment
                    continue;
                }
                ThriftStructAttribute attribute = exceptionType.GetTypeInfo().GetCustomAttribute <ThriftStructAttribute>();
                if (attribute == null)
                {
                    throw new ThriftyException($"ThriftExceptionAttribute on method {method.DeclaringType}.{method.Name} with {exceptionType.FullName} need {nameof(ThriftStructAttribute)} .");
                }
            }

            return(exceptions.ToImmutableDictionary());
        }