-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_problems.go
82 lines (62 loc) · 1.66 KB
/
make_problems.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
// SPDX-FileCopyrightText: 2018-2024 caixw
//
// SPDX-License-Identifier: MIT
//go:build ignore
package main
import (
"io/fs"
"strconv"
"github.com/issue9/errwrap"
"github.com/issue9/source/codegen"
"github.com/issue9/web/internal/status"
)
const (
filename = "problems.go"
pkgName = "web"
)
func main() {
buf := &errwrap.Buffer{}
buf.WString(status.FileHeader).
WString("package ").WString(pkgName).WString("\n\n").
WString("import \"net/http\"\n\n")
kvs, err := status.Get()
if err != nil {
panic(err)
}
makeID(buf, kvs)
makeIDs(buf, kvs)
makeInitProblems(buf, kvs)
if buf.Err != nil {
panic(buf.Err)
}
if err = codegen.Dump(filename, buf.Bytes(), fs.ModePerm); err != nil {
panic(err)
}
}
func makeID(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("const(\n")
buf.WString(`ProblemAboutBlank = "about:blank"`).WString("\n\n")
for _, item := range kvs {
buf.Printf("%s=\"%s\"\n", item.ID(), strconv.Itoa(item.Value))
}
buf.WString(")\n\n")
}
func makeIDs(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("var problemsID=map[int]string{\n")
for _, item := range kvs {
buf.Printf("%s:%s,\n", "http."+item.Name, item.ID())
}
buf.WString("}\n\n")
}
func makeInitProblems(buf *errwrap.Buffer, kvs []status.Pair) {
buf.WString("func initProblems(p*Problems){")
for _, item := range kvs {
status := "http." + item.Name
title := "problem." + strconv.Itoa(item.Value)
detail := title + ".detail"
title = "StringPhrase(\"" + title + "\")"
detail = "StringPhrase(\"" + detail + "\")"
buf.Printf(`p.Add(%s,&LocaleProblem{ID:%s,Title:%s,Detail:%s})`, status, item.ID(), title, detail).WByte('\n')
}
buf.WString("}\n\n")
}