mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
style: go fmt ./...
also delete mkrw.go, we can maintain this file by hand.
This commit is contained in:
parent
eaf34288de
commit
18a091145a
@ -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)
|
||||
|
@ -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")
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
// Code generated by mkrw.go; DO NOT EDIT.
|
||||
|
||||
package leveldb
|
||||
|
||||
import "github.com/Mrs4s/go-cqhttp/db"
|
@ -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 {
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// defaultConfig 默认配置文件
|
||||
//
|
||||
//go:embed default_config.yml
|
||||
var defaultConfig string
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user