PropEncode/PropDecode -> EncodeProperty/DecodeProperty.

This commit is contained in:
scruzin 2019-01-13 07:51:35 +10:30
parent 9ca1a49178
commit 8a68cbca2f
2 changed files with 18 additions and 19 deletions

View File

@ -262,8 +262,8 @@ func EncodeNamedBoolean(buf []byte, key string, val bool) ([]byte, error) {
return EncodeBoolean(buf[len(key):], val)
}
// PropEncode encodes a property.
func PropEncode(p *Property, buf []byte) ([]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) {
return nil, ErrShortBuffer
}
@ -300,8 +300,8 @@ func PropEncode(p *Property, buf []byte) ([]byte, error) {
return buf, nil
}
// PropDecode decodes a property, returning the number of bytes consumed from the supplied buffer.
func PropDecode(prop *Property, buf []byte, decodeName bool) (int, error) {
// DecodeProperty decodes a property, returning the number of bytes consumed from the supplied buffer.
func DecodeProperty(prop *Property, buf []byte, decodeName bool) (int, error) {
sz := len(buf)
if len(buf) == 0 {
return 0, ErrEndOfBuffer
@ -391,7 +391,7 @@ func Encode(obj *Object, buf []byte) ([]byte, error) {
for i := 0; i < len(obj.Properties); i++ {
var err error
buf, err = PropEncode(&obj.Properties[i], buf)
buf, err = EncodeProperty(&obj.Properties[i], buf)
if err != nil {
return nil, err
}
@ -416,7 +416,7 @@ func EncodeEcmaArray(obj *Object, buf []byte) ([]byte, error) {
for i := 0; i < len(obj.Properties); i++ {
var err error
buf, err = PropEncode(&obj.Properties[i], buf)
buf, err = EncodeProperty(&obj.Properties[i], buf)
if err != nil {
return nil, err
}
@ -441,7 +441,7 @@ func EncodeArray(obj *Object, buf []byte) ([]byte, error) {
for i := 0; i < len(obj.Properties); i++ {
var err error
buf, err = PropEncode(&obj.Properties[i], buf)
buf, err = EncodeProperty(&obj.Properties[i], buf)
if err != nil {
return nil, err
}
@ -460,9 +460,8 @@ func Decode(obj *Object, buf []byte, decodeName bool) (int, error) {
buf = buf[3:]
break
}
var prop Property
n, err := PropDecode(&prop, buf, decodeName)
n, err := DecodeProperty(&prop, buf, decodeName)
if err != nil {
return 0, err
}

View File

@ -101,9 +101,9 @@ func TestProperties(t *testing.T) {
enc := buf[:]
var err error
for i, _ := range testNumbers {
enc, err = PropEncode(&Property{Type: typeNumber, Number: float64(testNumbers[i])}, enc)
enc, err = EncodeProperty(&Property{Type: typeNumber, Number: float64(testNumbers[i])}, enc)
if err != nil {
t.Errorf("PropEncode of Number failed")
t.Errorf("EncodeProperty of Number failed")
}
}
@ -111,12 +111,12 @@ func TestProperties(t *testing.T) {
prop := Property{}
dec := buf[:]
for i, _ := range testNumbers {
n, err := PropDecode(&prop, dec, false)
n, err := DecodeProperty(&prop, dec, false)
if err != nil {
t.Errorf("PropDecode of Number failed")
t.Errorf("DecodeProperty of Number failed")
}
if int32(prop.Number) != testNumbers[i] {
t.Errorf("PropEncode/PropDecode returned wrong Number; got %v, expected %v", int32(prop.Number), testNumbers[i])
t.Errorf("EncodeProperty/DecodeProperty returned wrong Number; got %v, expected %v", int32(prop.Number), testNumbers[i])
}
dec = dec[n:]
}
@ -124,21 +124,21 @@ func TestProperties(t *testing.T) {
// Encode/decode string properties.
enc = buf[:]
for i, _ := range testStrings {
enc, err = PropEncode(&Property{Type: typeString, String: testStrings[i]}, enc)
enc, err = EncodeProperty(&Property{Type: typeString, String: testStrings[i]}, enc)
if err != nil {
t.Errorf("PropEncode of string failed")
t.Errorf("EncodeProperty of string failed")
}
}
prop = Property{}
dec = buf[:]
for i, _ := range testStrings {
n, err := PropDecode(&prop, dec, false)
n, err := DecodeProperty(&prop, dec, false)
if err != nil {
t.Errorf("PropDecode of string failed")
t.Errorf("DecodeProperty of string failed")
}
if prop.String != testStrings[i] {
t.Errorf("PropEncode/PropDecode returned wrong string; got %s, expected %s", prop.String, testStrings[i])
t.Errorf("EncodeProperty/DecodeProperty returned wrong string; got %s, expected %s", prop.String, testStrings[i])
}
dec = dec[n:]
}