// Parse implements the Parse function in `type Parser interface`. It determines // first the CommandHeader assosciated with the io.Reader, then creates a new // instance of the corresponding command type and then parses into it. // // If an error is encountered in parsing, or if no matching command can be // found, then an error will be returned. func (p *SimpleParser) Parse(r io.Reader) (Command, error) { meta := new(CommandHeader) if err := encoding.Unmarshal(r, meta); err != nil { return nil, err } factory, ok := p.typs[meta.Name] if !ok { return nil, fmt.Errorf( "cmd/stream: unknown NetStream command %s", meta.Name) } cmd := factory() if err := encoding.Unmarshal(r, cmd); err != nil { return nil, err } return cmd, nil }
func TestInvalidCommandHeaderRead(t *testing.T) { var header stream.CommandHeader buf := bytes.NewReader([]byte{ // Invalid payload, empty }) err := encoding.Unmarshal(buf, &header) assert.Equal(t, io.EOF, err) assert.Equal(t, stream.CommandHeader{}, header) }
func TestStrucScanWithNil(t *testing.T) { target := struct { Foo *amf0.Object }{} err := encoding.Unmarshal(bytes.NewBuffer([]byte{ 0x05, // <null> }), &target) assert.Nil(t, err) assert.Nil(t, target.Foo) }
func TestStructScanWithoutConverstion(t *testing.T) { target := struct { Foo amf0.Bool }{} err := encoding.Unmarshal(bytes.NewBuffer([]byte{ 0x01, 0x01, // <bool>, <true> }), &target) assert.Nil(t, err) assert.Equal(t, amf0.Bool(true), target.Foo) }
func TestSimpleUnmarshal(t *testing.T) { target := struct { Foo bool }{} err := encoding.Unmarshal(bytes.NewBuffer([]byte{ 0x01, 0x01, // <bool>, <true> }), &target) assert.Nil(t, err) assert.Equal(t, true, target.Foo) }
func TestUnmarshallingANilValue(t *testing.T) { target := struct { Foo amf0.Bool }{} err := encoding.Unmarshal(bytes.NewReader([]byte{ 0x05, // <null> }), &target) assert.Equal(t, "amf0: unable to assign nil to type amf0.Bool", err.Error()) }
func TestUnmarshallingANilPointerValue(t *testing.T) { target := struct { Foo *amf0.Bool }{} err := encoding.Unmarshal(bytes.NewReader([]byte{ 0x05, // <null> }), &target) assert.Nil(t, err) assert.Nil(t, target.Foo) }
func TestUnmarshallingANonNilValue(t *testing.T) { target := struct { Foo amf0.Bool }{} err := encoding.Unmarshal(bytes.NewReader([]byte{ 0x01, 0x01, // <bool>, <true> }), &target) assert.Nil(t, err) assert.EqualValues(t, true, target.Foo) }
func TestCommandHeaderRead(t *testing.T) { var header stream.CommandHeader buf := bytes.NewReader(ValidCommandHeader) err := encoding.Unmarshal(buf, &header) assert.Nil(t, err) assert.Equal(t, stream.CommandHeader{ Name: "onStatus", TransactionId: 0, Arguments: nil, }, header) }
// Parse implements the Parser.Parse method. It parses a Receivable type out of // the given AMF identifier (parsed from the io.Reader `r` as a source), or an // error in the following cases: // // 1) no corresponding command could be found // 2) an error occured during unmarshalling (see WatchBeam/rtmp) // // Otherwise the Receivable type is returned succesfully, and no error is // returned. func (p *SimpleParser) Parse(name *amf0.String, r io.Reader) (Receivable, error) { str := string(*name) factory := p.typs[str] if factory == nil { return nil, fmt.Errorf( "rtmp/cmd/conn: unknown command name: %v", str) } v := factory() if err := encoding.Unmarshal(r, v); err != nil { return nil, err } return v, nil }
// Read implements Data.Read. It uses the standard amf0-style procedure to // unmarshal the amf0 encoded data. func (d *DataFrame) Read(c *chunk.Chunk) error { return encoding.Unmarshal(bytes.NewReader(c.Data), d) }