Skip to content

Commit

Permalink
Added full message data structures and implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
oldgalileo committed Apr 25, 2017
1 parent a920897 commit 099c5c8
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 33 deletions.
55 changes: 41 additions & 14 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const (
TypeRPCDef = 0x20
TypeUndef = 0xFF

FlagTemporary EntryFlag = 0x00
FlagPersistent = 0x01
FlagReserved = 0xFE
FlagEntryTemporary EntryFlag = 0x00
FlagEntryPersistent = 0x01
FlagEntryReserved = 0xFE
FlagEntryUndef = 0xFF
)

const (
Expand All @@ -29,23 +30,29 @@ const (

var (
ErrEntryTypeCastInvalid = errors.New("entry: could not cast entrytype to type")

ErrEntryCastInvalid = errors.New("entry: could not cast entryvalue to type")
ErrEntryNoSuchType = errors.New("entry: no such type")
ErrEntryDataInvalid = errors.New("entry: data invalid")
ErrArrayIndexOutOfBounds = errors.New("array: index out of bounds")
ErrArrayOutOfSpace = errors.New("array: no more space")

ErrArrayIndexOutOfBounds = errors.New("entryarray: index out of bounds")
ErrArrayOutOfSpace = errors.New("entryarray: no more space")

ErrEntryFlagNoSuchType = errors.New("entryflag: no such flag")
)

type EntryType byte
type EntryFlag byte

type Entry struct {
Name ValueString
Name *ValueString
Type EntryType
ID [2]byte
Sequence [2]byte
Flags EntryFlag
Value EntryValue
}

type EntryFlag byte

type EntryValue interface {
GetRaw() []byte
}
Expand All @@ -57,6 +64,24 @@ type EntryValueArray interface {
EntryValue
}

func DecodeEntryFlag(r io.Reader) (EntryFlag, error) {
flagRaw := make([]byte, 1)
_, flagErr := r.Read(flagRaw)
if flagErr != nil {
return FlagEntryUndef, flagErr
}
switch EntryFlag(flagRaw[0]) {
case FlagEntryPersistent:
return FlagEntryPersistent, nil
case FlagEntryTemporary:
return FlagEntryTemporary, nil
case FlagEntryReserved:
return FlagEntryReserved, nil
default:
return FlagEntryUndef, ErrEntryFlagNoSuchType
}
}

func DecodeEntryType(r io.Reader) (EntryType, error) {
rawType := make([]byte, 1)
_, readErr := r.Read(rawType)
Expand All @@ -66,16 +91,18 @@ func DecodeEntryType(r io.Reader) (EntryType, error) {
return EntryType(rawType[0]), nil
}

func DecodeEntry(r io.Reader) (EntryValue, error) {
entryType := make([]byte, 1)
_, readErr := r.Read(entryType)
func DecodeEntryValueAndType(r io.Reader) (value EntryValue, entryType EntryType, err error) {
entryTypeRaw := make([]byte, 1)
_, readErr := r.Read(entryTypeRaw)
if readErr != nil {
return nil, readErr
return nil, TypeUndef, readErr
}
return DecodeEntryWithType(r, EntryType(entryType[0]))
entryType = EntryType(entryTypeRaw[0])
value, err = DecodeEntryValue(r, entryType)
return
}

func DecodeEntryWithType(r io.Reader, entryType EntryType) (EntryValue, error) {
func DecodeEntryValue(r io.Reader, entryType EntryType) (EntryValue, error) {
switch entryType {
case TypeBoolean:
return DecodeBoolean(r)
Expand Down
24 changes: 12 additions & 12 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func TestBuildStringArray(t *testing.T) {
}
}

func TestDecodeEntryBoolean(t *testing.T) {
func TestDecodeEntryValueBoolean(t *testing.T) {
entryBytes := []byte{byte(TypeBoolean),BoolFalse}
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand All @@ -98,9 +98,9 @@ func TestDecodeEntryBoolean(t *testing.T) {
}
}

func TestDecodeEntryString(t *testing.T) {
func TestDecodeEntryValueString(t *testing.T) {
entryBytes := []byte{byte(TypeString),0x05,0x6f,0x74,0x68,0x65,0x72} // Value of other
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand All @@ -113,9 +113,9 @@ func TestDecodeEntryString(t *testing.T) {
}
}

func TestDecodeEntryDouble(t *testing.T) {
func TestDecodeEntryValueDouble(t *testing.T) {
entryBytes := []byte{byte(TypeDouble),0x3f,0xe0,0x00,0x00,0x00,0x00,0x00,0x00}
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand All @@ -128,9 +128,9 @@ func TestDecodeEntryDouble(t *testing.T) {
}
}

func TestDecodeEntryBooleanArray(t *testing.T) {
func TestDecodeEntryValueBooleanArray(t *testing.T) {
entryBytes := []byte{byte(TypeBooleanArr),byte(uint8(1)),0x01}
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand All @@ -145,9 +145,9 @@ func TestDecodeEntryBooleanArray(t *testing.T) {
}
}

func TestDecodeEntryDoubleArray(t *testing.T) {
func TestDecodeEntryValueDoubleArray(t *testing.T) {
entryBytes := []byte{byte(TypeDoubleArr),byte(uint8(1)),0x3f,0xf2,0xe1,0x47,0xae,0x14,0x7a,0xe1}
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand All @@ -162,9 +162,9 @@ func TestDecodeEntryDoubleArray(t *testing.T) {
}
}

func TestDecodeEntryStringArray(t *testing.T) {
func TestDecodeEntryValueStringArray(t *testing.T) {
entryBytes := []byte{byte(TypeStringArr), byte(uint8(1)),0x05,0x61,0x72,0x72,0x61,0x79}
result, err := DecodeEntry(bytes.NewBuffer(entryBytes))
result, _, err := DecodeEntryValueAndType(bytes.NewBuffer(entryBytes))
if err != nil {
t.Fatalf("Unexpected error! %s", err)
}
Expand Down
Loading

0 comments on commit 099c5c8

Please sign in to comment.