Skip to content

Commit

Permalink
Good example
Browse files Browse the repository at this point in the history
Ptr encode fixed
  • Loading branch information
2tvenom committed Mar 15, 2014
1 parent 372d852 commit efec77f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
24 changes: 20 additions & 4 deletions src/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ import (

type Vector struct {
X, Y, Z int
Range []Range
Label string
}

func main() {
var v float64
type Range struct {
Length int
Align float32
}

v = 12.3
func main() {
v := &Vector {
X: 10,
Y: 15,
Z: 100,
Range: []Range{
Range {1,10},
Range {223432423,30},
Range {3,41.5},
Range {174,55555.2},
},
Label: "HoHoHo",
}

var buffTest bytes.Buffer
encoder := cbor.NewEncoder(&buffTest)
Expand All @@ -27,7 +43,7 @@ func main() {
}
fmt.Printf("-----------------\n")

var vd float64
var vd Vector

ok, err := encoder.Unmarshal(buffTest.Bytes(), &vd)

Expand Down
6 changes: 4 additions & 2 deletions src/cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func NewEncoder(buff *bytes.Buffer) (cborEncode){

func (encoder *cborEncode) Marshal(value interface{}) (bool, error){
encoder.buff.Reset()
ok, err := encoder.encodeValue(value)

ok, err := encoder.encodeValue(value)
if !ok {
return false, err
}
Expand All @@ -87,12 +88,13 @@ func (encoder *cborEncode) Marshal(value interface{}) (bool, error){
}

func (encoder *cborEncode) encodeValue(variable interface{}) (bool, error) {

if variable == nil {
return encoder.encodeNil()
}

switch reflect.TypeOf(variable).Kind() {
case reflect.Ptr:
return encoder.encodeValue(reflect.ValueOf(variable).Elem().Interface())
case reflect.Int:
return encoder.encodeNumber(variable.(int))
case reflect.String:
Expand Down

0 comments on commit efec77f

Please sign in to comment.