-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_doctype.go
128 lines (113 loc) · 2.63 KB
/
make_doctype.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//go:build codegen
// +build codegen
// This program generates doctype.go.
package main
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/coding-socks/ebml/schema"
"golang.org/x/tools/imports"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var header = []byte(`// Code generated by go run make_doctype.go. DO NOT EDIT.
package ebml
import (
_ "embed"
"github.com/coding-socks/ebml/schema"
)
`)
func main() {
filename := "doctype.go"
buf := bytes.NewBuffer(header)
gen(buf)
out, err := imports.Process(filename, buf.Bytes(), nil)
if err != nil {
err = ioutil.WriteFile(filename, buf.Bytes(), 0666)
if err != nil {
log.Fatal(err)
}
log.Fatal(err)
}
err = ioutil.WriteFile(filename, out, 0666)
if err != nil {
log.Fatal(err)
}
}
func gen(w io.Writer) {
root := schema.NewTreeNode(schema.Element{
Type: schema.TypeMaster,
Name: "Document",
})
for _, fp := range []string{filepath.Join(".", "ebml.xml")} {
var s schema.Schema
func() {
f, err := os.Open(fp)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := xml.NewDecoder(f).Decode(&s); err != nil {
log.Fatal(err)
}
}()
for _, el := range s.Elements {
p := strings.Split(el.Path, `\`)[1:]
branch := root
lastIndex := len(p) - 1
for _, s := range p[:lastIndex] {
node := branch.Get(s)
if node == nil {
node = schema.NewTreeNode(el)
branch.Put(s, node)
}
branch = node
}
branch.Put(p[lastIndex], schema.NewTreeNode(el))
}
}
fmt.Fprint(w, "//go:embed ebml.xml\n")
fmt.Fprint(w, "var schemaDefinition []byte\n")
fmt.Fprint(w, "\nvar (")
root.VisitAll(func(node *schema.TreeNode) {
writeID(w, node)
})
fmt.Fprint(w, ")\n")
root.VisitAll(func(node *schema.TreeNode) {
writeStruct(w, node)
})
}
func writeID(w io.Writer, node *schema.TreeNode) {
fmt.Fprintf(w, "\n\tID%s schema.ElementID = %s", strings.ReplaceAll(node.El.Name, "-", ""), node.El.ID)
if node.El.Type != schema.TypeMaster {
return
}
node.VisitAll(func(n *schema.TreeNode) {
writeID(w, n)
})
}
func writeStruct(w io.Writer, node *schema.TreeNode) {
if node.El.Type != schema.TypeMaster {
return
}
fmt.Fprintf(w, "type %s struct {", node.El.Name)
if node.El.Recursive {
fmt.Fprintf(w, "\n\t%[1]s *%[1]s", node.El.Name)
}
node.VisitAll(func(n *schema.TreeNode) {
if n.El.MaxOccurs.Unbounded() || n.El.MaxOccurs.Val() > 1 {
fmt.Fprintf(w, "\n\t%s []%s", n.El.Name, schema.ResolveGoType(n.El.Type, n.El.Name))
return
}
fmt.Fprintf(w, "\n\t%s %s", n.El.Name, schema.ResolveGoType(n.El.Type, n.El.Name))
})
fmt.Fprint(w, "\n}\n\n")
node.VisitAll(func(n *schema.TreeNode) {
writeStruct(w, n)
})
}