mirror of https://bitbucket.org/ausocean/av.git
More idiomatic names for Object's property getters.
This commit is contained in:
parent
8a68cbca2f
commit
8f2a8ced9d
|
@ -68,7 +68,7 @@ const (
|
||||||
typeInvalid = 0xff
|
typeInvalid = 0xff
|
||||||
)
|
)
|
||||||
|
|
||||||
// AMF represents an AMF message (object), which is simply a collection of properties.
|
// AMF represents an AMF object, which is simply a collection of properties.
|
||||||
type Object struct {
|
type Object struct {
|
||||||
Properties []Property
|
Properties []Property
|
||||||
}
|
}
|
||||||
|
@ -263,25 +263,25 @@ func EncodeNamedBoolean(buf []byte, key string, val bool) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeProperty encodes a property.
|
// EncodeProperty encodes a property.
|
||||||
func EncodeProperty(p *Property, buf []byte) ([]byte, error) {
|
func EncodeProperty(prop *Property, buf []byte) ([]byte, error) {
|
||||||
if p.Type != TypeNull && len(p.Name)+2+1 >= len(buf) {
|
if prop.Type != TypeNull && len(prop.Name)+2+1 >= len(buf) {
|
||||||
return nil, ErrShortBuffer
|
return nil, ErrShortBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Type != TypeNull && len(p.Name) != 0 {
|
if prop.Type != TypeNull && len(prop.Name) != 0 {
|
||||||
binary.BigEndian.PutUint16(buf[:2], uint16(len(p.Name)))
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(prop.Name)))
|
||||||
buf = buf[2:]
|
buf = buf[2:]
|
||||||
copy(buf, p.Name)
|
copy(buf, prop.Name)
|
||||||
buf = buf[len(p.Name):]
|
buf = buf[len(prop.Name):]
|
||||||
}
|
}
|
||||||
|
|
||||||
switch p.Type {
|
switch prop.Type {
|
||||||
case typeNumber:
|
case typeNumber:
|
||||||
return EncodeNumber(buf, p.Number)
|
return EncodeNumber(buf, prop.Number)
|
||||||
case typeBoolean:
|
case typeBoolean:
|
||||||
return EncodeBoolean(buf, p.Number != 0)
|
return EncodeBoolean(buf, prop.Number != 0)
|
||||||
case typeString:
|
case typeString:
|
||||||
return EncodeString(buf, p.String)
|
return EncodeString(buf, prop.String)
|
||||||
case TypeNull:
|
case TypeNull:
|
||||||
if len(buf) < 2 {
|
if len(buf) < 2 {
|
||||||
return nil, ErrShortBuffer
|
return nil, ErrShortBuffer
|
||||||
|
@ -289,11 +289,11 @@ func EncodeProperty(p *Property, buf []byte) ([]byte, error) {
|
||||||
buf[0] = TypeNull
|
buf[0] = TypeNull
|
||||||
buf = buf[1:]
|
buf = buf[1:]
|
||||||
case TypeObject:
|
case TypeObject:
|
||||||
return Encode(&p.Object, buf)
|
return Encode(&prop.Object, buf)
|
||||||
case typeEcmaArray:
|
case typeEcmaArray:
|
||||||
return EncodeEcmaArray(&p.Object, buf)
|
return EncodeEcmaArray(&prop.Object, buf)
|
||||||
case typeStrictArray:
|
case typeStrictArray:
|
||||||
return EncodeArray(&p.Object, buf)
|
return EncodeArray(&prop.Object, buf)
|
||||||
default:
|
default:
|
||||||
return nil, ErrInvalidType
|
return nil, ErrInvalidType
|
||||||
}
|
}
|
||||||
|
@ -472,9 +472,11 @@ func Decode(obj *Object, buf []byte, decodeName bool) (int, error) {
|
||||||
return sz - len(buf), nil
|
return sz - len(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProperty returns a property, either by its index when idx is non-negative, or by its name otherwise.
|
// Object methods:
|
||||||
|
|
||||||
|
// Property returns a property, either by its index when idx is non-negative, or by its name otherwise.
|
||||||
// If the requested property is not found or the type does not match, an ErrPropertyNotFound error is returned.
|
// If the requested property is not found or the type does not match, an ErrPropertyNotFound error is returned.
|
||||||
func (obj *Object) GetProperty(name string, idx int, typ uint8) (*Property, error) {
|
func (obj *Object) Property(name string, idx int, typ uint8) (*Property, error) {
|
||||||
var prop *Property
|
var prop *Property
|
||||||
if idx >= 0 {
|
if idx >= 0 {
|
||||||
if idx < len(obj.Properties) {
|
if idx < len(obj.Properties) {
|
||||||
|
@ -494,27 +496,27 @@ func (obj *Object) GetProperty(name string, idx int, typ uint8) (*Property, erro
|
||||||
return prop, nil
|
return prop, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNumber is a wrapper for GetProperty that returns a Number property's value, if any.
|
// NumberProperty is a wrapper for Property that returns a Number property's value, if any.
|
||||||
func (obj *Object) GetNumber(name string, idx int) (float64, error) {
|
func (obj *Object) NumberProperty(name string, idx int) (float64, error) {
|
||||||
prop, err := obj.GetProperty(name, idx, typeNumber)
|
prop, err := obj.Property(name, idx, typeNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return prop.Number, nil
|
return prop.Number, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetString is a wrapper for GetProperty that returns a String property's value, if any.
|
// StringProperty is a wrapper for Property that returns a String property's value, if any.
|
||||||
func (obj *Object) GetString(name string, idx int) (string, error) {
|
func (obj *Object) StringProperty(name string, idx int) (string, error) {
|
||||||
prop, err := obj.GetProperty(name, idx, typeString)
|
prop, err := obj.Property(name, idx, typeString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return prop.String, nil
|
return prop.String, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetObject is a wrapper for GetProperty that returns an Object property's value, if any.
|
// ObjectProperty is a wrapper for Property that returns an Object property's value, if any.
|
||||||
func (obj *Object) GetObject(name string, idx int) (*Object, error) {
|
func (obj *Object) ObjectProperty(name string, idx int) (*Object, error) {
|
||||||
prop, err := obj.GetProperty(name, idx, TypeObject)
|
prop, err := obj.Property(name, idx, TypeObject)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,32 +232,32 @@ func TestObject(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find some properties that exist.
|
// Find some properties that exist.
|
||||||
prop, err := obj2.GetProperty("", 2, typeString)
|
s, err := obj2.StringProperty("", 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("GetProperty(2) failed")
|
t.Errorf("Property(2) failed")
|
||||||
}
|
}
|
||||||
if prop.String != "foo" {
|
if s != "foo" {
|
||||||
t.Errorf("GetProperty(2) returned wrong Property")
|
t.Errorf("Property(2) returned wrong Property")
|
||||||
}
|
}
|
||||||
prop, err = obj2.GetProperty("", 3, typeNumber)
|
n, err := obj2.NumberProperty("", 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("GetProperty(1) failed")
|
t.Errorf("Property(3) failed")
|
||||||
}
|
}
|
||||||
if prop.Number != 1 {
|
if n != 1 {
|
||||||
t.Errorf("GetProperty(1) returned wrong Property")
|
t.Errorf("Property(3) returned wrong Property")
|
||||||
}
|
}
|
||||||
prop, err = obj2.GetProperty("", 9, typeBoolean)
|
prop, err := obj2.Property("", 9, typeBoolean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("GetProperty(9) failed")
|
t.Errorf("Property(9) failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if prop.Number != 1 {
|
if prop.Number != 1 {
|
||||||
t.Errorf("GetProperty(9) returned wrong Property")
|
t.Errorf("Property(9) returned wrong Property")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to find one that doesn't exist.
|
// Try to find one that doesn't exist.
|
||||||
prop, err = obj2.GetProperty("", 10, TypeObject)
|
prop, err = obj2.Property("", 10, TypeObject)
|
||||||
if err != ErrPropertyNotFound {
|
if err != ErrPropertyNotFound {
|
||||||
t.Errorf("GetProperty(10) failed")
|
t.Errorf("Property(10) failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue