diff --git a/coolq/cqcode/escape.go b/coolq/cqcode/escape.go index a9f0cdf..cee9033 100644 --- a/coolq/cqcode/escape.go +++ b/coolq/cqcode/escape.go @@ -3,15 +3,11 @@ package cqcode import "strings" -/*EscapeText 将字符串raw中部分字符转义 - -& -> & - -[ -> [ - -] -> ] - -*/ +// EscapeText 将字符串raw中部分字符转义 +// +// - & -> & +// - [ -> [ +// - ] -> ] func EscapeText(s string) string { count := strings.Count(s, "&") count += strings.Count(s, "[") @@ -47,31 +43,22 @@ func EscapeText(s string) string { return b.String() } -/*EscapeValue 将字符串value中部分字符转义 - -, -> , - -& -> & - -[ -> [ - -] -> ] - -*/ +// EscapeValue 将字符串value中部分字符转义 +// +// - , -> , +// - & -> & +// - [ -> [ +// - ] -> ] func EscapeValue(value string) string { ret := EscapeText(value) return strings.ReplaceAll(ret, ",", ",") } -/*UnescapeText 将字符串content中部分字符反转义 - -& -> & - -[ -> [ - -] -> ] - -*/ +// UnescapeText 将字符串content中部分字符反转义 +// +// - & -> & +// - [ -> [ +// - ] -> ] func UnescapeText(content string) string { ret := content ret = strings.ReplaceAll(ret, "[", "[") @@ -80,17 +67,12 @@ func UnescapeText(content string) string { return ret } -/*UnescapeValue 将字符串content中部分字符反转义 - -, -> , - -& -> & - -[ -> [ - -] -> ] - -*/ +// UnescapeValue 将字符串content中部分字符反转义 +// +// - , -> , +// - & -> & +// - [ -> [ +// - ] -> ] func UnescapeValue(content string) string { ret := strings.ReplaceAll(content, ",", ",") return UnescapeText(ret) diff --git a/db/leveldb/mkrw.go b/db/leveldb/mkrw.go deleted file mode 100644 index d7a7489..0000000 --- a/db/leveldb/mkrw.go +++ /dev/null @@ -1,129 +0,0 @@ -//go:build ignore - -package main - -import ( - "bytes" - "fmt" - "go/ast" - "go/format" - "go/parser" - "go/token" - "os" -) - -var output bytes.Buffer - -func fprintf(format string, args ...interface{}) { - _, _ = fmt.Fprintf(&output, format, args...) -} - -func main() { - f, _ := parser.ParseFile(token.NewFileSet(), "./../database.go", nil, 0) - fprintf("// Code generated by mkrw.go; DO NOT EDIT.\n\n") - fprintf("package leveldb\n\n") - fprintf("import \"github.com/Mrs4s/go-cqhttp/db\"\n\n") - ast.Inspect(f, func(node ast.Node) bool { - switch node := node.(type) { - case *ast.FuncDecl: - return false - case *ast.TypeSpec: - if !node.Name.IsExported() { - return false - } - x, ok := node.Type.(*ast.StructType) - if !ok { - return false - } - if x.Fields != nil && x.Fields.List != nil { - mkWrite(node) - mkRead(node) - } - } - return true - }) - out, err := format.Source(output.Bytes()) - if err != nil { - fmt.Println(string(output.Bytes())) - panic(err) - } - os.WriteFile("database_gen.go", out, 0o644) -} - -func typeName(typ ast.Expr) string { - switch typ := typ.(type) { - case *ast.Ident: - return typ.Name - case *ast.ArrayType: - if typ.Len != nil { - panic("unexpected array type") - } - return "[]" + typeName(typ.Elt) - case *ast.SelectorExpr: - return typeName(typ.X) + "." + typ.Sel.Name - } - panic("unexpected type") -} - -func mkWrite(node *ast.TypeSpec) { - typename := node.Name.String() - structType := node.Type.(*ast.StructType) - fprintf("func (w *writer) write%s(x *db.%s) {\n", typename, typename) - fprintf("if x == nil {\n") - fprintf("w.nil()\n") - fprintf("return\n") - fprintf("}\n") - fprintf("w.coder(coderStruct)\n") - for _, field := range structType.Fields.List { - switch typ := field.Type.(type) { - case *ast.Ident: - for _, name := range field.Names { - fprintf("w.%s(x.%s)\n", typ.Name, name.Name) - } - case *ast.ArrayType: - if typeName(typ) != "[]global.MSG" { - panic("unexpected array type") - } - for _, name := range field.Names { - fprintf("w.arrayMsg(x.%s)\n", name.Name) - } - case *ast.StarExpr: - for _, name := range field.Names { - fprintf("w.write%s(x.%s)\n", typeName(typ.X), name.Name) - } - } - } - fprintf("}\n\n") -} - -func mkRead(node *ast.TypeSpec) { - typename := node.Name.String() - structType := node.Type.(*ast.StructType) - fprintf(`func (r *reader) read%s() *db.%s { - coder := r.coder() - if coder == coderNil { - return nil - }`+"\n", typename, typename) - fprintf("x := &db.%s{}\n", typename) - for _, field := range structType.Fields.List { - switch typ := field.Type.(type) { - case *ast.Ident: - for _, name := range field.Names { - fprintf("x.%s = r.%s()\n", name.Name, typ.Name) - } - case *ast.ArrayType: - if typeName(typ) != "[]global.MSG" { - panic("unexpected array type") - } - for _, name := range field.Names { - fprintf("x.%s = r.arrayMsg()\n", name.Name) - } - case *ast.StarExpr: - for _, name := range field.Names { - fprintf("x.%s = r.read%s()\n", name.Name, typeName(typ.X)) - } - } - } - fprintf("return x\n") - fprintf("}\n\n") -} diff --git a/db/leveldb/database_gen.go b/db/leveldb/structs.go similarity index 98% rename from db/leveldb/database_gen.go rename to db/leveldb/structs.go index 929aeac..dfb7caa 100644 --- a/db/leveldb/database_gen.go +++ b/db/leveldb/structs.go @@ -1,5 +1,3 @@ -// Code generated by mkrw.go; DO NOT EDIT. - package leveldb import "github.com/Mrs4s/go-cqhttp/db" diff --git a/db/leveldb/writer.go b/db/leveldb/writer.go index baed92a..6067ca1 100644 --- a/db/leveldb/writer.go +++ b/db/leveldb/writer.go @@ -23,12 +23,25 @@ func (w *intWriter) uvarint(x uint64) { } // writer implements the index write. +// // data format(use uvarint to encode integers): -// | version | string data length | index data length | string data | index data | +// +// - version +// - string data length +// - index data length +// - string data +// - index data +// // for string data part, each string is encoded as: -// | string length | string | -// for index data part, each value is encoded as: -// | coder | value | +// +// - string length +// - string +// +// for index data part, each object value is encoded as: +// +// - coder +// - value +// // * coder is the identifier of value's type. // * specially for string, it's value is the offset in string data part. type writer struct { diff --git a/modules/config/config.go b/modules/config/config.go index 158bd62..23329d7 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -14,6 +14,7 @@ import ( ) // defaultConfig 默认配置文件 +// //go:embed default_config.yml var defaultConfig string