/// <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);
        }
Example #2
0
        /// <summary>
        /// Sets the fields.
        /// </summary>
        /// <param name="record">
        /// The record.
        /// </param>
        /// <param name="fields">
        /// The fields.
        /// </param>
        internal static void SetFields(RecordSchema record, IEnumerable <RecordField> fields)
        {
            if (record == null)
            {
                throw new ArgumentNullException("record");
            }

            if (fields == null)
            {
                throw new ArgumentNullException("fields");
            }

            if (record.Fields.Count != 0)
            {
                throw new InvalidOperationException("Fields can be set only on empty record.");
            }

            int fieldPosition = 0;

            foreach (var field in fields)
            {
                record.AddField(new RecordField(field.NamedEntityAttributes, field.TypeSchema, field.Order, field.HasDefaultValue, field.DefaultValue, field.MemberInfo, fieldPosition++));
            }
        }
        private List <RecordField> BuildReaderFields(RecordSchema w, RecordSchema r, int startPosition)
        {
            var readerFieldsWithDefault = r.Fields.Where(field => field.HasDefaultValue);
            var fieldsToAdd             = new List <RecordField>();

            foreach (var readerField in readerFieldsWithDefault)
            {
                if (!w.Fields.Any(f => this.DoNamesMatch(f, readerField)))
                {
                    var newField = new RecordField(
                        readerField.NamedEntityAttributes,
                        readerField.TypeSchema,
                        readerField.Order,
                        readerField.HasDefaultValue,
                        readerField.DefaultValue,
                        readerField.MemberInfo,
                        startPosition++)
                    {
                        UseDefaultValue = true
                    };

                    fieldsToAdd.Add(newField);
                }
            }

            if (r.RuntimeType == typeof(AvroRecord) &&
                r.Fields.Any(rf => !rf.HasDefaultValue && !w.Fields.Any(wf => this.DoNamesMatch(wf, rf))))
            {
                throw new SerializationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Fields without default values found in type '{0}'. Not corresponding writer fields found.",
                              r.RuntimeType));
            }
            return(fieldsToAdd);
        }