Remove the channel from struct reader/builder

Instead use a slice of Value
This commit is contained in:
Erik Arvidsson
2015-11-05 16:34:03 -05:00
parent 329e13e0ae
commit cc89fb2d90
5 changed files with 93 additions and 93 deletions

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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++
}
}

View File

@@ -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)
}

View File

@@ -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
}