holy toldeo... things might actually be working
This commit is contained in:
@@ -34,9 +34,6 @@ func (u *Union) Define(c *Context) {
|
||||
|
||||
// Write function that writes a list of this union.
|
||||
u.WriteList(c)
|
||||
|
||||
// Write function that computes the size of a list of these unions.
|
||||
u.WriteListSize(c)
|
||||
}
|
||||
|
||||
func (u *Union) New(c *Context) {
|
||||
@@ -49,7 +46,7 @@ func (u *Union) New(c *Context) {
|
||||
c.Putln("var b int")
|
||||
c.Putln("buf := make([]byte, %s)", u.Size())
|
||||
c.Putln("")
|
||||
field.Write(c)
|
||||
field.Write(c, "")
|
||||
c.Putln("")
|
||||
c.Putln("// Create the Union type")
|
||||
c.Putln("v := %s{}", u.SrcName())
|
||||
@@ -58,7 +55,7 @@ func (u *Union) New(c *Context) {
|
||||
c.Putln("")
|
||||
for _, field2 := range u.Fields {
|
||||
c.Putln("b = 0 // always read the same bytes")
|
||||
field2.Read(c)
|
||||
field2.Read(c, "v.")
|
||||
c.Putln("")
|
||||
}
|
||||
c.Putln("return v")
|
||||
@@ -74,7 +71,7 @@ func (u *Union) Read(c *Context) {
|
||||
c.Putln("")
|
||||
for _, field := range u.Fields {
|
||||
c.Putln("b = 0 // re-read the same bytes")
|
||||
field.Read(c)
|
||||
field.Read(c, "v.")
|
||||
c.Putln("")
|
||||
}
|
||||
c.Putln("return %s", u.Size())
|
||||
@@ -106,10 +103,10 @@ func (u *Union) Write(c *Context) {
|
||||
c.Putln("// Each field in a union must contain the same data.")
|
||||
c.Putln("// So simply pick the first field and write that to the wire.")
|
||||
c.Putln("func (v %s) Bytes() []byte {", u.SrcName())
|
||||
c.Putln("buf := make([]byte, %s)", u.Size().Reduce("v.", ""))
|
||||
c.Putln("buf := make([]byte, %s)", u.Size().Reduce("v."))
|
||||
c.Putln("b := 0")
|
||||
c.Putln("")
|
||||
u.Fields[0].Write(c)
|
||||
u.Fields[0].Write(c, "v.")
|
||||
c.Putln("return buf")
|
||||
c.Putln("}")
|
||||
c.Putln("")
|
||||
@@ -123,7 +120,7 @@ func (u *Union) WriteList(c *Context) {
|
||||
c.Putln("var unionBytes []byte")
|
||||
c.Putln("for _, item := range list {")
|
||||
c.Putln("unionBytes = item.Bytes()")
|
||||
c.Putln("copy(buf[b:], len(unionBytes))")
|
||||
c.Putln("copy(buf[b:], unionBytes)")
|
||||
c.Putln("b += pad(len(unionBytes))")
|
||||
c.Putln("}")
|
||||
c.Putln("return b")
|
||||
@@ -136,7 +133,7 @@ func (u *Union) WriteListSize(c *Context) {
|
||||
c.Putln("func %sListSize(list []%s) int {", u.SrcName(), u.SrcName())
|
||||
c.Putln("size := 0")
|
||||
c.Putln("for _, item := range list {")
|
||||
c.Putln("size += %s", u.Size().Reduce("item.", ""))
|
||||
c.Putln("size += %s", u.Size().Reduce("item."))
|
||||
c.Putln("}")
|
||||
c.Putln("return size")
|
||||
c.Putln("}")
|
||||
|
||||
Reference in New Issue
Block a user