Skip to content

Commit

Permalink
Correct encoder with buffer
Browse files Browse the repository at this point in the history
need refactor for buffer
  • Loading branch information
2tvenom committed Mar 10, 2014
1 parent 825120f commit fc2a6a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
18 changes: 8 additions & 10 deletions src/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ package main
import (
"fmt"
"cbor"
// "io/ioutil"
// "bytes"
"bytes"
)

type Vector struct {
X, Y, Z int
}

func main() {
// var buffTest bytes.Buffer
//
// encoder := cbor.NewEncoder(&buffTest)
// encoder.Encode(Vector{1,2,3})
v := Vector{1,2,3}
buff, error := cbor.Encode(v)

if error != nil {
var buffTest bytes.Buffer
encoder := cbor.NewEncoder(&buffTest)
ok, error := encoder.Encode(v)

if !ok {
fmt.Errorf("Error decoding %s", error)
} else {
fmt.Printf("Variable Hex = % x\n", buff)
fmt.Printf("Variable = %v\n", buff)
fmt.Printf("Variable Hex = % x\n", buffTest.Bytes())
fmt.Printf("Variable = %v\n", buffTest.Bytes())
}
}
38 changes: 28 additions & 10 deletions src/cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,30 @@ var additionalLength = map[byte]byte{
additionalTypeIntUint64 : 8,
}

//exported decode func
func Decode(byteBuff *[]byte) (interface {}, error) {
data, _, err := decode(*byteBuff)
return data, err
type cborEncode struct {
buff *bytes.Buffer
}

func NewEncoder(buff *bytes.Buffer) (cborEncode){
return cborEncode{buff}
}

//data, _, err := decode(*byteBuff)

func (encoder *cborEncode) Encode(value interface{}) (bool, error){
buff, err := encode(value)
if err != nil {
return false, err
}

encoder.buff.Reset()
encoder.buff.Write(buff)

return true, nil
}



//decode with offset
func decode(buff []byte) (interface {}, int64, error) {
if len(buff) == 0 {
Expand Down Expand Up @@ -202,7 +220,7 @@ func unpack(byteBuff []byte, test interface{}) (error){
return err
}

func Encode(variable interface{}) ([]byte, error) {
func encode(variable interface{}) ([]byte, error) {
if variable == nil {
return encodeNil()
}
Expand Down Expand Up @@ -294,7 +312,7 @@ func encodeArray(variable interface{}) ([]byte, error) {

//array slice encode
for i:=0; i < inputSlice.Len(); i++ {
elementBuff, err := Encode(inputSlice.Index(i).Interface())
elementBuff, err := encode(inputSlice.Index(i).Interface())

if err != nil {
return nil, err
Expand Down Expand Up @@ -342,15 +360,15 @@ func encodeStruct(variable interface{}) ([]byte, error) {
continue
}

keyBuff, keyErr := Encode(strings.ToLower(fieldType.Name))
keyBuff, keyErr := encode(strings.ToLower(fieldType.Name))

if keyErr != nil {
return nil, keyErr
}

buff = append(buff, keyBuff...)

elementBuff, elemErr := Encode(inputStructValue.Field(i).Interface())
elementBuff, elemErr := encode(inputStructValue.Field(i).Interface())

if elemErr != nil {
return nil, elemErr
Expand Down Expand Up @@ -378,15 +396,15 @@ func encodeMap(variable interface{}) ([]byte, error) {

//map encode
for _, key := range inputSlice.MapKeys() {
keyBuff, keyErr := Encode(key.Interface())
keyBuff, keyErr := encode(key.Interface())

if keyErr != nil {
return nil, keyErr
}

buff = append(buff, keyBuff...)

elementBuff, elemErr := Encode(inputSlice.MapIndex(key).Interface())
elementBuff, elemErr := encode(inputSlice.MapIndex(key).Interface())

if elemErr != nil {
return nil, elemErr
Expand Down
18 changes: 0 additions & 18 deletions src/cbor/cborEncode.go

This file was deleted.

0 comments on commit fc2a6a8

Please sign in to comment.