aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/context.go
blob: 3e484f34df0700585a41d61027abef99090278bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"log"
	"time"
)

type Context struct {
	protocol *Protocol
	out      *bytes.Buffer
}

func newContext() *Context {
	return &Context{
		out: bytes.NewBuffer([]byte{}),
	}
}

// Putln calls put and adds a new line to the end of 'format'.
func (c *Context) Putln(format string, v ...interface{}) {
	c.Put(format+"\n", v...)
}

// Put is a short alias to write to 'out'.
func (c *Context) Put(format string, v ...interface{}) {
	_, err := c.out.WriteString(fmt.Sprintf(format, v...))
	if err != nil {
		log.Fatalf("There was an error writing to context buffer: %s", err)
	}
}

// Morph is the big daddy of them all. It takes in an XML byte slice,
// parse it, transforms the XML types into more usable types,
// and writes Go code to the 'out' buffer.
func (c *Context) Morph(xmlBytes []byte) {
	parsedXml := &XML{}
	err := xml.Unmarshal(xmlBytes, parsedXml)
	if err != nil {
		log.Fatal(err)
	}

	// Parse all imports
	parsedXml.Imports.Eval()

	// Translate XML types to nice types
	c.protocol = parsedXml.Translate()

	// Start with Go header.
	c.Putln("package xgb")
	c.Putln("")
	c.Putln("/*")
	c.Putln("\tThis file was generated by %s.xml on %s.",
		c.protocol.Name, time.Now().Format("Jan 2 2006 3:04:05pm MST"))
	c.Putln("\tThis file is automatically generated. Edit at your peril!")
	c.Putln("*/")
	c.Putln("")

	// Write imports in comments
	if len(c.protocol.Imports) > 0 {
		c.Putln("// Imports are not necessary for XGB because everything is ")
		c.Putln("// in one package. They are still listed here for reference.")
		for _, imp := range c.protocol.Imports {
			c.Putln("// import \"%s\"", imp.Name)
		}
		c.Putln("")
	}

	// Now write Go source code
	for _, typ := range c.protocol.Types {
		typ.Define(c)
	}
	for _, req := range c.protocol.Requests {
		req.Define(c)
	}
}