diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-08-01 16:33:46 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-08-02 16:57:31 +0200 |
| commit | bdc8436c58b3952b79bdb590d85128f2a48fbc97 (patch) | |
| tree | 0401bed795e1eb81d63585d86b0684118d9ba120 /pkg/serializer | |
| parent | b24fab5550f14a34fa21553d8743eda7b194638c (diff) | |
pkg/serializer: refactor
Factor out serialization of pointers, slices and
structs into separate functions.
Update #538
Diffstat (limited to 'pkg/serializer')
| -rw-r--r-- | pkg/serializer/serializer.go | 124 |
1 files changed, 68 insertions, 56 deletions
diff --git a/pkg/serializer/serializer.go b/pkg/serializer/serializer.go index 3054bd3a4..473b6fa74 100644 --- a/pkg/serializer/serializer.go +++ b/pkg/serializer/serializer.go @@ -33,18 +33,7 @@ type writer struct { func (w *writer) do(v reflect.Value, sliceElem bool) { switch v.Kind() { case reflect.Ptr: - if v.IsNil() { - w.string("nil") - return - } - if !sliceElem { - w.byte('&') - } - if v.Elem().Kind() != reflect.Struct { - panic(fmt.Sprintf("only pointers to structs are supported, got %v", - v.Type().Name())) - } - w.do(v.Elem(), sliceElem) + w.doPtr(v, sliceElem) case reflect.Interface: if v.IsNil() { w.string("nil") @@ -52,51 +41,9 @@ func (w *writer) do(v reflect.Value, sliceElem bool) { w.do(v.Elem(), false) } case reflect.Slice: - if v.IsNil() || v.Len() == 0 { - w.string("nil") - } else { - w.typ(v.Type()) - sub := v.Type().Elem().Kind() - if sub == reflect.Ptr || sub == reflect.Interface || sub == reflect.Struct { - // Elem per-line. - w.string("{\n") - for i := 0; i < v.Len(); i++ { - w.do(v.Index(i), true) - w.string(",\n") - } - w.byte('}') - } else { - // All on one line. - w.byte('{') - for i := 0; i < v.Len(); i++ { - if i > 0 { - w.byte(',') - } - w.do(v.Index(i), true) - } - w.byte('}') - } - } + w.doSlice(v) case reflect.Struct: - if !sliceElem { - w.string(v.Type().Name()) - } - w.byte('{') - needComma := false - for i := 0; i < v.NumField(); i++ { - f := v.Field(i) - if isDefaultValue(f) { - continue - } - if needComma { - w.byte(',') - } - w.string(v.Type().Field(i).Name) - w.byte(':') - w.do(f, false) - needComma = true - } - w.byte('}') + w.doStruct(v, sliceElem) case reflect.Bool: if v.Bool() { w.string("true") @@ -116,6 +63,71 @@ func (w *writer) do(v reflect.Value, sliceElem bool) { } } +func (w *writer) doPtr(v reflect.Value, sliceElem bool) { + if v.IsNil() { + w.string("nil") + return + } + if !sliceElem { + w.byte('&') + } + if v.Elem().Kind() != reflect.Struct { + panic(fmt.Sprintf("only pointers to structs are supported, got %v", + v.Type().Name())) + } + w.do(v.Elem(), sliceElem) +} + +func (w *writer) doSlice(v reflect.Value) { + if v.IsNil() || v.Len() == 0 { + w.string("nil") + return + } + w.typ(v.Type()) + sub := v.Type().Elem().Kind() + if sub == reflect.Ptr || sub == reflect.Interface || sub == reflect.Struct { + // Elem per-line. + w.string("{\n") + for i := 0; i < v.Len(); i++ { + w.do(v.Index(i), true) + w.string(",\n") + } + w.byte('}') + return + } + // All on one line. + w.byte('{') + for i := 0; i < v.Len(); i++ { + if i > 0 { + w.byte(',') + } + w.do(v.Index(i), true) + } + w.byte('}') +} + +func (w *writer) doStruct(v reflect.Value, sliceElem bool) { + if !sliceElem { + w.string(v.Type().Name()) + } + w.byte('{') + needComma := false + for i := 0; i < v.NumField(); i++ { + f := v.Field(i) + if isDefaultValue(f) { + continue + } + if needComma { + w.byte(',') + } + w.string(v.Type().Field(i).Name) + w.byte(':') + w.do(f, false) + needComma = true + } + w.byte('}') +} + func (w *writer) typ(t reflect.Type) { switch t.Kind() { case reflect.Ptr: |
