Maps allow integers as keys - #27
Conversation
|
Merged master (8c040b4) in and added a test case. Some extra detail on why this matters: without this change the library cannot read its own output. Given valid Avro bytes for the map $schema = AvroSchema::parse('{"type":"map","values":"int"}');
$io = new AvroStringIO(hex2bin('040631323302063435360400'));
$read = (new AvroIODatumReader($schema))->read(new AvroIOBinaryDecoder($io));
var_export($read); // array(123 => 1, 456 => 2)
var_export(AvroSchema::is_valid_datum($schema, $read)); // false
$out = new AvroStringIO();
(new AvroIODatumWriter($schema))->write($read, new AvroIOBinaryEncoder($out));
// AvroIOTypeException: The datum array (123 => 1, 456 => 2) is not an example of
// schema {"type":"map","values":{"type":"int"}}The reader hands back int keys, because PHP casts digit-only array keys to int, and then the validator rejects them. A map read from one file cannot be written back out to another. Both changed lines are needed, and they fail in different places. With only the The new case in For background on how we hit this: we serialise maps whose keys are user-supplied identifiers, so they arrive as a mix of |
Added support so maps allow both strings and ints as keys.
Keys in PHP arrays will automatically cast to integers if all characters are integer digits. This change allows both strings and ints in the validation and then casts the key to a string when doing the actual encoding.
e.g.