mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
api-gen: clean up
This commit is contained in:
parent
9b0fae6346
commit
72173337ae
@ -10,6 +10,7 @@ import (
|
|||||||
"go/token"
|
"go/token"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -28,7 +29,6 @@ type Param struct {
|
|||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
Func string
|
Func string
|
||||||
Version []uint16
|
|
||||||
Path []string
|
Path []string
|
||||||
PathV11 []string // v11 only
|
PathV11 []string // v11 only
|
||||||
PathV12 []string // v12 only
|
PathV12 []string // v12 only
|
||||||
@ -59,62 +59,33 @@ func (g *generator) header() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *generator) genRouter(routers []Router) {
|
func (g *generator) genRouter(routers []Router) {
|
||||||
var actions []string // for onebot v12 get_supported_actions
|
|
||||||
for _, router := range routers {
|
|
||||||
if len(router.PathV12) > 0 {
|
|
||||||
actions = append(actions, router.PathV12...)
|
|
||||||
}
|
|
||||||
if len(router.Path) > 0 {
|
|
||||||
actions = append(actions, router.Path...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i := range actions {
|
|
||||||
actions[i] = `"` + actions[i] + `"`
|
|
||||||
}
|
|
||||||
|
|
||||||
g.WriteString("import (\n\n")
|
g.WriteString("import (\n\n")
|
||||||
g.WriteString("\"github.com/Mrs4s/go-cqhttp/coolq\"\n")
|
g.WriteString("\"github.com/Mrs4s/go-cqhttp/coolq\"\n")
|
||||||
g.WriteString("\"github.com/Mrs4s/go-cqhttp/global\"\n")
|
g.WriteString("\"github.com/Mrs4s/go-cqhttp/global\"\n")
|
||||||
g.WriteString("\"github.com/Mrs4s/go-cqhttp/pkg/onebot\"\n")
|
g.WriteString("\"github.com/Mrs4s/go-cqhttp/pkg/onebot\"\n")
|
||||||
g.WriteString(")\n\n")
|
g.WriteString(")\n\n")
|
||||||
g.WriteString(`func (c *Caller) call(action string, spec *onebot.Spec, p Getter) global.MSG {
|
g.WriteString(`func (c *Caller) call(action string, spec *onebot.Spec, p Getter) global.MSG {`)
|
||||||
if spec.Version == 12 {
|
genVer := func(path int) {
|
||||||
|
g.writef(`if spec.Version == %d {
|
||||||
switch action {
|
switch action {
|
||||||
`)
|
`, path)
|
||||||
for _, router := range routers {
|
for _, router := range routers {
|
||||||
g.router(router, PathV12)
|
g.router(router, path)
|
||||||
}
|
}
|
||||||
io.WriteString(g.out, `}}`)
|
g.WriteString("}}\n")
|
||||||
io.WriteString(g.out, "\n")
|
|
||||||
g.WriteString(`if spec.Version == 11 {
|
|
||||||
switch action {
|
|
||||||
`)
|
|
||||||
for _, router := range routers {
|
|
||||||
g.router(router, PathV11)
|
|
||||||
}
|
}
|
||||||
io.WriteString(g.out, `}}`)
|
genVer(PathV11)
|
||||||
io.WriteString(g.out, "\n")
|
genVer(PathV12)
|
||||||
io.WriteString(g.out, "switch action {\n")
|
// generic path
|
||||||
|
g.WriteString("switch action {\n")
|
||||||
for _, router := range routers {
|
for _, router := range routers {
|
||||||
g.router(router, PathAll)
|
g.router(router, PathAll)
|
||||||
}
|
}
|
||||||
io.WriteString(g.out, `}`)
|
g.WriteString("}\n")
|
||||||
io.WriteString(g.out, "\n")
|
g.WriteString("return coolq.Failed(404, \"API_NOT_FOUND\", \"API不存在\")}")
|
||||||
io.WriteString(g.out, "return coolq.Failed(404, \"API_NOT_FOUND\", \"API不存在\")}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *generator) router(router Router, pathVersion int) {
|
func (g *generator) router(router Router, pathVersion int) {
|
||||||
/*
|
|
||||||
checkVersion := func(v uint16) bool {
|
|
||||||
for _, ver := range router.Version {
|
|
||||||
if ver == v {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
path := router.Path
|
path := router.Path
|
||||||
if pathVersion == PathV11 {
|
if pathVersion == PathV11 {
|
||||||
path = router.PathV11
|
path = router.PathV11
|
||||||
@ -135,26 +106,17 @@ func (g *generator) router(router Router, pathVersion int) {
|
|||||||
}
|
}
|
||||||
g.WriteString(":\n")
|
g.WriteString(":\n")
|
||||||
|
|
||||||
if len(router.Version) == 1 { // 目前来说只需要判断一个版本的情况
|
|
||||||
check := make([]string, 0, len(router.Version))
|
|
||||||
for _, ver := range router.Version {
|
|
||||||
check = append(check, fmt.Sprintf("spec.Version != %v", ver))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(g.out, "if %v {\n", strings.Join(check, " && "))
|
|
||||||
fmt.Fprintf(g.out, "return coolq.Failed(405, \"VERSION_ERROR\", \"API版本不匹配\")}\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, p := range router.Params {
|
for i, p := range router.Params {
|
||||||
if p.Name == "spec" {
|
if p.Type == "*onebot.Spec" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if p.Default == "" {
|
if p.Default == "" {
|
||||||
v := "p.Get(" + strconv.Quote(p.Name) + ")"
|
v := "p.Get(" + strconv.Quote(p.Name) + ")"
|
||||||
fmt.Fprintf(g.out, "p%d := %s\n", i, conv(v, p.Type))
|
g.writef("p%d := %s\n", i, conv(v, p.Type))
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(g.out, "p%d := %s\n", i, p.Default)
|
g.writef("p%d := %s\n", i, p.Default)
|
||||||
fmt.Fprintf(g.out, "if pt := p.Get(%s); pt.Exists() {\n", strconv.Quote(p.Name))
|
g.writef("if pt := p.Get(%s); pt.Exists() {\n", strconv.Quote(p.Name))
|
||||||
fmt.Fprintf(g.out, "p%d = %s\n}\n", i, conv("pt", p.Type))
|
g.writef("p%d = %s\n}\n", i, conv("pt", p.Type))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,11 +125,11 @@ func (g *generator) router(router Router, pathVersion int) {
|
|||||||
if i != 0 {
|
if i != 0 {
|
||||||
g.WriteString(", ")
|
g.WriteString(", ")
|
||||||
}
|
}
|
||||||
if p.Name == "spec" {
|
if p.Type == "*onebot.Spec" {
|
||||||
fmt.Fprintf(g.out, "spec")
|
g.WriteString("spec")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Fprintf(g.out, "p%d", i)
|
g.writef("p%d", i)
|
||||||
}
|
}
|
||||||
g.WriteString(")\n")
|
g.WriteString(")\n")
|
||||||
}
|
}
|
||||||
@ -175,7 +137,7 @@ func (g *generator) router(router Router, pathVersion int) {
|
|||||||
func conv(v, t string) string {
|
func conv(v, t string) string {
|
||||||
switch t {
|
switch t {
|
||||||
default:
|
default:
|
||||||
panic("unknown type: " + t)
|
panic("unsupported type: " + t)
|
||||||
case "gjson.Result", "*onebot.Spec":
|
case "gjson.Result", "*onebot.Spec":
|
||||||
return v
|
return v
|
||||||
case "int64":
|
case "int64":
|
||||||
@ -208,23 +170,15 @@ func main() {
|
|||||||
for _, decl := range file.Decls {
|
for _, decl := range file.Decls {
|
||||||
switch decl := decl.(type) {
|
switch decl := decl.(type) {
|
||||||
case *ast.FuncDecl:
|
case *ast.FuncDecl:
|
||||||
if !decl.Name.IsExported() || decl.Recv == nil {
|
if !decl.Name.IsExported() || decl.Recv == nil ||
|
||||||
continue
|
typeName(decl.Recv.List[0].Type) != "*CQBot" {
|
||||||
}
|
|
||||||
if st, ok := decl.Recv.List[0].Type.(*ast.StarExpr); !ok || st.X.(*ast.Ident).Name != "CQBot" {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
router := Router{Func: decl.Name.Name}
|
router := Router{Func: decl.Name.Name}
|
||||||
|
|
||||||
// compute params
|
// compute params
|
||||||
for _, p := range decl.Type.Params.List {
|
for _, p := range decl.Type.Params.List {
|
||||||
var typ string
|
typ := typeName(p.Type)
|
||||||
switch t := p.Type.(type) {
|
|
||||||
case *ast.Ident:
|
|
||||||
typ = t.Name
|
|
||||||
case *ast.SelectorExpr:
|
|
||||||
typ = t.X.(*ast.Ident).Name + "." + t.Sel.Name
|
|
||||||
}
|
|
||||||
for _, name := range p.Names {
|
for _, name := range p.Names {
|
||||||
router.Params = append(router.Params, Param{Name: snakecase(name.Name), Type: typ})
|
router.Params = append(router.Params, Param{Name: snakecase(name.Name), Type: typ})
|
||||||
}
|
}
|
||||||
@ -261,13 +215,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "version":
|
|
||||||
version := strings.Split(args, ",")
|
|
||||||
for _, v := range version {
|
|
||||||
if i, err := strconv.ParseUint(v, 10, 16); err == nil {
|
|
||||||
router.Version = append(router.Version, uint16(i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sort.Slice(router.Path, func(i, j int) bool {
|
sort.Slice(router.Path, func(i, j int) bool {
|
||||||
return router.Path[i] < router.Path[j]
|
return router.Path[i] < router.Path[j]
|
||||||
@ -335,7 +282,7 @@ func unquote(s string) string {
|
|||||||
func parseMap(input string, sep string) map[string]string {
|
func parseMap(input string, sep string) map[string]string {
|
||||||
out := make(map[string]string)
|
out := make(map[string]string)
|
||||||
for _, arg := range strings.Split(input, ",") {
|
for _, arg := range strings.Split(input, ",") {
|
||||||
k, v, ok := cut(arg, sep)
|
k, v, ok := strings.Cut(arg, sep)
|
||||||
if !ok {
|
if !ok {
|
||||||
out[k] = "true"
|
out[k] = "true"
|
||||||
}
|
}
|
||||||
@ -353,20 +300,13 @@ func match(text string) (string, string) {
|
|||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
text = strings.Trim(text, "@)")
|
text = strings.Trim(text, "@)")
|
||||||
cmd, args, ok := cut(text, "(")
|
cmd, args, ok := strings.Cut(text, "(")
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
return cmd, unquote(args)
|
return cmd, unquote(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cut(s, sep string) (before, after string, found bool) {
|
|
||||||
if i := strings.Index(s, sep); i >= 0 {
|
|
||||||
return s[:i], s[i+len(sep):], true
|
|
||||||
}
|
|
||||||
return s, "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
// some abbreviations need translation before transforming ro snake case
|
// some abbreviations need translation before transforming ro snake case
|
||||||
var replacer = strings.NewReplacer("ID", "Id")
|
var replacer = strings.NewReplacer("ID", "Id")
|
||||||
|
|
||||||
@ -400,3 +340,16 @@ func convDefault(s string, t string) string {
|
|||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func typeName(x ast.Node) string {
|
||||||
|
switch x := x.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
return x.Name
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
return typeName(x.X) + "." + x.Sel.Name
|
||||||
|
case *ast.StarExpr:
|
||||||
|
return "*" + typeName(x.X)
|
||||||
|
default:
|
||||||
|
panic("unhandled type: " + reflect.TypeOf(x).String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,23 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *Caller) call(action string, spec *onebot.Spec, p Getter) global.MSG {
|
func (c *Caller) call(action string, spec *onebot.Spec, p Getter) global.MSG {
|
||||||
if spec.Version == 12 {
|
|
||||||
switch action {
|
|
||||||
case "get_self_info":
|
|
||||||
return c.bot.CQGetLoginInfo()
|
|
||||||
case "get_user_info":
|
|
||||||
p0 := p.Get("user_id").Int()
|
|
||||||
return c.bot.CQGetStrangerInfo(p0)
|
|
||||||
case "get_version":
|
|
||||||
return c.bot.CQGetVersion()
|
|
||||||
case "send_message":
|
|
||||||
p0 := p.Get("group_id").String()
|
|
||||||
p1 := p.Get("user_id").String()
|
|
||||||
p2 := p.Get("detail_type").String()
|
|
||||||
p3 := p.Get("message")
|
|
||||||
return c.bot.CQSendMessageV12(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if spec.Version == 11 {
|
if spec.Version == 11 {
|
||||||
switch action {
|
switch action {
|
||||||
case ".handle_quick_operation":
|
case ".handle_quick_operation":
|
||||||
@ -77,6 +60,23 @@ func (c *Caller) call(action string, spec *onebot.Spec, p Getter) global.MSG {
|
|||||||
return c.bot.CQSendPrivateMessage(p0, p1, p2, p3)
|
return c.bot.CQSendPrivateMessage(p0, p1, p2, p3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if spec.Version == 12 {
|
||||||
|
switch action {
|
||||||
|
case "get_self_info":
|
||||||
|
return c.bot.CQGetLoginInfo()
|
||||||
|
case "get_user_info":
|
||||||
|
p0 := p.Get("user_id").Int()
|
||||||
|
return c.bot.CQGetStrangerInfo(p0)
|
||||||
|
case "get_version":
|
||||||
|
return c.bot.CQGetVersion()
|
||||||
|
case "send_message":
|
||||||
|
p0 := p.Get("group_id").String()
|
||||||
|
p1 := p.Get("user_id").String()
|
||||||
|
p2 := p.Get("detail_type").String()
|
||||||
|
p3 := p.Get("message")
|
||||||
|
return c.bot.CQSendMessageV12(p0, p1, p2, p3)
|
||||||
|
}
|
||||||
|
}
|
||||||
switch action {
|
switch action {
|
||||||
case ".get_word_slices":
|
case ".get_word_slices":
|
||||||
p0 := p.Get("content").String()
|
p0 := p.Get("content").String()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user