Skip nil values when marshaling.
This commit is contained in:
parent
b1f8c9d7a7
commit
f81cd34e34
1 changed files with 16 additions and 0 deletions
16
struct.go
16
struct.go
|
@ -355,6 +355,9 @@ func writeSVList(w io.Writer, svList StringValueArray) (err os.Error) {
|
||||||
sort.Sort(svList)
|
sort.Sort(svList)
|
||||||
|
|
||||||
for _, sv := range (svList) {
|
for _, sv := range (svList) {
|
||||||
|
if isValueNil(sv.value) {
|
||||||
|
continue // Skip null values
|
||||||
|
}
|
||||||
s := sv.key
|
s := sv.key
|
||||||
_, err = fmt.Fprintf(w, "%d:%s", len(s), s)
|
_, err = fmt.Fprintf(w, "%d:%s", len(s), s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -468,6 +471,19 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValueNil(val reflect.Value) bool {
|
||||||
|
if val == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
switch v := val.(type) {
|
||||||
|
case *reflect.InterfaceValue:
|
||||||
|
return isValueNil(v.Elem())
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func Marshal(w io.Writer, val interface{}) os.Error {
|
func Marshal(w io.Writer, val interface{}) os.Error {
|
||||||
return writeValue(w, reflect.NewValue(val))
|
return writeValue(w, reflect.NewValue(val))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue