def read_record(fo, writer_schema, reader_schema=None):
    """A record is encoded by encoding the values of its fields in the order
    that they are declared. In other words, a record is encoded as just the
    concatenation of the encodings of its fields.  Field values are encoded per
    their schema.

    Schema Resolution:
     * the ordering of fields may be different: fields are matched by name.
     * schemas for fields with the same name in both records are resolved
         recursively.
     * if the writer's record contains a field with a name not present in the
         reader's record, the writer's value for that field is ignored.
     * if the reader's record schema has a field that contains a default value,
         and writer's schema does not have a field with the same name, then the
         reader should use the default value from its field.
     * if the reader's record schema has a field with no default value, and
         writer's schema does not have a field with the same name, then the
         field's value is unset.
    """
    record = {}
    if reader_schema is None:
        for field in writer_schema['fields']:
            record[field['name']] = read_data(fo, field['type'])
    else:
        readers_field_dict = {}
        aliases_field_dict = {}
        for f in reader_schema['fields']:
            readers_field_dict[f['name']] = f
            for alias in f.get('aliases', []):
                aliases_field_dict[alias] = f

        for field in writer_schema['fields']:
            readers_field = readers_field_dict.get(
                field['name'],
                aliases_field_dict.get(field['name']),
            )
            if readers_field:
                record[readers_field['name']] = read_data(
                    fo,
                    field['type'],
                    readers_field['type'],
                )
            else:
                # should implement skip
                read_data(fo, field['type'], field['type'])

        # fill in default values
        if len(readers_field_dict) > len(record):
            writer_fields = [f['name'] for f in writer_schema['fields']]
            for f_name, field in iteritems(readers_field_dict):
                if f_name not in writer_fields and f_name not in record:
                    if 'default' in field:
                        record[field['name']] = field['default']
                    else:
                        msg = 'No default value for %s' % field['name']
                        raise SchemaResolutionError(msg)

    return record
def read_record ( fo , writer_schema , reader_schema = None ) : record = { } if reader_schema is None : for field in writer_schema [ 'fields' ] : record [ field [ 'name' ] ] = read_data ( fo , field [ 'type' ] ) else : readers_field_dict = { } aliases_field_dict = { } for f in reader_schema [ 'fields' ] : readers_field_dict [ f [ 'name' ] ] = f for alias in f . get ( 'aliases' , [ ] ) : aliases_field_dict [ alias ] = f for field in writer_schema [ 'fields' ] : readers_field = readers_field_dict . get ( field [ 'name' ] , aliases_field_dict . get ( field [ 'name' ] ) , ) if readers_field : record [ readers_field [ 'name' ] ] = read_data ( fo , field [ 'type' ] , readers_field [ 'type' ] , ) else : read_data ( fo , field [ 'type' ] , field [ 'type' ] ) if len ( readers_field_dict ) > len ( record ) : writer_fields = [ f [ 'name' ] for f in writer_schema [ 'fields' ] ] for f_name , field in iteritems ( readers_field_dict ) : if f_name not in writer_fields and f_name not in record : if 'default' in field : record [ field [ 'name' ] ] = field [ 'default' ] else : msg = 'No default value for %s' % field [ 'name' ] raise SchemaResolutionError ( msg ) return record
A record is encoded by encoding the values of its fields in the order
    that they are declared. In other words, a record is encoded as just the
    concatenation of the encodings of its fields.  Field values are encoded per
    their schema.

    Schema Resolution:
     * the ordering of fields may be different: fields are matched by name.
     * schemas for fields with the same name in both records are resolved
         recursively.
     * if the writer's record contains a field with a name not present in the
         reader's record, the writer's value for that field is ignored.
     * if the reader's record schema has a field that contains a default value,
         and writer's schema does not have a field with the same name, then the
         reader should use the default value from its field.
     * if the reader's record schema has a field with no default value, and
         writer's schema does not have a field with the same name, then the
         field's value is unset.