rybakit / msgpack.php

A pure PHP implementation of the MessagePack serialization format / msgpack.org[PHP]
MIT License
388 stars 18 forks source link

BufferUnpacker with Type transformers not decoding correctly #27

Closed tuaris closed 5 years ago

tuaris commented 5 years ago

I'm having trouble decoding a message that has an extension in a field, for example, this is what I get with MessagePack\MessagePack::unpack($msg)

Array
(
    [timestamp] => MessagePack\Ext Object
        (
            [type] => 5
            [data] => binary data
        )
    [item] => Array
        (
            [0] => Array...
            [1] => Array...
            [2] => Array...
        )
    .... and so on

I tried to use BufferUnpacker() with a Type transformer (as shown in #17 with small changes) but that generates a 0 epoch date and it fails to decode the other items properly.

I can manually unpack the binary data:

    $data = MessagePack\MessagePack::unpack($msg);
    $timestamp = unpack('Jtimestamp', $data['timestamp']->data);    
    $date = \DateTimeImmutable::createFromFormat('U', $timestamp['timestamp']);
    echo $date->format('r') . PHP_EOL;

Results in:

Fri, 30 Nov 2018 14:46:50 +0000
rybakit commented 5 years ago

Hey @tuaris. Could you please show your type transformer and the messagepack data you try to unpack?

tuaris commented 5 years ago

This is the type transformer:

final class MsgpackLiteJsDateTransformer implements \MessagePack\TypeTransformer\Unpackable {

    public function getType(): int {
    return 5;
    }

    public function unpack(\MessagePack\BufferUnpacker $unpacker, int $extLength): \DateTimeImmutable {
    return \DateTimeImmutable::createFromFormat('U', $unpacker->unpackInt());
    }

}

The data I'm trying to unpack is generated by this library: https://github.com/tinylib/msgp:

type Event struct {
    Timestamp int64 `json:"timestamp" msg:"timestamp"`
        Message string `json:"message,omitempty" msg:"message"`
}

func NewEvent() *Event {
    return &Event{
        Timestamp:   time.Now().UnixNano(),
        Message: "A message",
    }
}
rybakit commented 5 years ago

Could you provide a full go script or data it generates? (from your example I don't see how you wrap event timestamps into extensions). I tried this (which obviously doesn't utilize msgpack extensions):

package main

import (
        "fmt"
        "time"
)

//go:generate msgp

type Event struct {
        Timestamp int64 `json:"timestamp" msg:"timestamp"`
        Message string `json:"message,omitempty" msg:"message"`
}

func NewEvent() *Event {
        return &Event{
                Timestamp: time.Now().UnixNano(),
                Message: "A message",
        }
}

func main() {
        data, err := NewEvent().MarshalMsg(nil)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Event is encoded as %x\n", data)
}

which outputs

Event is encoded as 82a974696d657374616d70d3156db5a268337dfba76d657373616765a941206d657373616765

Php script:

$packed = hex2bin('82a974696d657374616d70d3156db5fdd07481c9a76d657373616765a941206d657373616765');
$unpacker = new BufferUnpacker($packed);

var_dump($unpacker->unpack());

Output:

array(2) {
  'timestamp' =>
  int(1544090348986597833)
  'message' =>
  string(9) "A message"
}
rybakit commented 5 years ago

Closing as there is no feedback.