From 83a71d464887f4b6cf9124d2f8c565e6f17f2bd3 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Tue, 1 May 2012 01:08:03 -0400 Subject: unions, events and errors... oh my --- nexgb/xgbgen/go_error.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'nexgb/xgbgen/go_error.go') diff --git a/nexgb/xgbgen/go_error.go b/nexgb/xgbgen/go_error.go index 0cb77cc..493a8b9 100644 --- a/nexgb/xgbgen/go_error.go +++ b/nexgb/xgbgen/go_error.go @@ -22,18 +22,36 @@ func (e *ErrorCopy) Define(c *Context) { c.Putln("") c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType()) c.Putln("") + + // Read defines a function that transforms a byte slice into this + // error struct. + e.Read(c) + + // Write defines a function that transoforms this error struct into + // a byte slice. + e.Write(c) + + // Makes sure that this error type is an Error interface. + c.Putln("func (err %s) ImplementsError() { }", e.ErrType()) + c.Putln("") + + // Let's the XGB know how to read this error. + c.Putln("func init() {") + c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName()) + c.Putln("}") + c.Putln("") +} + +func (e *ErrorCopy) Read(c *Context) { c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType()) c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName()) c.Putln("}") c.Putln("") - c.Putln("func (err %s) ImplementsError() { }", e.ErrType()) - c.Putln("") +} + +func (e *ErrorCopy) Write(c *Context) { c.Putln("func (err %s) Bytes() []byte {", e.ErrType()) c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType()) c.Putln("}") c.Putln("") - c.Putln("func init() {") - c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName()) - c.Putln("}") - c.Putln("") } -- cgit v1.2.3-54-g00ecf