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
|
||||
)
|
||||
|
||||
// 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 {
|
||||
Properties []Property
|
||||
}
|
||||
|
@ -263,25 +263,25 @@ func EncodeNamedBoolean(buf []byte, key string, val bool) ([]byte, error) {
|
|||
}
|
||||
|
||||
// EncodeProperty encodes a property.
|
||||
func EncodeProperty(p *Property, buf []byte) ([]byte, error) {
|
||||
if p.Type != TypeNull && len(p.Name)+2+1 >= len(buf) {
|
||||
func EncodeProperty(prop *Property, buf []byte) ([]byte, error) {
|
||||
if prop.Type != TypeNull && len(prop.Name)+2+1 >= len(buf) {
|
||||
return nil, ErrShortBuffer
|
||||
}
|
||||
|
||||
if p.Type != TypeNull && len(p.Name) != 0 {
|
||||
binary.BigEndian.PutUint16(buf[:2], uint16(len(p.Name)))
|
||||
if prop.Type != TypeNull && len(prop.Name) != 0 {
|
||||
binary.BigEndian.PutUint16(buf[:2], uint16(len(prop.Name)))
|
||||
buf = buf[2:]
|
||||
copy(buf, p.Name)
|
||||
buf = buf[len(p.Name):]
|
||||
copy(buf, prop.Name)
|
||||
buf = buf[len(prop.Name):]
|
||||
}
|
||||
|
||||
switch p.Type {
|
||||
switch prop.Type {
|
||||
case typeNumber:
|
||||
return EncodeNumber(buf, p.Number)
|
||||
return EncodeNumber(buf, prop.Number)
|
||||
case typeBoolean:
|
||||
return EncodeBoolean(buf, p.Number != 0)
|
||||
return EncodeBoolean(buf, prop.Number != 0)
|
||||
case typeString:
|
||||
return EncodeString(buf, p.String)
|
||||
return EncodeString(buf, prop.String)
|
||||
case TypeNull:
|
||||
if len(buf) < 2 {
|
||||
return nil, ErrShortBuffer
|
||||
|
@ -289,11 +289,11 @@ func EncodeProperty(p *Property, buf []byte) ([]byte, error) {
|
|||
buf[0] = TypeNull
|
||||
buf = buf[1:]
|
||||
case TypeObject:
|
||||
return Encode(&p.Object, buf)
|
||||
return Encode(&prop.Object, buf)
|
||||
case typeEcmaArray:
|
||||
return EncodeEcmaArray(&p.Object, buf)
|
||||
return EncodeEcmaArray(&prop.Object, buf)
|
||||
case typeStrictArray:
|
||||
return EncodeArray(&p.Object, buf)
|
||||
return EncodeArray(&prop.Object, buf)
|
||||
default:
|
||||
return nil, ErrInvalidType
|
||||
}
|
||||
|
@ -472,9 +472,11 @@ func Decode(obj *Object, buf []byte, decodeName bool) (int, error) {
|
|||
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.
|
||||
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
|
||||
if idx >= 0 {
|
||||
if idx < len(obj.Properties) {
|
||||
|
@ -494,27 +496,27 @@ func (obj *Object) GetProperty(name string, idx int, typ uint8) (*Property, erro
|
|||
return prop, nil
|
||||
}
|
||||
|
||||
// GetNumber is a wrapper for GetProperty that returns a Number property's value, if any.
|
||||
func (obj *Object) GetNumber(name string, idx int) (float64, error) {
|
||||
prop, err := obj.GetProperty(name, idx, typeNumber)
|
||||
// NumberProperty is a wrapper for Property that returns a Number property's value, if any.
|
||||
func (obj *Object) NumberProperty(name string, idx int) (float64, error) {
|
||||
prop, err := obj.Property(name, idx, typeNumber)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return prop.Number, nil
|
||||
}
|
||||
|
||||
// GetString is a wrapper for GetProperty that returns a String property's value, if any.
|
||||
func (obj *Object) GetString(name string, idx int) (string, error) {
|
||||
prop, err := obj.GetProperty(name, idx, typeString)
|
||||
// StringProperty is a wrapper for Property that returns a String property's value, if any.
|
||||
func (obj *Object) StringProperty(name string, idx int) (string, error) {
|
||||
prop, err := obj.Property(name, idx, typeString)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return prop.String, nil
|
||||
}
|
||||
|
||||
// GetObject is a wrapper for GetProperty that returns an Object property's value, if any.
|
||||
func (obj *Object) GetObject(name string, idx int) (*Object, error) {
|
||||
prop, err := obj.GetProperty(name, idx, TypeObject)
|
||||
// ObjectProperty is a wrapper for Property that returns an Object property's value, if any.
|
||||
func (obj *Object) ObjectProperty(name string, idx int) (*Object, error) {
|
||||
prop, err := obj.Property(name, idx, TypeObject)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -232,32 +232,32 @@ func TestObject(t *testing.T) {
|
|||
}
|
||||
|
||||
// Find some properties that exist.
|
||||
prop, err := obj2.GetProperty("", 2, typeString)
|
||||
s, err := obj2.StringProperty("", 2)
|
||||
if err != nil {
|
||||
t.Errorf("GetProperty(2) failed")
|
||||
t.Errorf("Property(2) failed")
|
||||
}
|
||||
if prop.String != "foo" {
|
||||
t.Errorf("GetProperty(2) returned wrong Property")
|
||||
if s != "foo" {
|
||||
t.Errorf("Property(2) returned wrong Property")
|
||||
}
|
||||
prop, err = obj2.GetProperty("", 3, typeNumber)
|
||||
n, err := obj2.NumberProperty("", 3)
|
||||
if err != nil {
|
||||
t.Errorf("GetProperty(1) failed")
|
||||
t.Errorf("Property(3) failed")
|
||||
}
|
||||
if prop.Number != 1 {
|
||||
t.Errorf("GetProperty(1) returned wrong Property")
|
||||
if n != 1 {
|
||||
t.Errorf("Property(3) returned wrong Property")
|
||||
}
|
||||
prop, err = obj2.GetProperty("", 9, typeBoolean)
|
||||
prop, err := obj2.Property("", 9, typeBoolean)
|
||||
if err != nil {
|
||||
t.Errorf("GetProperty(9) failed")
|
||||
t.Errorf("Property(9) failed")
|
||||
return
|
||||
}
|
||||
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.
|
||||
prop, err = obj2.GetProperty("", 10, TypeObject)
|
||||
prop, err = obj2.Property("", 10, TypeObject)
|
||||
if err != ErrPropertyNotFound {
|
||||
t.Errorf("GetProperty(10) failed")
|
||||
t.Errorf("Property(10) failed")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue