public EventBean GetEvent(XmlNode result) { if (eventType == null) { var candidateEventType = eventTypeResolver.GetTypeByName(eventTypeName); if (candidateEventType == null) { Log.Warn( "Event type by name '" + eventTypeName + "' was not found for property '" + propertyName + "'"); return null; } if (!(candidateEventType is BaseXMLEventType)) { Log.Warn( "Event type by name '" + eventTypeName + "' is not an XML event type for property '" + propertyName + "'"); return null; } eventType = candidateEventType; } return eventBeanTypedEventFactory.AdapterForTypedDOM(result, eventType); }
public static EventType Resolve( EventTypeMetadata metadata, EventTypeNameResolver publics, IDictionary<string, EventType> locals, PathRegistry<string, EventType> path) { EventTypeSPI type; // public can only see public if (metadata.AccessModifier == NameAccessModifier.PRECONFIGURED) { type = (EventTypeSPI) publics.GetTypeByName(metadata.Name); // for create-schema the type may be defined by the same module if (type == null) { type = (EventTypeSPI) locals.Get(metadata.Name); } } else if (metadata.AccessModifier == NameAccessModifier.PUBLIC || metadata.AccessModifier == NameAccessModifier.INTERNAL) { // path-visibility can be provided as local var local = locals.Get(metadata.Name); if (local != null) { if (local.Metadata.AccessModifier == NameAccessModifier.PUBLIC || local.Metadata.AccessModifier == NameAccessModifier.INTERNAL) { return (EventTypeSPI) local; } } try { var pair = path.GetAnyModuleExpectSingle( metadata.Name, Collections.SingletonSet(metadata.ModuleName)); type = (EventTypeSPI) pair?.First; } catch (PathException e) { throw new EPException(e.Message, e); } } else { type = (EventTypeSPI) locals.Get(metadata.Name); } if (type == null) { throw new EPException( "Failed to find event type '" + metadata.Name + "' among public types, modules-in-path or the current module itself"); } return type; }
public EventType GetTypeByName(string typeName) { var localType = locals.Get(typeName); if (localType != null) { return localType; } var publicType = publics.GetTypeByName(typeName); if (publicType != null) { return publicType; } try { var pair = path.GetAnyModuleExpectSingle(typeName, null); return pair?.First; } catch (PathException e) { throw new EPException("Event type name '" + typeName + "' is ambiguous: " + e.Message, e); } }
public static LinkedHashMap<string, object> ResolvePropertyTypes( IDictionary<string, object> propertyTypes, EventTypeNameResolver eventTypeNameResolver) { var verified = new LinkedHashMap<string, object>(); foreach (var prop in propertyTypes) { var propertyName = prop.Key; var propertyType = prop.Value; if (propertyType is Type || propertyType is EventType || propertyType == null || propertyType is TypeBeanOrUnderlying) { verified.Put(propertyName, propertyType); continue; } if (propertyType is EventType[]) { var types = (EventType[]) propertyType; if (types.Length != 1 || types[0] == null) { throw new ArgumentException("Invalid null event type array"); } verified.Put(propertyName, propertyType); continue; } if (propertyType is TypeBeanOrUnderlying[]) { var types = (TypeBeanOrUnderlying[]) propertyType; if (types.Length != 1 || types[0] == null) { throw new ArgumentException("Invalid null event type array"); } verified.Put(propertyName, propertyType); continue; } if (propertyType is IDictionary<string, object>) { IDictionary<string, object> inner = ResolvePropertyTypes( (IDictionary<string, object>) propertyType, eventTypeNameResolver); verified.Put(propertyName, inner); continue; } if (!(propertyType is string propertyTypeName)) { throw MakeUnexpectedTypeException(propertyType.ToString(), propertyName); } var isArray = EventTypeUtility.IsPropertyArray(propertyTypeName); if (isArray) { propertyTypeName = EventTypeUtility.GetPropertyRemoveArray(propertyTypeName); } var eventType = eventTypeNameResolver.GetTypeByName(propertyTypeName); if (!(eventType is BaseNestableEventType) && !(eventType is BeanEventType)) { var clazz = TypeHelper.GetPrimitiveTypeForName(propertyTypeName); if (clazz != null) { verified.Put(propertyName, clazz); continue; } throw MakeUnexpectedTypeException(propertyTypeName, propertyName); } if (eventType is BaseNestableEventType) { var typeEntity = !isArray ? new TypeBeanOrUnderlying(eventType) : (object) new[] {new TypeBeanOrUnderlying(eventType)}; verified.Put(propertyName, typeEntity); continue; } var beanEventType = (BeanEventType) eventType; object type = !isArray ? beanEventType.UnderlyingType : TypeHelper.GetArrayType(beanEventType.UnderlyingType); verified.Put(propertyName, type); } return verified; }
public static void AssembleField( string propertyName, object propertyType, JArray assembler, Attribute[] annotations, ConfigurationCommonEventTypeMeta.AvroSettingsConfig avroSettings, EventTypeNameResolver eventTypeNameResolver, string statementName, TypeRepresentationMapper optionalMapper) { if (propertyName.Contains(".")) { throw new EPException( "Invalid property name as Avro does not allow dot '.' in field names (property '" + propertyName + "')"); } Schema schema = GetAnnotationSchema(propertyName, annotations); if (schema != null) { assembler.Add(TypeBuilder.Field(propertyName, schema)); // assembler.Name(propertyName).Type(schema).NoDefault(); return; } if (optionalMapper != null && propertyType is Type propertyTypeType) { var schemaResult = optionalMapper.Map( new TypeRepresentationMapperContext(propertyTypeType, propertyName, statementName)); if (schemaResult is JToken schemaAsJsonToken) { assembler.Add(TypeBuilder.Field(propertyName, schemaAsJsonToken)); return; } if (schemaResult is Schema schemaFromResult) { assembler.Add(TypeBuilder.Field(propertyName, schemaFromResult)); // assembler.Name(propertyName).Type(result).NoDefault(); return; } } if (propertyType == null) { assembler.Add(TypeBuilder.Field(propertyName, TypeBuilder.NullType())); // assembler.Name(propertyName).Type("null"); } else if (propertyType is string) { string propertyTypeName = propertyType.ToString(); bool isArray = EventTypeUtility.IsPropertyArray(propertyTypeName); if (isArray) { propertyTypeName = EventTypeUtility.GetPropertyRemoveArray(propertyTypeName); } // Add EventType itself as a property EventType eventType = eventTypeNameResolver.GetTypeByName(propertyTypeName); if (!(eventType is AvroEventType)) { throw new EPException( "Type definition encountered an unexpected property type name '" + propertyType + "' for property '" + propertyName + "', expected the name of a previously-declared Avro type"); } schema = ((AvroEventType) eventType).SchemaAvro; if (!isArray) { assembler.Add(TypeBuilder.Field(propertyName, schema)); } else { assembler.Add(TypeBuilder.Field(propertyName, TypeBuilder.Array(schema))); } } else if (propertyType is EventType) { var eventType = (EventType) propertyType; CheckCompatibleType(eventType); if (eventType is AvroEventType) { schema = ((AvroEventType) eventType).SchemaAvro; assembler.Add(TypeBuilder.Field(propertyName, schema)); } else if (eventType is MapEventType) { var mapEventType = (MapEventType) eventType; var nestedSchema = AssembleNestedSchema( mapEventType, avroSettings, annotations, eventTypeNameResolver, statementName, optionalMapper); assembler.Add(TypeBuilder.Field(propertyName, nestedSchema)); } else { throw new IllegalStateException("Unrecognized event type " + eventType); } } else if (propertyType is EventType[]) { EventType eventType = ((EventType[]) propertyType)[0]; CheckCompatibleType(eventType); if (eventType is AvroEventType) { schema = ((AvroEventType) eventType).SchemaAvro; assembler.Add(TypeBuilder.Field(propertyName, TypeBuilder.Array(schema))); } else if (eventType is MapEventType) { var mapEventType = (MapEventType) eventType; var nestedSchema = AssembleNestedSchema( mapEventType, avroSettings, annotations, eventTypeNameResolver, statementName, optionalMapper); assembler.Add(TypeBuilder.Field(propertyName, TypeBuilder.Array(nestedSchema))); } else { throw new IllegalStateException("Unrecognized event type " + eventType); } } else if (propertyType is Type) { var propertyClass = (Type) propertyType; var propertyClassBoxed = propertyClass.GetBoxedType(); bool nullable = propertyClass == propertyClassBoxed; bool preferNonNull = avroSettings.IsEnableSchemaDefaultNonNull; if (propertyClassBoxed == typeof(bool?)) { AssemblePrimitive(nullable, REQ_BOOLEAN, OPT_BOOLEAN, assembler, propertyName, preferNonNull); } else if (propertyClassBoxed == typeof(int?) || propertyClassBoxed == typeof(byte?)) { AssemblePrimitive(nullable, REQ_INT, OPT_INT, assembler, propertyName, preferNonNull); } else if (propertyClassBoxed == typeof(long?)) { AssemblePrimitive(nullable, REQ_LONG, OPT_LONG, assembler, propertyName, preferNonNull); } else if (propertyClassBoxed == typeof(float?)) { AssemblePrimitive(nullable, REQ_FLOAT, OPT_FLOAT, assembler, propertyName, preferNonNull); } else if (propertyClassBoxed == typeof(double?)) { AssemblePrimitive(nullable, REQ_DOUBLE, OPT_DOUBLE, assembler, propertyName, preferNonNull); } else if (propertyClass == typeof(string)) { if (avroSettings.IsEnableNativeString) { if (preferNonNull) { assembler.Add( TypeBuilder.Field( propertyName, TypeBuilder.Primitive( "string", TypeBuilder.Property( AvroConstant.PROP_STRING_KEY, AvroConstant.PROP_STRING_VALUE)))); } else { assembler.Add( TypeBuilder.Field( propertyName, TypeBuilder.Union( TypeBuilder.NullType(), TypeBuilder.StringType( TypeBuilder.Property( AvroConstant.PROP_STRING_KEY, AvroConstant.PROP_STRING_VALUE))))); } } else { AssemblePrimitive(nullable, REQ_STRING, OPT_STRING, assembler, propertyName, preferNonNull); } } else if (propertyClass == typeof(byte[])) { if (preferNonNull) { assembler.Add(TypeBuilder.RequiredBytes(propertyName)); } else { assembler.Add( TypeBuilder.Field( propertyName, TypeBuilder.Union( TypeBuilder.NullType(), TypeBuilder.BytesType()))); } } else if (propertyClass.IsArray) { var componentType = propertyClass.GetElementType(); var componentTypeBoxed = componentType.GetBoxedType(); var nullableElements = componentType == componentTypeBoxed; if (componentTypeBoxed == typeof(bool?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_BOOLEAN, ARRAY_OF_OPT_BOOLEAN, assembler, propertyName, preferNonNull); } else if (componentTypeBoxed == typeof(int?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_INT, ARRAY_OF_OPT_INT, assembler, propertyName, preferNonNull); } else if (componentTypeBoxed == typeof(long?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_LONG, ARRAY_OF_OPT_LONG, assembler, propertyName, preferNonNull); } else if (componentTypeBoxed == typeof(float?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_FLOAT, ARRAY_OF_OPT_FLOAT, assembler, propertyName, preferNonNull); } else if (componentTypeBoxed == typeof(double?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_DOUBLE, ARRAY_OF_OPT_DOUBLE, assembler, propertyName, preferNonNull); } else if (componentTypeBoxed == typeof(byte?)) { AssembleArray( nullableElements, ARRAY_OF_REQ_INT, ARRAY_OF_OPT_INT, assembler, propertyName, preferNonNull); } else if (propertyClass == typeof(string[])) { JObject array; if (avroSettings.IsEnableNativeString) { array = TypeBuilder.Array( TypeBuilder.StringType( TypeBuilder.Property( AvroConstant.PROP_STRING_KEY, AvroConstant.PROP_STRING_VALUE))); } else { array = TypeBuilder.Array(TypeBuilder.StringType()); } if (preferNonNull) { assembler.Add(TypeBuilder.Field(propertyName, array)); } else { assembler.Add( TypeBuilder.Field( propertyName, TypeBuilder.Union( TypeBuilder.NullType(), array))); } } else if (propertyClass.CanUnwrap<object>()) { } else { throw MakeEPException(propertyName, propertyType); } } else if (propertyClass.IsGenericDictionary()) { JToken value; if (avroSettings.IsEnableNativeString) { value = TypeBuilder.StringType( TypeBuilder.Property(AvroConstant.PROP_STRING_KEY, AvroConstant.PROP_STRING_VALUE)); } else { value = TypeBuilder.StringType(); } if (preferNonNull) { assembler.Add(TypeBuilder.Field(propertyName, TypeBuilder.Map(value))); } else { assembler.Add( TypeBuilder.Field( propertyName, TypeBuilder.Union( TypeBuilder.NullType(), TypeBuilder.Map(value)))); } } else if (propertyClass.IsGenericCollection()) { AssembleFieldForCollection( propertyName, propertyType, assembler, avroSettings, propertyClass, preferNonNull); } else { throw MakeEPException(propertyName, propertyType); } } else { throw MakeEPException(propertyName, propertyType); } }