Esempio n. 1
0
        protected IEnumerable <ParameterInjection> GetParameterInjections(Type structType, ParameterInfo[] parametersList)
        {
            List <ParameterInjection> parameters = new List <ParameterInjection>(parametersList.Length);

            for (int parameterIndex = 0; parameterIndex < parametersList.Length; parameterIndex++)
            {
                var p = parametersList[parameterIndex];
                ThriftFieldAttribute thriftField = p.GetCustomAttribute <ThriftFieldAttribute>();

                /*
                 * 这里以属性上的 ThriftFieldAttribute 优先,否则将双写 ThriftFieldAttribute 上的各种属性,例如 Required, Name,这将非常麻烦。
                 * 或许,最合理的方案应该是定义一个 ThriftParameterAttribute,只给一个 id 属性 ?
                 * JAVA 字段名和构造函数参数均是小驼峰,不存在此问题,但依然存在 Required 必须一致的问题,也很麻烦, 优先使用字段配置可以缓解这个问题。
                 */
                string extractedName = p.Name;
                if (thriftField != null)
                {
                    var field = _fields.FirstOrDefault(f => f.Id == thriftField.Id);
                    thriftField   = field?.Annotation ?? thriftField;
                    extractedName = field?.ExtractName() ?? p.Name;
                }

                ParameterInjection parameterInjection = new ParameterInjection(
                    structType,
                    parameterIndex,
                    thriftField,
                    extractedName,
                    p.ParameterType
                    );

                parameters.Add(parameterInjection);
            }
            return(parameters);
        }
Esempio n. 2
0
        protected override ThriftFieldMetadata BuildField(IEnumerable <FieldMetadata> input)
        {
            short id = -1;
            //IDictionary<String, String> idlAnnotations = null;
            String               name                = null;
            Requiredness         requiredness        = Requiredness.Unspecified;
            bool                 recursive           = false;
            IThriftTypeReference thriftTypeReference = null;

            // process field injections and extractions
            List <IThriftInjection> injections = new List <IThriftInjection>();
            IThriftExtraction       extraction = null;

            foreach (FieldMetadata fieldMetadata in input)
            {
                id           = fieldMetadata.Id;
                name         = fieldMetadata.Name;
                recursive    = fieldMetadata.IsRecursiveReference ?? false;
                requiredness = fieldMetadata.Requiredness;
                //idlAnnotations = fieldMetadata.getIdlAnnotations();
                thriftTypeReference = this.Catalog.GetFieldThriftTypeReference(fieldMetadata);

                FieldInjection fieldInjection = fieldMetadata as FieldInjection;
                if (fieldInjection != null)
                {
                    injections.Add(new ThriftFieldInjection(fieldInjection.Id,
                                                            fieldInjection.Name,
                                                            fieldInjection.Field,
                                                            fieldInjection.Type));
                }
                else if (fieldMetadata is ParameterInjection)
                {
                    ParameterInjection parameterInjection = (ParameterInjection)fieldMetadata;
                    injections.Add(new ThriftParameterInjection(
                                       parameterInjection.Id,
                                       parameterInjection.Name,
                                       parameterInjection.ParameterIndex,
                                       fieldMetadata.CSharpType
                                       ));
                }
                else if (fieldMetadata is FieldExtractor)
                {
                    FieldExtractor fieldExtractor = (FieldExtractor)fieldMetadata;
                    extraction = new ThriftFieldExtractor(
                        fieldExtractor.Id, fieldExtractor.Name,
                        fieldExtractor.Type,
                        fieldExtractor.Field,
                        fieldExtractor.CSharpType);
                }
                else if (fieldMetadata is MethodExtractor)
                {
                    MethodExtractor methodExtractor = (MethodExtractor)fieldMetadata;
                    extraction = new ThriftMethodExtractor(
                        methodExtractor.Id,
                        methodExtractor.Name,
                        methodExtractor.Type,
                        methodExtractor.Method,
                        methodExtractor.CSharpType);
                }
            }

            // add type coercion
            TypeCoercion coercion = null;

            if (!thriftTypeReference.Recursive && thriftTypeReference.Get().IsCoerced)
            {
                coercion = this.Catalog.GetDefaultCoercion(thriftTypeReference.Get().CSharpType);
            }

            if (recursive && requiredness != Requiredness.Optional)
            {
                this.MetadataErrors.AddError($"Struct '{this.StructName}' field '{name}' is recursive but not marked optional");
            }

            ThriftFieldMetadata thriftFieldMetadata = new ThriftFieldMetadata(
                id,
                recursive,
                requiredness,
                //idlAnnotations,
                thriftTypeReference,
                name,
                FieldKind.ThriftField,
                injections,
                extraction: extraction,
                coercion: coercion
                );

            return(thriftFieldMetadata);
        }