public void TestGetTypeEquals(string name, Schema.Type schemaType, Type expected) { var objectCreator = new ObjectCreator(); var actual = objectCreator.GetType(name, Schema.Type.Record); Assert.AreEqual(expected, actual); }
/// <summary> /// A generic method to serialize primitive Avro types. /// </summary> /// <typeparam name="S">Type of the C# type to be serialized</typeparam> /// <param name="value">The value to be serialized</param> /// <param name="tag">The schema type tag</param> /// <param name="writer">The writer which should be used to write the given type.</param> protected void Write <S>(object value, Schema.Type tag, Writer <S> writer) { if (!(value is S)) { throw TypeMismatch(value, tag.ToString(), typeof(S).ToString()); } writer((S)value); }
/// <summary> /// A generic method to serialize primitive Avro types. /// </summary> /// <typeparam name="TValue">Type of the C# type to be serialized</typeparam> /// <param name="value">The value to be serialized</param> /// <param name="tag">The schema type tag</param> /// <param name="writer">The writer which should be used to write the given type.</param> protected void Write <TValue>(object value, Schema.Type tag, Writer <TValue> writer) { if (!(value is TValue)) { throw TypeMismatch(value, tag.ToString(), typeof(TValue).ToString()); } writer((TValue)value); }
public void TestPrimitive(string s, Schema.Type type) { Schema sc = Schema.Parse(s); Assert.IsTrue(sc is PrimitiveSchema); Assert.AreEqual(type, sc.Tag); testEquality(s, sc); testToString(sc); }
public void TestGetTypeAssignable(string name, Schema.Type schemaType, Type expected) { var objectCreator = new ObjectCreator(); var actual = objectCreator.GetType(name, schemaType); Assert.True( expected.IsAssignableFrom(actual), " Expected: assignable from {0}\n But was: {1}", expected, actual); }
private bool FieldAllowsNull(Field field) { Schema.Type fieldType = field.Schema.Tag; if (fieldType != Schema.Type.Union) { return(false); } IList <Schema> schemas = ((UnionSchema)field.Schema).Schemas; return(schemas.Any(schema => schema.Tag == Schema.Type.Null)); }
/// <summary> /// Creates new instance of the given type /// </summary> /// <param name="name">fully qualified name of the type</param> /// <param name="schemaType">type of schema</param> /// <returns>new object of the given type</returns> public object New(string name, Schema.Type schemaType) { NameCtorKey key = new NameCtorKey(name, schemaType); CtorDelegate ctor; if (!ctors.TryGetValue(key, out ctor)) { Type type = GetType(name, schemaType); ctor = GetConstructor(name, schemaType, type); ctors.Add(key, ctor); } return(ctor()); }
/// <summary> /// Gets the type of the specified type name /// </summary> /// <param name="name">name of the object to get type of</param> /// <param name="schemaType">schema type for the object</param> /// <returns>Type</returns> /// <exception cref="AvroException"> /// No type found matching the given name. /// </exception> public Type GetType(string name, Schema.Type schemaType) { Type type = FindType(name); if (schemaType == Schema.Type.Map) { type = GenericMapType.MakeGenericType(typeof(string), type); } else if (schemaType == Schema.Type.Array) { type = GenericListType.MakeGenericType(type); } return(type); }
/// <summary> /// Gets the type of the specified type name /// </summary> /// <param name="name">name of the object to get type of</param> /// <param name="schemaType">schema type for the object</param> /// <returns>Type</returns> public Type GetType(string name, Schema.Type schemaType) { Type type; if (diffAssembly) { // entry assembly different from current assembly, try entry assembly first type = entryAssembly.GetType(name); if (type == null) // now try current assembly and mscorlib { type = Type.GetType(name); } } else { type = Type.GetType(name); } if (type == null) // type is still not found, need to loop through all loaded assemblies { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { type = assembly.GetType(name); if (type != null) { break; } } } if (type == null) { throw new AvroException("Unable to find type " + name + " in all loaded assemblies"); } if (schemaType == Schema.Type.Map) { margs[1] = type; type = GenericMapType.MakeGenericType(margs); } else if (schemaType == Schema.Type.Array) { largs[0] = type; type = GenericListType.MakeGenericType(largs); } return(type); }
/// <inheritdoc/> protected override bool IsReusable(Schema.Type tag) { switch (tag) { case Schema.Type.Double: case Schema.Type.Boolean: case Schema.Type.Int: case Schema.Type.Long: case Schema.Type.Float: case Schema.Type.Bytes: case Schema.Type.String: case Schema.Type.Null: return(false); } return(true); }
/// <summary> /// Gets the type of the specified type name /// </summary> /// <param name="name">name of the object to get type of</param> /// <param name="schemaType">schema type for the object</param> /// <returns>Type</returns> public Type GetType(string name, Schema.Type schemaType) { Type type = FindType(name, true); if (schemaType == Schema.Type.Map) { margs[1] = type; type = GenericMapType.MakeGenericType(margs); } else if (schemaType == Schema.Type.Array) { largs[0] = type; type = GenericListType.MakeGenericType(largs); } return(type); }
/// <summary> /// Gets the default constructor for the specified type /// </summary> /// <param name="name">name of object for the type</param> /// <param name="schemaType">schema type for the object</param> /// <param name="type">type of the object</param> /// <returns>Default constructor for the type</returns> public CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type) { ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes); if (ctorInfo == null) { throw new AvroException("Class " + name + " has no default constructor"); } DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object), null, type, true); ILGenerator ilGen = dynMethod.GetILGenerator(); ilGen.Emit(OpCodes.Nop); ilGen.Emit(OpCodes.Newobj, ctorInfo); ilGen.Emit(OpCodes.Ret); return((CtorDelegate)dynMethod.CreateDelegate(ctorType)); }
private void AssertTypeArray( Type componentType, Schema.Type expectedElementType, bool unionOfNull, bool unionOfNullElements, ConfigurationCommonEventTypeMeta.AvroSettingsConfig avroSettings) { var schema = Assemble( TypeHelper.GetArrayType(componentType), null, avroSettings, EVENT_ADAPTER_SERVICE); Schema elementSchema; if (!unionOfNull) { var arraySchema = schema.AsArraySchema(); Assert.AreEqual(Schema.Type.Array, schema.Tag); elementSchema = arraySchema.ItemSchema; } else { var unionSchema = schema.AsUnionSchema(); Assert.AreEqual(2, unionSchema.Count); Assert.AreEqual(Schema.Type.Null, unionSchema.Schemas[0].Tag); Assert.AreEqual(Schema.Type.Array, unionSchema.Schemas[1].Tag); elementSchema = unionSchema.Schemas[1].AsArraySchema().ItemSchema; } // assert element type if (!unionOfNullElements) { Assert.AreEqual(expectedElementType, elementSchema.Tag); AssertStringNative(elementSchema, avroSettings); } else { var unionSchema = elementSchema.AsUnionSchema(); Assert.AreEqual(2, unionSchema.Count); Assert.AreEqual(Schema.Type.Null, unionSchema.Schemas[0].Tag); Assert.AreEqual(expectedElementType, unionSchema.Schemas[1].Tag); } }
private object CreateInstance(Schema schema, Schema.Type schemaType) { var recordSchema = schema as RecordSchema; var fixedSchema = schema as FixedSchema; object instance = null; if (!string.IsNullOrWhiteSpace(recordSchema?.Fullname)) { var typeName = GetTypeName(recordSchema.Fullname); instance = ObjectCreator.Instance.New(typeName, schemaType); } else if (!string.IsNullOrWhiteSpace(fixedSchema?.Fullname)) { var typeName = GetTypeName(fixedSchema.Fullname); instance = ObjectCreator.Instance.New(typeName, schemaType); } return(instance); }
/// <summary> /// Gets the default constructor for the specified type /// </summary> /// <param name="name">name of object for the type</param> /// <param name="schemaType">schema type for the object</param> /// <param name="type">type of the object</param> /// <returns>Default constructor for the type</returns> public CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type) { //ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes); //if (ctorInfo == null) // throw new AvroException("Class " + name + " has no default constructor"); //DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object), null, type, true); //ILGenerator ilGen = dynMethod.GetILGenerator(); //ilGen.Emit(OpCodes.Nop); //ilGen.Emit(OpCodes.Newobj, ctorInfo); //ilGen.Emit(OpCodes.Ret); //return (CtorDelegate)dynMethod.CreateDelegate(ctorType); // https://ayende.com/blog/3167/creating-objects-perf-implications // http://bit.ly/2cbySMk //return () => Activator.CreateInstance(type); return(GetCreator(type)); }
private Schema.Type GetFieldType(Field field) { Schema.Type fieldType = field.Schema.Tag; if (fieldType == Schema.Type.Union) { IList <Schema> types = ((UnionSchema)field.Schema).Schemas; foreach (Schema schema in types) { if (schema.Tag != Schema.Type.Null) { return(schema.Tag); } } return(fieldType); } return(fieldType); }
private void AssertType( Type clazz, Schema.Type expected, bool unionOfNull, ConfigurationCommonEventTypeMeta.AvroSettingsConfig avroSettings) { var schema = Assemble(clazz, null, avroSettings, EVENT_ADAPTER_SERVICE); if (!unionOfNull) { Assert.AreEqual(expected, schema.Tag); AssertStringNative(schema, avroSettings); } else { UnionSchema unionSchema = schema.AsUnionSchema(); Assert.AreEqual(Schema.Type.Union, schema.Tag); Assert.AreEqual(2, unionSchema.Schemas.Count); Assert.AreEqual(Schema.Type.Null, unionSchema.Schemas[0].Tag); Assert.AreEqual(expected, unionSchema.Schemas[1].Tag); } }
public void TestPrimitiveWithMetadata(string rawSchema, Schema.Type type) { Schema definedSchema = Schema.Parse(rawSchema); Assert.IsTrue(definedSchema is PrimitiveSchema); Assert.AreEqual(type.ToString().ToLower(), definedSchema.Name); Assert.AreEqual(type, definedSchema.Tag); testEquality(rawSchema, definedSchema); testToString(definedSchema); Assert.True(definedSchema.ToString().Contains("metafield")); var rawRecordSchema = "{\"type\":\"record\",\"name\":\"Foo\"," + "\"fields\":[{\"name\":\"f1\",\"type\":" + rawSchema + "}]}"; Schema baseRecordSchema = Schema.Parse(rawRecordSchema); Assert.AreEqual(Schema.Type.Record, baseRecordSchema.Tag); RecordSchema recordSchema = baseRecordSchema as RecordSchema; Assert.IsNotNull(recordSchema); Assert.AreEqual(1, recordSchema.Count); Assert.IsTrue(recordSchema["f1"].Schema is PrimitiveSchema); Assert.AreEqual(type.ToString().ToLower(), recordSchema["f1"].Schema.Name); Assert.AreEqual(type, recordSchema["f1"].Schema.Tag); testEquality(rawRecordSchema, baseRecordSchema); testToString(recordSchema["f1"].Schema); Assert.True(baseRecordSchema.ToString().Contains("metafield")); Assert.True(recordSchema["f1"].Schema.ToString().Contains("metafield")); Assert.True(definedSchema.Equals(recordSchema["f1"].Schema)); Assert.AreEqual(definedSchema.GetHashCode(), recordSchema["f1"].Schema.GetHashCode()); }
/// <summary> /// A generic function to read primitive types /// </summary> /// <typeparam name="S">The .NET type to read</typeparam> /// <param name="tag">The Avro type tag for the object on the stream</param> /// <param name="readerSchema">A schema compatible to the Avro type</param> /// <param name="reader">A function that can read the avro type from the stream</param> /// <returns>The primitive type just read</returns> protected S Read <S>(Schema.Type tag, Schema readerSchema, Reader <S> reader) { return(reader()); }
private static ObjectCreator.CtorDelegate GetConstructor(string name, Schema.Type schemaType) { var creator = ObjectCreator.Instance; return(creator.GetConstructor(name, schemaType, creator.GetType(name, schemaType))); }
private object GetObject(Field field, string value) { Schema.Type fieldType = GetFieldType(field); if (!string.IsNullOrWhiteSpace(value)) { switch (fieldType) { case Schema.Type.Int: try { return(int.Parse(value)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new FormatException( GetParseExceptionMessage(value, field.Name, typeof(int)), ex); } case Schema.Type.Long: try { return(long.Parse(value)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new FormatException(GetParseExceptionMessage(value, field.Name, typeof(long)), ex); } case Schema.Type.Float: try { return(float.Parse(value)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new FormatException(GetParseExceptionMessage(value, field.Name, typeof(float)), ex); } case Schema.Type.Double: try { return(double.Parse(value)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new FormatException(GetParseExceptionMessage(value, field.Name, typeof(double)), ex); } case Schema.Type.Boolean: try { return(bool.Parse(value)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new FormatException(GetParseExceptionMessage(value, field.Name, typeof(bool)), ex); } case Schema.Type.String: return(value); default: throw new NotSupportedException($"Type {fieldType} is not supported for field '{field.Name}'."); } } if (FieldAllowsNull(field)) { return(null); } throw new InvalidOperationException($"Value of 'null' is not allowed for field '{field.Name}'."); }
public NameCtorKey(string value1, Schema.Type value2) : this() { name = value1; type = value2; }
/// <summary> /// Creates new instance of the given type /// </summary> /// <param name="name">fully qualified name of the type</param> /// <param name="schemaType">type of schema</param> /// <returns>new object of the given type</returns> /// <exception cref="AvroException"> /// No type found matching the given name. /// </exception> public object New(string name, Schema.Type schemaType) { return(Activator.CreateInstance(GetType(name, schemaType))); }
/// <summary> /// Gets the type of the specified type name /// </summary> /// <param name="name">name of the object to get type of</param> /// <param name="schemaType">schema type for the object</param> /// <returns>Type</returns> public Type GetType(string name, Schema.Type schemaType) { Type type; // Modify provided type to ensure it can be discovered. // This is mainly for Generics, and Nullables. name = name.Replace("Nullable", "Nullable`1"); name = name.Replace("IList", "System.Collections.Generic.IList`1"); name = name.Replace("<", "["); name = name.Replace(">", "]"); if (diffAssembly) { // entry assembly different from current assembly, try entry assembly first type = entryAssembly.GetType(name); if (type == null) // now try current assembly and mscorlib { type = Type.GetType(name); } } else { type = Type.GetType(name); } Type[] types; if (type == null) // type is still not found, need to loop through all loaded assemblies { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { types = assembly.GetTypes(); // Change the search to look for Types by both NAME and FULLNAME foreach (Type t in types) { if (name == t.Name || name == t.FullName) { type = t; } } if (type != null) { break; } } } if (type == null) { throw new AvroException("Unable to find type " + name + " in all loaded assemblies"); } if (schemaType == Schema.Type.Map) { margs[1] = type; type = GenericMapType.MakeGenericType(margs); } else if (schemaType == Schema.Type.Array) { largs[0] = type; type = GenericListType.MakeGenericType(largs); } return(type); }
private static StringBuilder Build(IDictionary <string, string> env, Schema s, StringBuilder o) { bool firstTime = true; Schema.Type st = s.Tag; switch (st) { case Schema.Type.Union: UnionSchema us = s as UnionSchema; o.Append('['); foreach (Schema b in us.Schemas) { if (!firstTime) { o.Append(','); } else { firstTime = false; } Build(env, b, o); } return(o.Append(']')); case Schema.Type.Array: case Schema.Type.Map: o.Append("{\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"'); if (st == Schema.Type.Array) { ArraySchema arraySchema = s as ArraySchema; Build(env, arraySchema.ItemSchema, o.Append(",\"items\":")); } else { MapSchema mapSchema = s as MapSchema; Build(env, mapSchema.ValueSchema, o.Append(",\"values\":")); } return(o.Append('}')); case Schema.Type.Enumeration: case Schema.Type.Fixed: case Schema.Type.Record: NamedSchema namedSchema = s as NamedSchema; var name = namedSchema.Fullname; if (env.ContainsKey(name)) { return(o.Append(env[name])); } var qname = "\"" + name + "\""; env.Add(name, qname); o.Append("{\"name\":").Append(qname); o.Append(",\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"'); if (st == Schema.Type.Enumeration) { EnumSchema enumSchema = s as EnumSchema; o.Append(",\"symbols\":["); foreach (var enumSymbol in enumSchema.Symbols) { if (!firstTime) { o.Append(','); } else { firstTime = false; } o.Append('\"').Append(enumSymbol).Append('\"'); } o.Append(']'); } else if (st == Schema.Type.Fixed) { FixedSchema fixedSchema = s as FixedSchema; o.Append(",\"size\":") .Append(fixedSchema.Size.ToString(CultureInfo.InvariantCulture)); } else // st == Schema.Type.Record { RecordSchema recordSchema = s as RecordSchema; o.Append(",\"fields\":["); foreach (var field in recordSchema.Fields) { if (!firstTime) { o.Append(','); } else { firstTime = false; } o.Append("{\"name\":\"").Append(field.Name).Append('\"'); Build(env, field.Schema, o.Append(",\"type\":")).Append('}'); } o.Append(']'); } return(o.Append('}')); default: //boolean, bytes, double, float, int, long, null, string return(o.Append('\"').Append(s.Name).Append('\"')); } }
private static Type GetSchemaType(CapnProto.Pointer parent, System.Type type, out int len) { if (type == null) { len = 0; return(default(Type)); } // objects and enums var idAttrib = (IdAttribute)Attribute.GetCustomAttribute(type, typeof(IdAttribute)); if (idAttrib != null) { if (type.IsEnum) { len = 16; Schema.Type tmp = Schema.Type.Create(parent, Type.Unions.@enum); var @enum = tmp.@enum; @enum.typeId = idAttrib.Id; return(tmp); } if (type.IsClass || type.IsValueType) { len = Type.LEN_POINTER; Schema.Type tmp = Schema.Type.Create(parent, Type.Unions.@struct); var @struct = tmp.@struct; @struct.typeId = idAttrib.Id; } } if (type == typeof(byte[])) { len = 0; return(Schema.Type.Create(parent, Type.Unions.data)); } else if (type == typeof(void)) { len = 0; return(Schema.Type.Create(parent, Type.Unions.@void)); } else if (type == typeof(object) || type == typeof(System.Collections.IList)) { len = Type.LEN_POINTER; return(Schema.Type.Create(parent, Type.Unions.anyPointer)); } switch (System.Type.GetTypeCode(type)) { case TypeCode.Empty: len = 0; return(default(Type)); case TypeCode.Boolean: len = 1; return(Schema.Type.Create(parent, Type.Unions.@bool)); case TypeCode.SByte: len = 8; return(Schema.Type.Create(parent, Type.Unions.int8)); case TypeCode.Byte: len = 8; return(Schema.Type.Create(parent, Type.Unions.uint8)); case TypeCode.Int16: len = 16; return(Schema.Type.Create(parent, Type.Unions.int16)); case TypeCode.UInt16: len = 16; return(Schema.Type.Create(parent, Type.Unions.uint16)); case TypeCode.Int32: len = 32; return(Schema.Type.Create(parent, Type.Unions.int32)); case TypeCode.UInt32: len = 32; return(Schema.Type.Create(parent, Type.Unions.uint32)); case TypeCode.Int64: len = 64; return(Schema.Type.Create(parent, Type.Unions.int64)); case TypeCode.UInt64: len = 64; return(Schema.Type.Create(parent, Type.Unions.uint64)); case TypeCode.Char: len = 16; return(Schema.Type.Create(parent, Type.Unions.uint16)); case TypeCode.DBNull: len = 0; return(Schema.Type.Create(parent, Type.Unions.@void)); case TypeCode.Single: len = 32; return(Schema.Type.Create(parent, Type.Unions.float32)); case TypeCode.Double: len = 64; return(Schema.Type.Create(parent, Type.Unions.float64)); case TypeCode.String: len = Type.LEN_POINTER; return(Schema.Type.Create(parent, Type.Unions.text)); } // lists (note this includes recursive) var elType = GetSchemaType(parent, GetElementType(type), out len); if (elType.IsValid()) { len = Type.LEN_POINTER; Schema.Type tmp = Schema.Type.Create(parent, Type.Unions.list); var list = tmp.list; list.elementType = elType; return(tmp); } len = 0; return(default(Type)); }
/// <summary> /// A generic function to read primitive types /// </summary> /// <typeparam name="T">The .NET type to read</typeparam> /// <param name="tag">The Avro type tag for the object on the stream</param> /// <param name="readerSchema">A schema compatible to the Avro type</param> /// <param name="reader">A function that can read the avro type from the stream</param> /// <returns>The primitive type just read</returns> protected T Read <T>(Schema.Type tag, Schema readerSchema, Reader <T> reader) { return(reader()); }
/// <summary> /// Indicates if it's possible to reuse an object of the specified type. Generally /// false for immutable objects like int, long, string, etc but may differ between /// the Specific and Generic implementations. Used to avoid retrieving the existing /// value if it's not reusable. /// </summary> protected virtual bool IsReusable(Schema.Type tag) { return(true); }