/// <summary> /// Initializes a new instance of the <see cref="EnumSchema" /> class. /// </summary> /// <param name="namedEntityAttributes">The named schema attributes.</param> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="attributes">The attributes.</param> internal EnumSchema( NamedEntityAttributes namedEntityAttributes, Type runtimeType, Dictionary <string, string> attributes) : base(namedEntityAttributes, runtimeType, attributes) { if (runtimeType == null) { throw new ArgumentNullException("runtimeType"); } this.symbols = new List <string>(); this.symbolToValue = new Dictionary <string, int>(); this.valueToSymbol = new Dictionary <int, string>(); this.avroToCSharpValueMapping = new List <long>(); if (runtimeType.IsEnum()) { this.symbols = Enum.GetNames(runtimeType).ToList(); Array values = Enum.GetValues(runtimeType); for (int i = 0; i < this.symbols.Count; i++) { int v = Convert.ToInt32(values.GetValue(i), CultureInfo.InvariantCulture); this.avroToCSharpValueMapping.Add(Convert.ToInt64(values.GetValue(i), CultureInfo.InvariantCulture)); this.symbolToValue.Add(this.symbols[i], v); this.valueToSymbol.Add(v, this.symbols[i]); } } }
/// <summary> /// Parses the record type. /// </summary> /// <param name="record">The record.</param> /// <param name="parent">The parent schema.</param> /// <param name="namedSchemas">The named schemas.</param> /// <returns> /// Schema internal representation. /// </returns> /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="record"/> can not be parsed properly.</exception> private TypeSchema ParseRecordType(JObject record, NamedSchema parent, Dictionary <string, NamedSchema> namedSchemas) { var name = record.RequiredProperty <string>(Token.Name); var nspace = this.GetNamespace(record, parent, name); var recordName = new SchemaName(name, nspace); var doc = record.OptionalProperty <string>(Token.Doc); var aliases = this.GetAliases(record, recordName.Namespace); var attributes = new NamedEntityAttributes(recordName, aliases, doc); Dictionary <string, string> customAttributes = record.GetAttributesNotIn(StandardProperties.Record); var result = new RecordSchema(attributes, typeof(AvroRecord), customAttributes); namedSchemas.Add(result.FullName, result); List <RecordField> fields = record.OptionalArrayProperty( Token.Fields, (field, index) => { if (field.Type != JTokenType.Object) { throw new SerializationException( string.Format(CultureInfo.InvariantCulture, "Property 'fields' has invalid value '{0}'.", field)); } return(this.ParseRecordField(field as JObject, result, namedSchemas, index)); }); fields.ForEach(result.AddField); return(result); }
/// <summary> /// Parses a JSON object representing an Avro enumeration to a <see cref="EnumSchema"/>. /// </summary> /// <param name="enumeration">The JSON token that represents the enumeration.</param> /// <param name="parent">The parent schema.</param> /// <param name="namedSchemas">The named schemas.</param> /// <returns> /// Instance of <see cref="TypeSchema" /> containing IR of the enumeration. /// </returns> /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="enumeration"/> contains invalid symbols.</exception> private TypeSchema ParseEnumType(JObject enumeration, NamedSchema parent, Dictionary <string, NamedSchema> namedSchemas) { var name = enumeration.RequiredProperty <string>(Token.Name); var nspace = this.GetNamespace(enumeration, parent, name); var enumName = new SchemaName(name, nspace); var doc = enumeration.OptionalProperty <string>(Token.Doc); var aliases = this.GetAliases(enumeration, enumName.Namespace); var attributes = new NamedEntityAttributes(enumName, aliases, doc); List <string> symbols = enumeration.OptionalArrayProperty( Token.Symbols, (symbol, index) => { if (symbol.Type != JTokenType.String) { throw new SerializationException( string.Format(CultureInfo.InvariantCulture, "Expected an enum symbol of type string however the type of the symbol is '{0}'.", symbol.Type)); } return((string)symbol); }); Dictionary <string, string> customAttributes = enumeration.GetAttributesNotIn(StandardProperties.Enumeration); var result = new EnumSchema(attributes, typeof(AvroEnum), customAttributes); namedSchemas.Add(result.FullName, result); symbols.ForEach(result.AddSymbol); return(result); }
internal RecordSchema( NamedEntityAttributes namedAttributes, Type runtimeType, Dictionary <string, string> attributes) : base(namedAttributes, runtimeType, attributes) { this.fields = new List <RecordFieldSchema>(); this.fieldsByName = new Dictionary <string, RecordFieldSchema>(StringComparer.InvariantCultureIgnoreCase); }
/// <summary> /// Initializes a new instance of the <see cref="RecordSchema" /> class. /// </summary> /// <param name="namedAttributes">The named attributes.</param> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="attributes">The attributes.</param> internal RecordSchema( NamedEntityAttributes namedAttributes, Type runtimeType, Dictionary <string, string> attributes) : base(namedAttributes, runtimeType, attributes) { this.fields = new List <RecordField>(); this.fiedsByName = new Dictionary <string, RecordField>(); }
/// <summary> /// Initializes a new instance of the <see cref="RecordField" /> class. /// </summary> /// <param name="namedEntityAttributes">The named entity attributes.</param> /// <param name="typeSchema">The type schema.</param> /// <param name="order">The order.</param> /// <param name="hasDefaultValue">Whether the field has a default value or not.</param> /// <param name="defaultValue">The default value.</param> /// <param name="info">The info.</param> /// <param name="position">The position of the field in the schema.</param> internal RecordField( NamedEntityAttributes namedEntityAttributes, TypeSchema typeSchema, SortOrder order, bool hasDefaultValue, object defaultValue, MemberInfo info, int position) : this(namedEntityAttributes, typeSchema, order, hasDefaultValue, defaultValue, info, position, new Dictionary <string, string>()) { }
/// <summary> /// Initializes a new instance of the <see cref="NamedSchema" /> class. /// </summary> /// <param name="nameAttributes">The name attributes.</param> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="attributes">The attributes.</param> internal NamedSchema( NamedEntityAttributes nameAttributes, Type runtimeType, Dictionary <string, string> attributes) : base(runtimeType, attributes) { if (nameAttributes == null) { throw new ArgumentNullException("nameAttributes"); } this.attributes = nameAttributes; }
/// <summary> /// Initializes a new instance of the <see cref="FixedSchema" /> class. /// </summary> /// <param name="namedEntityAttributes">The named schema attributes.</param> /// <param name="size">The size.</param> /// <param name="runtimeType">Type of the runtime.</param> /// <param name="attributes">The attributes.</param> internal FixedSchema( NamedEntityAttributes namedEntityAttributes, int size, Type runtimeType, Dictionary <string, string> attributes) : base(namedEntityAttributes, runtimeType, attributes) { if (size <= 0) { throw new ArgumentOutOfRangeException("size"); } this.size = size; }
private TypeSchema BuildCore(FixedSchema w, FixedSchema r) { bool match = this.DoNamesMatch(w, r) && w.Size == r.Size; if (!match) { return(null); } if (this.visited.ContainsKey(w)) { return(this.visited[w]); } var attr = new NamedEntityAttributes(new SchemaName(w.Name, w.Namespace), w.Aliases, w.Doc); var schema = new FixedSchema(attr, w.Size, r.RuntimeType); this.visited.Add(w, schema); return(schema); }
/// <summary> /// Parses the record field. /// </summary> /// <param name="field">The field.</param> /// <param name="parent">The parent schema.</param> /// <param name="namedSchemas">The named schemas.</param> /// <param name="position">The position.</param> /// <returns> /// Schema internal representation. /// </returns> /// <exception cref="System.Runtime.Serialization.SerializationException">Thrown when <paramref name="field"/> is not valid or when sort order is not valid.</exception> private RecordField ParseRecordField(JObject field, NamedSchema parent, Dictionary <string, NamedSchema> namedSchemas, int position) { var name = field.RequiredProperty <string>(Token.Name); var doc = field.OptionalProperty <string>(Token.Doc); var order = field.OptionalProperty <string>(Token.Order); var aliases = this.GetAliases(field, parent.FullName); var fieldType = field[Token.Type]; if (fieldType == null) { throw new SerializationException( string.Format(CultureInfo.InvariantCulture, "Record field schema '{0}' has no type.", field)); } TypeSchema type = this.Parse(fieldType, parent, namedSchemas); object defaultValue = null; bool hasDefaultValue = field[Token.Default] != null; if (hasDefaultValue) { var objectParser = new JsonObjectParser(); defaultValue = objectParser.Parse(type, field[Token.Default].ToString()); } var orderValue = SortOrder.Ascending; if (!string.IsNullOrEmpty(order)) { if (!SortValue.ContainsKey(order.ToUpperInvariant())) { throw new SerializationException( string.Format(CultureInfo.InvariantCulture, "Invalid sort order of the field '{0}'.", order)); } orderValue = SortValue[order.ToUpperInvariant()]; } var fieldName = new SchemaName(name); var attributes = new NamedEntityAttributes(fieldName, aliases, doc); return(new RecordField(attributes, type, orderValue, hasDefaultValue, defaultValue, null, position)); }
private TypeSchema BuildCore(EnumSchema w, EnumSchema r) { bool match = this.DoNamesMatch(w, r) && w.Symbols.Select((s, i) => i < r.Symbols.Count && r.Symbols[i] == s).All(m => m); if (!match) { return(null); } if (this.visited.ContainsKey(w)) { return(this.visited[w]); } var attr = new NamedEntityAttributes(new SchemaName(w.Name, w.Namespace), w.Aliases, w.Doc); var schema = new EnumSchema(attr, r.RuntimeType); r.Symbols.Where(s => !schema.Symbols.Contains(s)).ToList().ForEach(schema.AddSymbol); this.visited.Add(w, schema); return(schema); }
/// <summary> /// Initializes a new instance of the <see cref="RecordField" /> class. /// </summary> /// <param name="namedEntityAttributes">The named entity attributes.</param> /// <param name="typeSchema">The type schema.</param> /// <param name="order">The order.</param> /// <param name="hasDefaultValue">Whether the field has a default value or not.</param> /// <param name="defaultValue">The default value.</param> /// <param name="info">The info.</param> /// <param name="position">The position of the field in the schema.</param> /// <param name="attributes">The attributes.</param> internal RecordField( NamedEntityAttributes namedEntityAttributes, TypeSchema typeSchema, SortOrder order, bool hasDefaultValue, object defaultValue, MemberInfo info, int position, Dictionary <string, string> attributes) : base(attributes) { this.namedEntityAttributes = namedEntityAttributes; this.typeSchema = typeSchema; this.order = order; this.hasDefaultValue = hasDefaultValue; this.defaultValue = defaultValue; this.info = info; this.position = position; this.ShouldBeSkipped = false; this.UseDefaultValue = false; }
private FixedSchema ParseFixedType(JObject type, NamedSchema parent) { var name = type.RequiredProperty <string>(Token.Name); var nspace = this.GetNamespace(type, parent, name); var fixedName = new SchemaName(name, nspace); var size = type.RequiredProperty <int>(Token.Size); if (size <= 0) { throw new SerializationException( string.Format(CultureInfo.InvariantCulture, "Only positive size of fixed values allowed: '{0}'.", size)); } var aliases = this.GetAliases(type, fixedName.Namespace); var attributes = new NamedEntityAttributes(fixedName, aliases, string.Empty); var customAttributes = type.GetAttributesNotIn(StandardProperties.Record); var result = new FixedSchema(attributes, size, typeof(byte[]), customAttributes); return(result); }
/// <summary> /// Generates the record type schema. /// </summary> /// <param name="type">The type.</param> /// <param name="schemas">The schemas.</param> /// <param name="currentDepth">The current depth.</param> /// <returns> /// Instance of schema. /// </returns> private TypeSchema BuildRecordTypeSchema(Type type, Dictionary <string, NamedSchema> schemas, uint currentDepth) { if (type == typeof(DateTimeOffset)) { return(this.settings.UsePosixTime ? (TypeSchema) new LongSchema(type) : new StringSchema(type)); } NamedSchema schema; if (schemas.TryGetValue(type.ToString(), out schema)) { return(schema); } if (type == typeof(Guid)) { var recordName = new SchemaName(type.GetStrippedFullName()); var attributes = new NamedEntityAttributes(recordName, new List <string>(), string.Empty); var result = new FixedSchema(attributes, 16, type); schemas.Add(type.ToString(), result); return(result); } var attr = this.GetNamedEntityAttributesFrom(type); AvroContractResolver resolver = this.settings.Resolver; var record = new RecordSchema( attr, type); schemas.Add(type.ToString(), record); var members = resolver.ResolveMembers(type); this.AddRecordFields(members, schemas, currentDepth, record); return(record); }
internal FixedSchema(NamedEntityAttributes namedEntityAttributes, int size, Type runtimeType) : this(namedEntityAttributes, size, runtimeType, new Dictionary <string, string>()) { }
/// <summary> /// Initializes a new instance of the <see cref="EnumSchema"/> class. /// </summary> /// <param name="namedEntityAttributes">The named entity attributes.</param> /// <param name="runtimeType">Type of the runtime.</param> internal EnumSchema(NamedEntityAttributes namedEntityAttributes, Type runtimeType) : this(namedEntityAttributes, runtimeType, new Dictionary <string, string>()) { }