Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        //private IEnumerable<String> documentation;

        public ThriftFieldMetadata(
            short id,
            bool isRecursiveReference,
            Requiredness requiredness,
            //IEnumerable<KeyValuePair<String, String>> idlAnnotations,
            IThriftTypeReference thriftTypeReference,
            String name,
            FieldKind fieldKind,
            IEnumerable <IThriftInjection> injections       = null,
            ThriftConstructorInjection constructorInjection = null,
            ThriftMethodInjection methodInjection           = null,
            IThriftExtraction extraction = null,
            TypeCoercion coercion        = null
            )
        {
            Guard.ArgumentNotNull(thriftTypeReference, nameof(thriftTypeReference));
            Guard.ArgumentNotNull(fieldKind, nameof(fieldKind));
            Guard.ArgumentNullOrWhiteSpaceString(name, nameof(name));
            Guard.ArgumentNotNull(thriftTypeReference, nameof(thriftTypeReference));
            Guard.ArgumentNullOrWhiteSpaceString(name, nameof(name));
            //Guard.ArgumentNotNull(constructorInjection, nameof(constructorInjection));
            //Guard.ArgumentNotNull(methodInjection, nameof(methodInjection));
            //Guard.ArgumentNotNull(extraction, nameof(extraction));
            //Guard.ArgumentNotNull(coercion, nameof(coercion));

            this.IsRecursiveReference = isRecursiveReference;
            this.Required             = requiredness;
            this._thriftTypeReference = thriftTypeReference;
            this._fieldKind           = fieldKind;
            this.Name                  = name;
            this.Injections            = injections ?? Enumerable.Empty <IThriftInjection>();
            this._constructorInjection = constructorInjection;
            this._methodInjection      = methodInjection;

            this._extraction = extraction;
            this._coercion   = coercion;

            switch (fieldKind)
            {
            case FieldKind.ThriftField:
                Guard.ArgumentCondition(id >= 0, "isLegacyId must be specified on fields with negative IDs", nameof(id));

                break;

            case FieldKind.ThriftUnionId:
                Guard.ArgumentCondition(id == short.MinValue, "thrift union id must be short.MinValue", nameof(id));
                break;
            }

            Guard.ArgumentCondition(injections.Any() ||
                                    extraction != null ||
                                    constructorInjection != null ||
                                    methodInjection != null, "A thrift field must have an injection or extraction point");

            this.Id = id;

            if (extraction != null)
            {
                if (extraction is ThriftFieldExtractor)
                {
                    ThriftFieldExtractor e = (ThriftFieldExtractor)extraction;
                    //this.documentation = ThriftCatalog.getThriftDocumentation(e.getField());
                }
                else if (extraction != null && extraction is ThriftMethodExtractor)
                {
                    ThriftMethodExtractor e = (ThriftMethodExtractor)extraction;
                    //this.documentation = ThriftCatalog.getThriftDocumentation(e.getMethod());
                }
            }

            //this.idlAnnotations = idlAnnotations;
        }