-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (99 loc) · 1.88 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
)
type Property struct {
Description string
Name string
Type string
IsArray bool
}
type DataStructure struct {
Name string
Properties []*Property
}
type MetaData struct {
Key string
Value string
}
type Document struct {
MetaData []*MetaData
DataStructures []*DataStructure
}
func NewDoc() *Document {
return &Document{
MetaData: make([]*MetaData, 0, 10),
DataStructures: make([]*DataStructure, 0, 10),
}
}
func main() {
var filename string
var pkgname string
flag.StringVar(&filename, "input", "", "Input filename.")
flag.StringVar(&pkgname, "package", "", "Package name.")
flag.Parse()
if filename == "" || pkgname == "" {
flag.Usage()
os.Exit(1)
}
r, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
b, err := ioutil.ReadAll(r)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
l := New(filename, string(b))
go func() {
l.Run()
}()
doc := NewDoc()
var md *MetaData
var model *DataStructure
var prop *Property
for item := range l.Items {
switch item.Type {
case ItemError:
fmt.Printf("%v\n", item.Value)
os.Exit(1)
case ItemMetaKey:
md = &MetaData{}
md.Key = item.Value
continue
case ItemMetaValue:
md.Value = item.Value
doc.MetaData = append(doc.MetaData, md)
md = nil
continue
case ItemModel:
model = &DataStructure{}
doc.DataStructures = append(doc.DataStructures, model)
model.Name = item.Value
continue
case ItemPropertyName:
prop = &Property{}
model.Properties = append(model.Properties, prop)
prop.Name = item.Value
continue
case ItemPropertyType:
prop.Type = item.Value
prop.IsArray = false
continue
case ItemPropertyArrayType:
prop.Type = item.Value
prop.IsArray = true
continue
}
}
w := &GoWriter{
os.Stdout,
pkgname,
}
w.WriteDoc(doc)
}