-
Notifications
You must be signed in to change notification settings - Fork 18
/
gen.go
131 lines (104 loc) · 2.4 KB
/
gen.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
129
130
131
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)
func run() error {
tfb, err := ioutil.ReadFile("./node/config/types.go")
if err != nil {
return err
}
// could use the ast lib, but this is simpler
type st int
const (
stGlobal st = iota // looking for typedef
stType st = iota // in typedef
)
lines := strings.Split(string(tfb), "\n")
state := stGlobal
type field struct {
Name string
Type string
Comment string
}
var currentType string
var currentComment []string
out := map[string][]field{}
for l := range lines {
line := strings.TrimSpace(lines[l])
switch state {
case stGlobal:
if strings.HasPrefix(line, "type ") {
currentType = line
currentType = strings.TrimPrefix(currentType, "type")
currentType = strings.TrimSuffix(currentType, "{")
currentType = strings.TrimSpace(currentType)
currentType = strings.TrimSuffix(currentType, "struct")
currentType = strings.TrimSpace(currentType)
currentComment = nil
state = stType
continue
}
case stType:
if strings.HasPrefix(line, "// ") {
cline := strings.TrimSpace(strings.TrimPrefix(line, "//"))
currentComment = append(currentComment, cline)
continue
}
comment := currentComment
currentComment = nil
if strings.HasPrefix(line, "}") {
state = stGlobal
continue
}
f := strings.Fields(line)
if len(f) < 2 { // empty or embedded struct
continue
}
name := f[0]
typ := f[1]
out[currentType] = append(out[currentType], field{
Name: name,
Type: typ,
Comment: strings.Join(comment, "\n"),
})
}
}
var outt []string
for t := range out {
outt = append(outt, t)
}
sort.Strings(outt)
fmt.Print(`// Code generated by github.com/github.com/Filecoin-Titan/titan/node/config/cfgdocgen. DO NOT EDIT.
package config
type DocField struct {
Name string
Type string
Comment string
}
var Doc = map[string][]DocField{
`)
for _, typeName := range outt {
typ := out[typeName]
fmt.Printf("\t\"%s\": []DocField{\n", typeName)
for _, f := range typ {
fmt.Println("\t\t{")
fmt.Printf("\t\t\tName: \"%s\",\n", f.Name)
fmt.Printf("\t\t\tType: \"%s\",\n\n", f.Type)
fmt.Printf("\t\t\tComment: `%s`,\n", f.Comment)
fmt.Println("\t\t},")
}
fmt.Printf("\t},\n")
}
fmt.Println(`}`)
return nil
}
func main() {
if err := run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}