From 7ad95dd0798a40da1ccdff6dff35fd177b5edf40 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Wed, 24 Jun 2015 11:29:02 +0100 Subject: [PATCH] Fix omitempty support for floats. --- encode_test.go | 8 +++++++- yaml.go | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/encode_test.go b/encode_test.go index 1b35452..84099bd 100644 --- a/encode_test.go +++ b/encode_test.go @@ -190,6 +190,12 @@ var marshalTests = []struct { A struct{ X, y int } "a,omitempty,flow" }{struct{ X, y int }{0, 1}}, "{}\n", + }, { + &struct { + A float64 "a,omitempty" + B float64 "b,omitempty" + }{1, 0}, + "a: 1\n", }, // Flow flag @@ -340,7 +346,7 @@ var marshalErrorTests = []struct { panic: `Duplicated key 'b' in struct struct \{ B int; .*`, }, { value: &struct { - A int + A int B map[string]int ",inline" }{1, map[string]int{"a": 2}}, panic: `Can't have key "a" in inlined map; conflicts with struct field`, diff --git a/yaml.go b/yaml.go index af4df8a..d133edf 100644 --- a/yaml.go +++ b/yaml.go @@ -324,13 +324,15 @@ func isZero(v reflect.Value) bool { return v.Len() == 0 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return v.Int() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return v.Uint() == 0 case reflect.Bool: return !v.Bool() case reflect.Struct: vt := v.Type() - for i := v.NumField()-1; i >= 0; i-- { + for i := v.NumField() - 1; i >= 0; i-- { if vt.Field(i).PkgPath != "" { continue // Private field }