Exemple #1
0
        private static EventPropertyGetterSPI PropertyGetterNested(
            GetterNestedFactory factory,
            Schema fieldSchema,
            Property property,
            string moduleName,
            EventBeanTypedEventFactory eventAdapterService,
            EventTypeAvroHandler eventTypeAvroHandler,
            AvroEventTypeFragmentTypeCache fragmentTypeCache)
        {
            if (property is SimpleProperty) {
                Field fieldNested = fieldSchema.GetField(property.PropertyNameAtomic);
                if (fieldNested == null) {
                    return null;
                }

                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    fieldNested.Schema,
                    moduleName,
                    eventAdapterService,
                    eventTypeAvroHandler,
                    fragmentTypeCache);
                return factory.MakeSimple(
                    fieldNested,
                    fragmentEventType?.FragmentType,
                    AvroTypeUtil.PropertyType(fieldNested.Schema));
            }

            if (property is IndexedProperty indexedProperty) {
                var fieldNested = fieldSchema.GetField(indexedProperty.PropertyNameAtomic);
                if (fieldNested == null) {
                    return null;
                }

                if ((fieldNested.Schema.Tag != Schema.Type.Array) &&
                    (fieldNested.Schema.Tag != Schema.Type.String)) {
                    return null;
                }

                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    fieldNested.Schema,
                    moduleName,
                    eventAdapterService,
                    eventTypeAvroHandler,
                    fragmentTypeCache);
                
                return factory.MakeIndexed(fieldNested, indexedProperty.Index, fragmentEventType?.FragmentType);
            }

            if (property is MappedProperty mappedProperty) {
                Field fieldNested = fieldSchema.GetField(mappedProperty.PropertyNameAtomic);
                if (fieldNested == null || fieldNested.Schema.Tag != Schema.Type.Map) {
                    return null;
                }

                return factory.MakeMapped(fieldNested, mappedProperty.Key);
            }

            if (property is DynamicProperty) {
                if (property is DynamicSimpleProperty) {
                    return factory.MakeDynamicSimple(property.PropertyNameAtomic);
                }

                throw new NotSupportedException();
            }

            var nested = (NestedProperty) property;
            var allSimple = true;
            foreach (var levelProperty in nested.Properties) {
                if (!(levelProperty is SimpleProperty)) {
                    allSimple = false;
                    break;
                }
            }

            if (allSimple) {
                var currentSchema = fieldSchema;
                var count = 0;
                var path = new Field[nested.Properties.Count];
                var types = new Type[nested.Properties.Count];
                foreach (var levelProperty in nested.Properties) {
                    if (currentSchema.Tag != Schema.Type.Record) {
                        return null;
                    }

                    Field fieldNested = currentSchema.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldNested == null) {
                        return null;
                    }

                    currentSchema = fieldNested.Schema;
                    path[count] = fieldNested;
                    types[count] = AvroTypeUtil.PropertyType(currentSchema);
                    count++;
                }

                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    currentSchema,
                    moduleName,
                    eventAdapterService,
                    eventTypeAvroHandler,
                    fragmentTypeCache);
                return factory.MakeNestedSimpleMultiLevel(path, types, fragmentEventType?.FragmentType);
            }

            var getters = new AvroEventPropertyGetter[nested.Properties.Count];
            var countX = 0;
            var currentSchemaX = fieldSchema;
            foreach (var levelProperty in nested.Properties) {
                if (currentSchemaX == null) {
                    return null;
                }

                if (levelProperty is SimpleProperty) {
                    Field fieldNested = currentSchemaX.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldNested == null) {
                        return null;
                    }

                    var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                        fieldNested.Schema,
                        moduleName,
                        eventAdapterService,
                        eventTypeAvroHandler,
                        fragmentTypeCache);
                    var propertyType = AvroTypeUtil.PropertyType(fieldNested.Schema);
                    getters[countX] = new AvroEventBeanGetterSimple(
                        fieldNested,
                        fragmentEventType?.FragmentType,
                        eventAdapterService,
                        propertyType);
                    currentSchemaX = fieldNested.Schema;
                }
                else if (levelProperty is IndexedProperty indexed) {
                    var fieldIndexed = currentSchemaX.GetField(indexed.PropertyNameAtomic);
                    if (fieldIndexed == null) {
                        return null;
                    }

                    if ((fieldIndexed.Schema.Tag != Schema.Type.Array) &&
                        (fieldIndexed.Schema.Tag != Schema.Type.String)) {
                        return null;
                    }

                    var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                        fieldIndexed.Schema,
                        moduleName,
                        eventAdapterService,
                        eventTypeAvroHandler,
                        fragmentTypeCache);
                    getters[countX] = new AvroEventBeanGetterIndexed(
                        fieldIndexed,
                        indexed.Index,
                        fragmentEventType?.FragmentType,
                        eventAdapterService);
                    currentSchemaX = fieldIndexed.Schema.AsArraySchema().ItemSchema;
                }
                else if (levelProperty is MappedProperty mapped) {
                    Field fieldMapped = currentSchemaX.GetField(mapped.PropertyNameAtomic);
                    if (fieldMapped == null || fieldMapped.Schema.Tag != Schema.Type.Map) {
                        return null;
                    }

                    getters[countX] = new AvroEventBeanGetterMapped(fieldMapped, mapped.Key);
                    currentSchemaX = fieldMapped.Schema;
                }
                else if (levelProperty is DynamicSimpleProperty) {
                    if (currentSchemaX.Tag != Schema.Type.Record) {
                        return null;
                    }

                    Field fieldDynamic = currentSchemaX.GetField(levelProperty.PropertyNameAtomic);
                    getters[countX] = new AvroEventBeanGetterSimpleDynamic(levelProperty.PropertyNameAtomic);
                    if (fieldDynamic.Schema.Tag == Schema.Type.Record) {
                        currentSchemaX = fieldDynamic.Schema;
                    }
                    else if (fieldDynamic.Schema.Tag == Schema.Type.Union) {
                        currentSchemaX = AvroSchemaUtil.FindUnionRecordSchemaSingle(fieldDynamic.Schema);
                    }
                }
                else {
                    throw new NotSupportedException();
                }

                countX++;
            }

            return factory.MakeNestedPolyMultiLevel(getters);
        }
