This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
139 lines (125 loc) · 3.75 KB
/
generate.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
132
133
134
135
136
137
138
139
package log
import (
"bytes"
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"os"
"strings"
"text/template"
)
var messageFileTemplate = `# Message / error codes
| Code | Explanation |
|------|-------------|
{{range $key,$val := . -}}
| ` + "`{{ $key }}`" + ` | {{ $val }} |
{{end}}
`
// GetMessageCodes parses the specified go source file and returns a key-value mapping of message codes and their
// associated description or an error.
func GetMessageCodes(filename string) (map[string]string, error) {
result := map[string]string{}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return result, fmt.Errorf("failed to parse file %s (%w)", filename, err)
}
constDocs, err := extractConstDocs(filename, fset, f)
if err != nil {
return result, err
}
for _, decl := range f.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if genDecl.Tok == token.CONST {
for _, spec := range genDecl.Specs {
if vSpec, ok := spec.(*ast.ValueSpec); ok {
for _, name := range vSpec.Names {
for _, val := range vSpec.Values {
d, ok := constDocs[name.Name]
if !ok || d == "" {
return result, fmt.Errorf("constant %s is not documented", name.Name)
}
basicVal, ok := val.(*ast.BasicLit)
if !ok {
return result, fmt.Errorf(
"the value of constant %s should be a basic string",
name.Name,
)
}
docParts := strings.Split(strings.TrimSpace(d), "\n")
resultingDocParts := []string{}
for _, part := range docParts {
if !strings.HasPrefix(part, "goland:") {
resultingDocParts = append(resultingDocParts, strings.TrimSpace(part))
}
}
result[strings.Trim(basicVal.Value, `"`)] = strings.Join(resultingDocParts, " ")
}
}
}
}
}
}
}
return result, nil
}
func extractConstDocs(
filename string,
fset *token.FileSet,
f *ast.File,
) (map[string]string, error) {
p, err := doc.NewFromFiles(fset, []*ast.File{f}, "")
if err != nil {
return nil, fmt.Errorf("failed to parse docs from file %s (%w)", filename, err)
}
constDocs := map[string]string{}
for _, c := range p.Consts {
for _, name := range c.Names {
constDocs[name] = c.Doc
}
}
return constDocs, nil
}
// GenerateMessageCodeFiles generates the contents of the CODES.md file and returns them.
func GenerateMessageCodesFile(filename string) (string, error) {
codes, err := GetMessageCodes(filename)
if err != nil {
return "", err
}
tpl, err := template.New("CODES.md.tpl").Parse(messageFileTemplate)
if err != nil {
return "", fmt.Errorf("bug: failed to parse template (%w)", err)
}
wr := &bytes.Buffer{}
if err := tpl.Execute(wr, codes); err != nil {
return "", fmt.Errorf("failed to render codes template (%w)", err)
}
return wr.String(), nil
}
// WriteMessageCodesFile generates and writes the CODES.md file
func WriteMessageCodesFile(sourceFile string, destinationFile string) error {
data, err := GenerateMessageCodesFile(sourceFile)
if err != nil {
return err
}
fh, err := os.Create(destinationFile)
if err != nil {
return fmt.Errorf("failed to open destination file %s (%w)", destinationFile, err)
}
if _, err = fh.Write([]byte(data)); err != nil {
_ = fh.Close()
return fmt.Errorf("failed to write to destination file %s (%w)", destinationFile, err)
}
if err := fh.Close(); err != nil {
return fmt.Errorf("failed to close destination file %s (%w)", destinationFile, err)
}
return nil
}
// MustWriteMessageCodesFile is identical to WriteMessageCodesFile but panics on error.
func MustWriteMessageCodesFile(sourceFile string, destinationFile string) {
if err := WriteMessageCodesFile(sourceFile, destinationFile); err != nil {
panic(err)
}
}