This commit is contained in:
Roger Peppe 2011-11-24 19:47:20 +00:00
Родитель 1632dd8118
Коммит 6491ec31f7
7 изменённых файлов: 18 добавлений и 31 удалений

Просмотреть файл

@ -7,9 +7,9 @@ package goyaml
import "C"
import (
"unsafe"
"reflect"
"strconv"
"unsafe"
)
const (
@ -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.
@ -196,7 +195,6 @@ func (p *parser) mapping() *node {
return n
}
// ----------------------------------------------------------------------------
// Decoder, unmarshals a node into a provided value.
@ -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
}
@ -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

Просмотреть файл

@ -1,14 +1,12 @@
package goyaml_test
import (
. "launchpad.net/gocheck"
"launchpad.net/goyaml"
"reflect"
"math"
"reflect"
)
var unmarshalIntTest = 123
var unmarshalTests = []struct {
@ -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()
@ -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))
}
}
@ -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{}

Просмотреть файл

@ -5,11 +5,10 @@ import "C"
import (
"reflect"
"unsafe"
"strconv"
"unsafe"
)
type encoder struct {
emitter C.yaml_emitter_t
event C.yaml_event_t
@ -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)
@ -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()

Просмотреть файл

@ -1,6 +1,5 @@
package goyaml_test
import (
. "launchpad.net/gocheck"
"launchpad.net/goyaml"
@ -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)

Просмотреть файл

@ -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)
@ -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)
@ -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)
@ -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()
@ -138,7 +138,6 @@ func Marshal(in interface{}) (out []byte, err os.Error) {
return
}
// --------------------------------------------------------------------------
// Maintain a mapping of keys to structure field indexes
@ -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()
@ -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

Просмотреть файл

@ -1,12 +1,11 @@
package goyaml
import (
"math"
"strconv"
"strings"
"math"
)
// TODO: merge, timestamps, base 60 floats, omap.
@ -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

Просмотреть файл

@ -1,6 +1,5 @@
package goyaml_test
import (
. "launchpad.net/gocheck"
"testing"