Exemple #2
0
        private static EventPropertyGetter PropertyGetterNested(
            GetterNestedFactory factory,
            Schema fieldSchema,
            Property property,
            EventAdapterService eventAdapterService)
        {
            if (property is SimpleProperty)
            {
                Field fieldNested = fieldSchema.GetField(property.PropertyNameAtomic);
                if (fieldNested == null)
                {
                    return(null);
                }
                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    fieldNested.Schema, eventAdapterService);
                return(factory.MakeSimple(
                           fieldNested, fragmentEventType == null ? null : fragmentEventType.FragmentType));
            }

            if (property is IndexedProperty)
            {
                var   indexed     = (IndexedProperty)property;
                Field fieldNested = fieldSchema.GetField(property.PropertyNameAtomic);
                if (fieldNested == null || fieldNested.Schema.Tag != Schema.Type.Array)
                {
                    return(null);
                }
                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    fieldNested.Schema, eventAdapterService);
                return(factory.MakeIndexed(
                           fieldNested, indexed.Index, fragmentEventType == null ? null : fragmentEventType.FragmentType));
            }

            if (property is MappedProperty)
            {
                var   mapped      = (MappedProperty)property;
                Field fieldNested = fieldSchema.GetField(property.PropertyNameAtomic);
                if (fieldNested == null || fieldNested.Schema.Tag != Schema.Type.Map)
                {
                    return(null);
                }
                return(factory.MakeMapped(fieldNested, mapped.Key));
            }

            if (property is DynamicProperty)
            {
                if (property is DynamicSimpleProperty)
                {
                    return(factory.MakeDynamicSimple(property.PropertyNameAtomic));
                }
                throw new UnsupportedOperationException();
            }

            var nested    = (NestedProperty)property;
            var allSimple = true;

            foreach (var levelProperty in nested.Properties)
            {
                if (!(levelProperty is SimpleProperty))
                {
                    allSimple = false;
                    break;
                }
            }
            if (allSimple)
            {
                var currentSchemaX = fieldSchema;
                var countX         = 0;
                var path           = new Field[nested.Properties.Count];
                foreach (var levelProperty in nested.Properties)
                {
                    if (currentSchemaX.Tag != Schema.Type.Record)
                    {
                        return(null);
                    }
                    Field fieldNested = currentSchemaX.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldNested == null)
                    {
                        return(null);
                    }
                    currentSchemaX = fieldNested.Schema;
                    path[countX]   = fieldNested;
                    countX++;
                }
                var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                    currentSchemaX, eventAdapterService);
                return(factory.MakeNestedSimpleMultiLevel(
                           path, fragmentEventType == null ? null : fragmentEventType.FragmentType));
            }

            var getters       = new AvroEventPropertyGetter[nested.Properties.Count];
            var count         = 0;
            var currentSchema = fieldSchema;

            foreach (var levelProperty in nested.Properties)
            {
                if (currentSchema == null)
                {
                    return(null);
                }

                if (levelProperty is SimpleProperty)
                {
                    Field fieldNested = currentSchema.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldNested == null)
                    {
                        return(null);
                    }
                    FragmentEventType fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                        fieldNested.Schema, eventAdapterService);
                    getters[count] = new AvroEventBeanGetterSimple(
                        fieldNested, fragmentEventType == null ? null : fragmentEventType.FragmentType,
                        eventAdapterService);
                    currentSchema = fieldNested.Schema;
                }
                else if (levelProperty is IndexedProperty)
                {
                    var   indexed      = (IndexedProperty)levelProperty;
                    Field fieldIndexed = currentSchema.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldIndexed == null || fieldIndexed.Schema.Tag != Schema.Type.Array)
                    {
                        return(null);
                    }
                    var fragmentEventType = AvroFragmentTypeUtil.GetFragmentEventTypeForField(
                        fieldIndexed.Schema, eventAdapterService);
                    getters[count] = new AvroEventBeanGetterIndexed(
                        fieldIndexed, indexed.Index,
                        fragmentEventType == null ? null : fragmentEventType.FragmentType, eventAdapterService);
                    currentSchema = fieldIndexed.Schema.GetElementType();
                }
                else if (levelProperty is MappedProperty)
                {
                    var   mapped      = (MappedProperty)levelProperty;
                    Field fieldMapped = currentSchema.GetField(levelProperty.PropertyNameAtomic);
                    if (fieldMapped == null || fieldMapped.Schema.Tag != Schema.Type.Map)
                    {
                        return(null);
                    }
                    getters[count] = new AvroEventBeanGetterMapped(fieldMapped, mapped.Key);
                    currentSchema  = fieldMapped.Schema;
                }
                else if (levelProperty is DynamicSimpleProperty)
                {
                    if (currentSchema.Tag != Schema.Type.Record)
                    {
                        return(null);
                    }
                    Field fieldDynamic = currentSchema.GetField(levelProperty.PropertyNameAtomic);
                    getters[count] = new AvroEventBeanGetterSimpleDynamic(levelProperty.PropertyNameAtomic);
                    if (fieldDynamic.Schema.Tag == Schema.Type.Record)
                    {
                        currentSchema = fieldDynamic.Schema;
                    }
                    else if (fieldDynamic.Schema.Tag == Schema.Type.Union)
                    {
                        currentSchema = AvroSchemaUtil.FindUnionRecordSchemaSingle(fieldDynamic.Schema);
                    }
                }
                else
                {
                    throw new UnsupportedOperationException();
                }
                count++;
            }
            return(factory.MakeNestedPolyMultiLevel(getters));
        }