make gofmt + s/float/float64/
diff --git a/Makefile b/Makefile index 5114a49..4752710 100644 --- a/Makefile +++ b/Makefile
@@ -25,4 +25,18 @@ CGO_LDFLAGS+=-lm -lpthread CGO_CFLAGS+=-I. -DHAVE_CONFIG_H=1 +GOFMT=gofmt -spaces=true -tabwidth=4 -tabindent=false + +BADFMT:=$(shell $(GOFMT) -l $(GOFILES) $(CGOFILES) $(wildcard *_test.go)) + +all: package +gofmt: $(BADFMT) + @for F in $(BADFMT); do $(GOFMT) -w $$F && echo $$F; done + include $(GOROOT)/src/Make.pkg + +ifneq ($(BADFMT),) +ifneq ($(MAKECMDGOALS),gofmt) +$(warning WARNING: make gofmt: $(BADFMT)) +endif +endif
diff --git a/decode.go b/decode.go index 55544e6..e3749ef 100644 --- a/decode.go +++ b/decode.go
@@ -18,13 +18,13 @@ ) type node struct { - kind int + kind int line, column int - tag string - value string - implicit bool - children []*node - anchors map[string]*node + tag string + value string + implicit bool + children []*node + anchors map[string]*node } func stry(s *C.yaml_char_t) string { @@ -37,8 +37,8 @@ type parser struct { parser C.yaml_parser_t - event C.yaml_event_t - doc *node + event C.yaml_event_t + doc *node } func newParser(b []byte) *parser { @@ -59,7 +59,7 @@ p.skip() if p.event._type != C.YAML_STREAM_START_EVENT { panic("Expected stream start event, got " + - strconv.Itoa(int(p.event._type))) + strconv.Itoa(int(p.event._type))) } p.skip() return &p @@ -127,15 +127,15 @@ return nil default: panic("Attempted to parse unknown event: " + - strconv.Itoa(int(p.event._type))) + strconv.Itoa(int(p.event._type))) } panic("Unreachable") } func (p *parser) node(kind int) *node { return &node{kind: kind, - line: int(C.int(p.event.start_mark.line)), - column: int(C.int(p.event.start_mark.column))} + line: int(C.int(p.event.start_mark.line)), + column: int(C.int(p.event.start_mark.column))} } func (p *parser) document() *node { @@ -146,7 +146,7 @@ n.children = append(n.children, p.parse()) if p.event._type != C.YAML_DOCUMENT_END_EVENT { panic("Expected end of document event but got " + - strconv.Itoa(int(p.event._type))) + strconv.Itoa(int(p.event._type))) } p.skip() return n @@ -198,7 +198,7 @@ // Decoder, unmarshals a node into a provided value. type decoder struct { - doc *node + doc *node aliases map[string]bool } @@ -345,8 +345,8 @@ } case *reflect.FloatValue: switch resolved := resolved.(type) { - case float: - out.Set(float64(resolved)) + case float64: + out.Set(resolved) good = true } case *reflect.PtrValue:
diff --git a/decode_test.go b/decode_test.go index 57a6653..aca12ca 100644 --- a/decode_test.go +++ b/decode_test.go
@@ -2,7 +2,7 @@ import ( - . "gocheck" + . "gocheck" "goyaml" "reflect" "math" @@ -11,7 +11,10 @@ var unmarshalIntTest = 123 -var unmarshalTests = []struct{data string; value interface{}}{ +var unmarshalTests = []struct { + data string + value interface{} +}{ {"", &struct{}{}}, {"{}", &struct{}{}}, @@ -41,7 +44,7 @@ //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported {"neginf: -.inf", map[string]interface{}{"neginf": math.Inf(-1)}}, {"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, - {"fixed: 685_230.15", map[string]float{"fixed": 685230.15}}, + {"fixed: 685_230.15", map[string]float64{"fixed": 685230.15}}, // Bools from spec {"canonical: y", map[string]interface{}{"canonical": true}}, @@ -76,17 +79,27 @@ // Map inside interface with no type hints. {"a: {b: c}", - map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}}, + map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}}, // Structs and type conversions. - {"hello: world", &struct{Hello string}{"world"}}, - {"a: {b: c}", &struct{A struct{B string}}{struct{B string}{"c"}}}, - {"a: {b: c}", &struct{A *struct{B string}}{&struct{B string}{"c"}}}, - {"a: 1", &struct{A int}{1}}, - {"a: [1, 2]", &struct{A []int}{[]int{1, 2}}}, - {"a: 1", &struct{B int}{0}}, - {"a: 1", &struct{B int "a"}{1}}, - {"a: y", &struct{A bool}{true}}, + {"hello: world", &struct{ Hello string }{"world"}}, + {"a: {b: c}", &struct { + A struct { + B string + } + }{struct{ B string }{"c"}}}, + {"a: {b: c}", &struct { + A *struct { + B string + } + }{&struct{ B string }{"c"}}}, + {"a: 1", &struct{ A int }{1}}, + {"a: [1, 2]", &struct{ A []int }{[]int{1, 2}}}, + {"a: 1", &struct{ B int }{0}}, + {"a: 1", &struct { + B int "a" + }{1}}, + {"a: y", &struct{ A bool }{true}}, // Some cross type conversions {"v: 42", map[string]uint{"v": 42}}, @@ -105,13 +118,17 @@ {"v: !!float '1.1'", map[string]interface{}{"v": 1.1}}, {"v: !!null ''", map[string]interface{}{"v": nil}}, {"%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'", - map[string]interface{}{"v": 1}}, + map[string]interface{}{"v": 1}}, // Anchors and aliases. - {"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", &struct{A, B, C, D int}{1, 2, 1, 2}}, + {"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", &struct{ A, B, C, D int }{1, 2, 1, 2}}, {"a: &a {c: 1}\nb: *a", - &struct{A, B struct{C int}}{struct{C int}{1}, struct{C int}{1}}}, - {"a: &a [1, 2]\nb: *a", &struct{B []int}{[]int{1, 2}}}, + &struct { + A, B struct { + C int + } + }{struct{ C int }{1}, struct{ C int }{1}}}, + {"a: &a [1, 2]\nb: *a", &struct{ B []int }{[]int{1, 2}}}, } @@ -133,9 +150,11 @@ } } -var unmarshalErrorTests = []struct{data, error string}{ +var unmarshalErrorTests = []struct { + data, error string +}{ {"v: !!float 'error'", - "YAML error: Can't decode !!str 'error' as a !!float"}, + "YAML error: Can't decode !!str 'error' as a !!float"}, {"v: [A,", "YAML error: line 1: did not find expected node content"}, {"v:\n- [A,", "YAML error: line 2: did not find expected node content"}, {"a: *b\n", "YAML error: Unknown anchor 'b' referenced"}, @@ -150,7 +169,10 @@ } } -var setterTests = []struct{data, tag string; value interface{}}{ +var setterTests = []struct { + data, tag string + value interface{} +}{ {"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}}, {"_: [1,A]", "!!seq", []interface{}{1, "A"}}, {"_: 10", "!!int", 10}, @@ -161,7 +183,7 @@ var setterResult = map[int]bool{} type typeWithSetter struct { - tag string + tag string value interface{} } @@ -186,7 +208,7 @@ err := goyaml.Unmarshal([]byte(item.data), obj) c.Assert(err, IsNil) c.Assert(obj.Field, NotNil, - Bug("Pointer not initialized (%#v)", item.value)) + Bug("Pointer not initialized (%#v)", item.value)) c.Assert(obj.Field.tag, Equals, item.tag) c.Assert(obj.Field.value, Equals, item.value) }
diff --git a/encode.go b/encode.go index 3f9a6ce..8233eb7 100644 --- a/encode.go +++ b/encode.go
@@ -12,11 +12,11 @@ type encoder struct { emitter C.yaml_emitter_t - event C.yaml_event_t - out []byte - tmp []byte - tmph *reflect.SliceHeader - flow bool + event C.yaml_event_t + out []byte + tmp []byte + tmph *reflect.SliceHeader + flow bool } @@ -59,8 +59,8 @@ func (e *encoder) emit() { // This will internally delete the e.event value. if C.yaml_emitter_emit(&e.emitter, &e.event) == 0 && - e.event._type != C.YAML_DOCUMENT_END_EVENT && - e.event._type != C.YAML_STREAM_END_EVENT { + e.event._type != C.YAML_DOCUMENT_END_EVENT && + e.event._type != C.YAML_STREAM_END_EVENT { if e.emitter.error == C.YAML_EMITTER_ERROR { // XXX TESTME panic("YAML emitter error: " + C.GoString(e.emitter.problem)) @@ -168,7 +168,7 @@ cstyle = C.YAML_FLOW_MAPPING_STYLE } C.yaml_mapping_start_event_initialize(&e.event, nil, ctag, cimplicit, - cstyle) + cstyle) e.emit() f() C.yaml_mapping_end_event_initialize(&e.event) @@ -193,7 +193,7 @@ cstyle = C.YAML_FLOW_SEQUENCE_STYLE } C.yaml_sequence_start_event_initialize(&e.event, nil, ctag, cimplicit, - cstyle) + cstyle) e.emit() n := in.Len() for i := 0; i < n; i++ { @@ -238,9 +238,12 @@ // FIXME: Handle 64 bits here. s := strconv.Ftoa32(float32(in.Get()), 'g', -1) switch s { - case "+Inf": s = ".inf" - case "-Inf": s = "-.inf" - case "NaN": s = ".nan" + case "+Inf": + s = ".inf" + case "-Inf": + s = "-.inf" + case "NaN": + s = ".nan" } e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE) } @@ -250,7 +253,7 @@ } 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() @@ -270,7 +273,7 @@ defer free() size := C.int(len(value)) if C.yaml_scalar_event_initialize(&e.event, canchor, ctag, cvalue, size, - cimplicit, cimplicit, style) == 0 { + cimplicit, cimplicit, style) == 0 { e.fail("") } e.emit()
diff --git a/encode_test.go b/encode_test.go index 1c3d85c..2a8ceb1 100644 --- a/encode_test.go +++ b/encode_test.go
@@ -2,7 +2,7 @@ import ( - . "gocheck" + . "gocheck" "goyaml" "math" //"reflect" @@ -10,7 +10,10 @@ var marshalIntTest = 123 -var marshalTests = []struct{data string; value interface{}}{ +var marshalTests = []struct { + data string + value interface{} +}{ {"{}\n", &struct{}{}}, {"v: hi\n", map[string]string{"v": "hi"}}, {"v: hi\n", map[string]interface{}{"v": "hi"}}, @@ -36,31 +39,59 @@ {"v:\n- A\n- B\n", map[string][]string{"v": []string{"A", "B"}}}, {"v:\n- A\n- 1\n", map[string][]interface{}{"v": []interface{}{"A", 1}}}, {"a:\n b: c\n", - map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}}, + map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}}, // Simple values. {"123\n", &marshalIntTest}, // Structures - {"hello: world\n", &struct{Hello string}{"world"}}, - {"a:\n b: c\n", &struct{A struct{B string}}{struct{B string}{"c"}}}, - {"a:\n b: c\n", &struct{A *struct{B string}}{&struct{B string}{"c"}}}, - {"a: null\n", &struct{A *struct{B string}}{}}, - {"a: 1\n", &struct{A int}{1}}, - {"a:\n- 1\n- 2\n", &struct{A []int}{[]int{1, 2}}}, - {"a: 1\n", &struct{B int "a"}{1}}, - {"a: true\n", &struct{A bool}{true}}, + {"hello: world\n", &struct{ Hello string }{"world"}}, + {"a:\n b: c\n", &struct { + A struct { + B string + } + }{struct{ B string }{"c"}}}, + {"a:\n b: c\n", &struct { + A *struct { + B string + } + }{&struct{ B string }{"c"}}}, + {"a: null\n", &struct { + A *struct { + B string + } + }{}}, + {"a: 1\n", &struct{ A int }{1}}, + {"a:\n- 1\n- 2\n", &struct{ A []int }{[]int{1, 2}}}, + {"a: 1\n", &struct { + B int "a" + }{1}}, + {"a: true\n", &struct{ A bool }{true}}, // Conditional flag - {"a: 1\n", &struct{A int "a/c"; B int "b/c"}{1, 0}}, - {"{}\n", &struct{A int "a/c"; B int "b/c"}{0, 0}}, + {"a: 1\n", &struct { + A int "a/c" + B int "b/c" + }{1, 0}}, + {"{}\n", &struct { + A int "a/c" + B int "b/c" + }{0, 0}}, // Flow flag - {"a: [1, 2]\n", &struct{A []int "a/f"}{[]int{1, 2}}}, + {"a: [1, 2]\n", &struct { + A []int "a/f" + }{[]int{1, 2}}}, {"a: {b: c}\n", - &struct{A map[string]string "a/f"}{map[string]string{"b": "c"}}}, + &struct { + A map[string]string "a/f" + }{map[string]string{"b": "c"}}}, {"a: {b: c}\n", - &struct{A struct{B string} "a/f"}{struct{B string}{"c"}}}, + &struct { + A struct { + B string + } "a/f" + }{struct{ B string }{"c"}}}, } @@ -84,9 +115,12 @@ // } //} -var marshalTaggedIfaceTest interface{} = &struct{A string}{"B"} +var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"} -var getterTests = []struct{data, tag string; value interface{}}{ +var getterTests = []struct { + data, tag string + value interface{} +}{ {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}}, {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}}, {"_: 10\n", "", 10}, @@ -104,7 +138,7 @@ } type typeWithGetter struct { - tag string + tag string value interface{} }
diff --git a/goyaml.go b/goyaml.go index fc1219c..60d578e 100644 --- a/goyaml.go +++ b/goyaml.go
@@ -130,15 +130,15 @@ // The code in this section was copied from gobson. type structFields struct { - Map map[string]fieldInfo + Map map[string]fieldInfo List []fieldInfo } type fieldInfo struct { - Key string - Num int + Key string + Num int Conditional bool - Flow bool + Flow bool } var fieldMap = make(map[string]*structFields)
diff --git a/resolve.go b/resolve.go index 9af47c9..45e6955 100644 --- a/resolve.go +++ b/resolve.go
@@ -12,7 +12,7 @@ type resolveMapItem struct { value interface{} - tag string + tag string } var resolveTable = make([]byte, 256) @@ -32,7 +32,11 @@ t[int('.')] = '.' // Float (potentially in map) t[int('<')] = '<' // Merge - var resolveMapList = []struct{v interface{}; tag string; l []string} { + var resolveMapList = []struct { + v interface{} + tag string + l []string + }{ {true, "!!bool", []string{"y", "Y", "yes", "Yes", "YES"}}, {true, "!!bool", []string{"true", "True", "TRUE"}}, {true, "!!bool", []string{"on", "On", "ON"}}, @@ -104,7 +108,7 @@ case '.': // Not in the map, so maybe a normal float. - floatv, err := strconv.Atof(in) + floatv, err := strconv.Atof64(in) if err == nil { return "!!float", floatv } @@ -126,7 +130,7 @@ return "!!int", intv } } - floatv, err := strconv.Atof(in) + floatv, err := strconv.Atof64(in) if err == nil { return "!!float", floatv } @@ -148,7 +152,7 @@ default: panic("resolveTable item not yet handled: " + - string([]byte{c}) + " (with " + in +")") + string([]byte{c}) + " (with " + in + ")") } return "!!str", in }
diff --git a/suite_test.go b/suite_test.go index fbb8919..be26fc2 100644 --- a/suite_test.go +++ b/suite_test.go
@@ -2,7 +2,7 @@ import ( - . "gocheck" + . "gocheck" "testing" )