Skip to content

Commit

Permalink
error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Peppe committed Nov 24, 2011
1 parent 1632dd8 commit 6491ec3
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 31 deletions.
8 changes: 3 additions & 5 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package goyaml
import "C"

import (
"unsafe"
"reflect"
"strconv"
"unsafe"
)

const (
Expand All @@ -34,7 +34,6 @@ func stry(s *C.yaml_char_t) string {
return C.GoString((*C.char)(unsafe.Pointer(s)))
}


// ----------------------------------------------------------------------------
// Parser, produces a node tree out of a libyaml event stream.

Expand Down Expand Up @@ -196,7 +195,6 @@ func (p *parser) mapping() *node {
return n
}


// ----------------------------------------------------------------------------
// Decoder, unmarshals a node into a provided value.

Expand Down Expand Up @@ -292,7 +290,7 @@ func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
}
d.aliases[n.value] = true
good = d.unmarshal(an, out)
d.aliases[n.value] = false, false
delete(d.aliases, n.value)
return good
}

Expand Down Expand Up @@ -357,7 +355,7 @@ func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
good = true
}
case reflect.Ptr:
switch resolved := resolved.(type) {
switch resolved.(type) {
case nil:
out.Set(reflect.Zero(out.Type()))
good = true
Expand Down
11 changes: 4 additions & 7 deletions decode_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package goyaml_test


import (
. "launchpad.net/gocheck"
"launchpad.net/goyaml"
"reflect"
"math"
"reflect"
)


var unmarshalIntTest = 123

var unmarshalTests = []struct {
Expand Down Expand Up @@ -131,7 +129,6 @@ var unmarshalTests = []struct {
{"a: &a [1, 2]\nb: *a", &struct{ B []int }{[]int{1, 2}}},
}


func (s *S) TestUnmarshal(c *C) {
for i, item := range unmarshalTests {
t := reflect.ValueOf(item.value).Type()
Expand Down Expand Up @@ -163,7 +160,7 @@ func (s *S) TestUnmarshalErrors(c *C) {
for _, item := range unmarshalErrorTests {
var value interface{}
err := goyaml.Unmarshal([]byte(item.data), &value)
c.Assert(err, Matches, item.error, Bug("Partial unmarshal: %#v", value))
c.Assert(err, ErrorMatches, item.error, Bug("Partial unmarshal: %#v", value))
}
}

Expand Down Expand Up @@ -226,8 +223,8 @@ func (s *S) TestUnmarshalWithFalseSetterIgnoresValue(c *C) {
setterResult[2] = false
setterResult[4] = false
defer func() {
setterResult[2] = false, false
setterResult[4] = false, false
delete(setterResult, 2)
delete(setterResult, 4)
}()

m := map[string]*typeWithSetter{}
Expand Down
6 changes: 2 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import "C"

import (
"reflect"
"unsafe"
"strconv"
"unsafe"
)


type encoder struct {
emitter C.yaml_emitter_t
event C.yaml_event_t
Expand All @@ -19,7 +18,6 @@ type encoder struct {
flow bool
}


//export outputHandler
func outputHandler(data unsafe.Pointer, buffer *C.uchar, size C.size_t) C.int {
e := (*encoder)(data)
Expand Down Expand Up @@ -253,7 +251,7 @@ func (e *encoder) nilv() {
}

func (e *encoder) emitScalar(value, anchor, tag string,
style C.yaml_scalar_style_t) {
style C.yaml_scalar_style_t) {
var canchor, ctag, cvalue *C.yaml_char_t
var cimplicit C.int
var free func()
Expand Down
2 changes: 0 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package goyaml_test


import (
. "launchpad.net/gocheck"
"launchpad.net/goyaml"
Expand Down Expand Up @@ -94,7 +93,6 @@ var marshalTests = []struct {
}{struct{ B string }{"c"}}},
}


func (s *S) TestMarshal(c *C) {
for _, item := range marshalTests {
data, err := goyaml.Marshal(item.value)
Expand Down
17 changes: 8 additions & 9 deletions goyaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
package goyaml

import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"os"
)

func handleErr(err *os.Error) {
func handleErr(err *error) {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
Expand All @@ -27,8 +27,8 @@ func handleErr(err *os.Error) {
} else if _, ok := r.(externalPanic); ok {
panic(r)
} else if s, ok := r.(string); ok {
*err = os.NewError("YAML error: " + s)
} else if e, ok := r.(os.Error); ok {
*err = errors.New("YAML error: " + s)
} else if e, ok := r.(error); ok {
*err = e
} else {
panic(r)
Expand Down Expand Up @@ -84,7 +84,7 @@ type Getter interface {
// var T t
// goyaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
//
func Unmarshal(in []byte, out interface{}) (err os.Error) {
func Unmarshal(in []byte, out interface{}) (err error) {
defer handleErr(&err)
d := newDecoder()
p := newParser(in)
Expand Down Expand Up @@ -128,7 +128,7 @@ func Unmarshal(in []byte, out interface{}) (err os.Error) {
// goyaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
// goyaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
//
func Marshal(in interface{}) (out []byte, err os.Error) {
func Marshal(in interface{}) (out []byte, err error) {
defer handleErr(&err)
e := newEncoder()
defer e.destroy()
Expand All @@ -138,7 +138,6 @@ func Marshal(in interface{}) (out []byte, err os.Error) {
return
}


// --------------------------------------------------------------------------
// Maintain a mapping of keys to structure field indexes

Expand All @@ -165,7 +164,7 @@ func (e externalPanic) String() string {
return string(e)
}

func getStructFields(st reflect.Type) (*structFields, os.Error) {
func getStructFields(st reflect.Type) (*structFields, error) {
path := st.PkgPath()
name := st.Name()

Expand Down Expand Up @@ -235,7 +234,7 @@ func getStructFields(st reflect.Type) (*structFields, os.Error) {

if _, found = fieldsMap[info.Key]; found {
msg := "Duplicated key '" + info.Key + "' in struct " + st.String()
return nil, os.NewError(msg)
return nil, errors.New(msg)
}

fieldsList[len(fieldsMap)] = info
Expand Down
4 changes: 1 addition & 3 deletions resolve.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package goyaml

import (
"math"
"strconv"
"strings"
"math"
)


// TODO: merge, timestamps, base 60 floats, omap.


Expand All @@ -18,7 +17,6 @@ type resolveMapItem struct {
var resolveTable = make([]byte, 256)
var resolveMap = make(map[string]resolveMapItem)


func init() {
t := resolveTable
t[int('+')] = 'S' // Sign
Expand Down
1 change: 0 additions & 1 deletion suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package goyaml_test


import (
. "launchpad.net/gocheck"
"testing"
Expand Down

0 comments on commit 6491ec3

Please sign in to comment.