Exemplo n.º 1
0
        protected void AddField(PropertyInfo fieldField)
        {
            var att = fieldField.GetCustomAttribute <ThriftFieldAttribute>();

            if (att == null)
            {
                throw new ThriftyException($"{fieldField.DeclaringType}.{fieldField.Name} has no {nameof(ThriftFieldAttribute)}");
            }
            if (fieldField.CanRead)
            {
                FieldExtractor fieldExtractor = new FieldExtractor(fieldField, att, FieldKind.ThriftField);
                _fields.Add(fieldExtractor);
                _extractors.Add(fieldExtractor);
            }
            if (fieldField.CanWrite)
            {
                FieldInjection fieldInjection = new FieldInjection(fieldField, att, FieldKind.ThriftField);
                _fields.Add(fieldInjection);
                _fieldInjections.Add(fieldInjection);
            }
        }
Exemplo 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);
        }