Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 2
0
        private List<string> GetAliases(JObject type, string @namespace)
        {
            List<string> aliases = type.OptionalArrayProperty(
                Token.Aliases,
                (alias, index) =>
                {
                    if (alias.Type != JTokenType.String)
                    {
                        throw new SerializationException(
                            string.Format(CultureInfo.InvariantCulture, "Property 'aliases' has invalid value '{0}'.", alias));
                    }

                    var result = (string)alias;
                    if (string.IsNullOrEmpty(result))
                    {
                        throw new SerializationException(
                            string.Format(CultureInfo.InvariantCulture, "Alias is not allowed to be null or empty."));
                    }

                    return result;
                });

            return aliases
                .Select(alias => string.IsNullOrEmpty(@namespace) || alias.Contains(".") ? alias : @namespace + "." + alias)
                .ToList();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses a JSON object representing an Avro enumeration to a <see cref="Microsoft.Hadoop.Avro.Schema.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;
        }