From cc89fb2d9059dbddea161a434c1751fe11df7bd8 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Thu, 5 Nov 2015 16:34:03 -0500 Subject: [PATCH] Remove the channel from struct reader/builder Instead use a slice of Value --- nomdl/codegen/struct.tmpl | 54 ++++++++++++------------ types/decode_noms_value.go | 17 ++++---- types/encode_noms_value.go | 18 +++++--- types/package_registry.go | 12 +++--- types/struct.go | 85 ++++++++++++++++++-------------------- 5 files changed, 93 insertions(+), 93 deletions(-) diff --git a/nomdl/codegen/struct.tmpl b/nomdl/codegen/struct.tmpl index f94154b7d9..02c0b15862 100644 --- a/nomdl/codegen/struct.tmpl +++ b/nomdl/codegen/struct.tmpl @@ -56,35 +56,35 @@ func init() { {{$typesPackage}}RegisterStruct(__typeRefFor{{.Name}}, builderFor{{.Name}}, readerFor{{.Name}}) } -func builderFor{{.Name}}() chan {{$typesPackage}}Value { - c := make(chan {{$typesPackage}}Value) - go func() { - s := {{.Name}}{ref: &ref.Ref{}}{{range .Fields}}{{if .Optional}} - s.__optional{{.Name}} = bool((<-c).({{$typesPackage}}Bool)) - if s.__optional{{.Name}} { - s._{{.Name}} = {{valueToUser "(<-c)" .T}} - }{{else}} - s._{{.Name}} = {{valueToUser "(<-c)" .T}}{{end}}{{end}} - {{if .HasUnion}}s.__unionIndex = uint32((<-c).({{$typesPackage}}UInt32)) - s.__unionValue = <-c - {{end}}c <- s - }() - return c +func builderFor{{.Name}}(values []{{$typesPackage}}Value) {{$typesPackage}}Value { + i := 0 + s := {{.Name}}{ref: &ref.Ref{}}{{range .Fields}}{{if .Optional}} + s.__optional{{.Name}} = bool(values[i].({{$typesPackage}}Bool)) + i++ + if s.__optional{{.Name}} { + s._{{.Name}} = {{valueToUser "values[i]" .T}} + i++ + }{{else}} + s._{{.Name}} = {{valueToUser "values[i]" .T}} + i++{{end}}{{end}} + {{if .HasUnion}}s.__unionIndex = uint32(values[i].({{$typesPackage}}UInt32)) + i++ + s.__unionValue = values[i] + i++ + {{end}}return s } -func readerFor{{.Name}}(v {{$typesPackage}}Value) chan {{$typesPackage}}Value { - c := make(chan {{$typesPackage}}Value) - go func() { - s := v.({{.Name}}){{range .Fields}}{{if .Optional}} - c <- {{$typesPackage}}Bool(s.__optional{{.Name}}) - if s.__optional{{.Name}} { - c <- {{userToValue (printf "s._%s" .Name) .T}} - }{{else}} - c <- {{userToValue (printf "s._%s" .Name) .T}}{{end}}{{end}} - {{if .HasUnion}}c <- {{$typesPackage}}UInt32(s.__unionIndex) - c <- s.__unionValue - {{end}} }() - return c +func readerFor{{.Name}}(v {{$typesPackage}}Value) []{{$typesPackage}}Value { + values := []{{$typesPackage}}Value{} + s := v.({{.Name}}){{range .Fields}}{{if .Optional}} + values = append(values, {{$typesPackage}}Bool(s.__optional{{.Name}})) + if s.__optional{{.Name}} { + values = append(values, {{userToValue (printf "s._%s" .Name) .T}}) + }{{else}} + values = append(values, {{userToValue (printf "s._%s" .Name) .T}}){{end}}{{end}} + {{if .HasUnion}}values = append(values, {{$typesPackage}}UInt32(s.__unionIndex)) + values = append(values, s.__unionValue) + {{end}}return values } func (s {{.Name}}) Equals(other {{$typesPackage}}Value) bool { diff --git a/types/decode_noms_value.go b/types/decode_noms_value.go index 3fd5c49537..75764525e7 100644 --- a/types/decode_noms_value.go +++ b/types/decode_noms_value.go @@ -333,27 +333,24 @@ func fixupTypeRef(tr TypeRef, pkg *Package) TypeRef { func (r *jsonArrayReader) readStruct(typeDef, typeRef TypeRef, pkg *Package) Value { // We've read `[StructKind, sha1, name` at this point - - typeRef = fixupTypeRef(typeRef, pkg) - c := structBuilderForTypeRef(typeRef, typeDef) - + values := []Value{} desc := typeDef.Desc.(StructDesc) for _, f := range desc.Fields { if f.Optional { b := r.read().(bool) - c <- Bool(b) + values = append(values, Bool(b)) if b { - c <- r.readValueWithoutTag(f.T, pkg) + values = append(values, r.readValueWithoutTag(f.T, pkg)) } } else { - c <- r.readValueWithoutTag(f.T, pkg) + values = append(values, r.readValueWithoutTag(f.T, pkg)) } } if len(desc.Union) > 0 { unionIndex := uint32(r.read().(float64)) - c <- UInt32(unionIndex) - c <- r.readValueWithoutTag(desc.Union[unionIndex].T, pkg) + values = append(values, UInt32(unionIndex), r.readValueWithoutTag(desc.Union[unionIndex].T, pkg)) } - return (<-c).(Value) + typeRef = fixupTypeRef(typeRef, pkg) + return structBuilderForTypeRef(values, typeRef, typeDef) } diff --git a/types/encode_noms_value.go b/types/encode_noms_value.go index c78f933edc..14d2d536b1 100644 --- a/types/encode_noms_value.go +++ b/types/encode_noms_value.go @@ -218,24 +218,30 @@ func (w *jsonArrayWriter) writeBlob(b Blob) { func (w *jsonArrayWriter) writeStruct(v Value, typeRef, typeDef TypeRef, pkg *Package) { typeRef = fixupTypeRef(typeRef, pkg) - c := structReaderForTypeRef(v, typeRef, typeDef) + i := 0 + values := structReaderForTypeRef(v, typeRef, typeDef) desc := typeDef.Desc.(StructDesc) for _, f := range desc.Fields { if f.Optional { - ok := bool((<-c).(Bool)) + ok := bool(values[i].(Bool)) + i++ w.write(ok) if ok { - w.writeValue(<-c, f.T, pkg) + w.writeValue(values[i], f.T, pkg) + i++ } } else { - w.writeValue(<-c, f.T, pkg) + w.writeValue(values[i], f.T, pkg) + i++ } } if len(desc.Union) > 0 { - unionIndex := uint32((<-c).(UInt32)) + unionIndex := uint32(values[i].(UInt32)) + i++ w.write(unionIndex) - w.writeValue(<-c, desc.Union[unionIndex].T, pkg) + w.writeValue(values[i], desc.Union[unionIndex].T, pkg) + i++ } } diff --git a/types/package_registry.go b/types/package_registry.go index 9cd80ae8a5..ef5c53cac8 100644 --- a/types/package_registry.go +++ b/types/package_registry.go @@ -9,8 +9,8 @@ import ( type enumBuilderFunc func(v uint32) Value type enumReaderFunc func(v Value) uint32 type refBuilderFunc func(target ref.Ref) Value -type structBuilderFunc func() chan Value -type structReaderFunc func(v Value) chan Value +type structBuilderFunc func(values []Value) Value +type structReaderFunc func(v Value) []Value type valueBuilderFunc func(v Value) Value type valueReaderFunc func(v Value) Value @@ -61,14 +61,14 @@ func RegisterStruct(t TypeRef, bf structBuilderFunc, rf structReaderFunc) { structFuncMap[t.Ref()] = structFuncs{bf, rf} } -func structBuilderForTypeRef(typeRef, typeDef TypeRef) chan Value { +func structBuilderForTypeRef(values []Value, typeRef, typeDef TypeRef) Value { if s, ok := structFuncMap[typeRef.Ref()]; ok { - return s.builder() + return s.builder(values) } - return structBuilder(typeRef, typeDef) + return structBuilder(values, typeRef, typeDef) } -func structReaderForTypeRef(v Value, typeRef, typeDef TypeRef) chan Value { +func structReaderForTypeRef(v Value, typeRef, typeDef TypeRef) []Value { if s, ok := structFuncMap[typeRef.Ref()]; ok { return s.reader(v) } diff --git a/types/struct.go b/types/struct.go index 2c18724c7c..39f0cfe8ad 100644 --- a/types/struct.go +++ b/types/struct.go @@ -155,58 +155,55 @@ func (s Struct) findField(n string) (Field, int32, bool) { return Field{}, -1, false } -func structBuilder(typeRef, typeDef TypeRef) chan Value { - c := make(chan Value) +func structBuilder(values []Value, typeRef, typeDef TypeRef) Value { + i := 0 + desc := typeDef.Desc.(StructDesc) + data := structData{} + unionIndex := uint32(0) + var unionValue Value - go func() { - desc := typeDef.Desc.(StructDesc) - data := structData{} - unionIndex := uint32(0) - var unionValue Value - - for _, f := range desc.Fields { - if f.Optional { - b := bool((<-c).(Bool)) - if b { - data[f.Name] = <-c - } - } else { - data[f.Name] = <-c + for _, f := range desc.Fields { + if f.Optional { + b := bool(values[i].(Bool)) + i++ + if b { + data[f.Name] = values[i] + i++ } + } else { + data[f.Name] = values[i] + i++ } - if len(desc.Union) > 0 { - unionIndex = uint32((<-c).(UInt32)) - unionValue = <-c - } + } + if len(desc.Union) > 0 { + unionIndex = uint32(values[i].(UInt32)) + i++ + unionValue = values[i] + i++ + } - c <- newStructFromData(data, unionIndex, unionValue, typeRef, typeDef) - }() - - return c + return newStructFromData(data, unionIndex, unionValue, typeRef, typeDef) } -func structReader(s Struct, typeRef, typeDef TypeRef) chan Value { - c := make(chan Value) +func structReader(s Struct, typeRef, typeDef TypeRef) []Value { + values := []Value{} - go func() { - desc := typeDef.Desc.(StructDesc) - for _, f := range desc.Fields { - v, ok := s.data[f.Name] - if f.Optional { - c <- Bool(ok) - if ok { - c <- v - } - } else { - d.Chk.True(ok) - c <- v + desc := typeDef.Desc.(StructDesc) + for _, f := range desc.Fields { + v, ok := s.data[f.Name] + if f.Optional { + values = append(values, Bool(ok)) + if ok { + values = append(values, v) } + } else { + d.Chk.True(ok) + values = append(values, v) } - if len(desc.Union) > 0 { - c <- UInt32(s.unionIndex) - c <- s.unionValue - } - }() + } + if len(desc.Union) > 0 { + values = append(values, UInt32(s.unionIndex), s.unionValue) + } - return c + return values }