schema improvement

This commit is contained in:
Aleksandr Zelenin 2018-10-19 21:06:07 +03:00
parent ccea55b18f
commit c780ca35c4
7 changed files with 732 additions and 326 deletions

View file

@ -5,12 +5,12 @@ schema-update:
generate-json:
go run ./cmd/generate-json.go \
-input "./data/td_api.tl" \
-version "${TAG}" \
-output "./data/td_api.json"
generate-code:
go run ./cmd/generate-code.go \
-schema "./data/td_api.tl" \
-version "${TAG}" \
-outputDir "./client" \
-package client \
-functionFile function.go \

View file

@ -4,6 +4,7 @@ import (
"bufio"
"flag"
"log"
"net/http"
"os"
"path/filepath"
@ -12,7 +13,7 @@ import (
)
type config struct {
schemaFilePath string
version string
outputDirPath string
packageName string
functionFileName string
@ -23,7 +24,7 @@ type config struct {
func main() {
var config config
flag.StringVar(&config.schemaFilePath, "schema", "./td_api.tl", ".tl schema file")
flag.StringVar(&config.version, "version", "", "TDLib version")
flag.StringVar(&config.outputDirPath, "outputDir", "./tdlib", "output directory")
flag.StringVar(&config.packageName, "package", "tdlib", "package name")
flag.StringVar(&config.functionFileName, "functionFile", "function.go", "functions filename")
@ -32,15 +33,17 @@ func main() {
flag.Parse()
schemaFile, err := os.OpenFile(config.schemaFilePath, os.O_RDONLY, os.ModePerm)
resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + config.version + "/td/generate/scheme/td_api.tl")
if err != nil {
log.Fatalf("schemaFile open error: %s", err)
log.Fatalf("http.Get error: %s", err)
return
}
defer schemaFile.Close()
defer resp.Body.Close()
schema, err := tlparser.Parse(schemaFile)
schema, err := tlparser.Parse(resp.Body)
if err != nil {
log.Fatalf("schema parse error: %s", err)
return
}
err = os.MkdirAll(config.outputDirPath, 0755)

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"log"
"net/http"
"os"
"path/filepath"
"strings"
@ -12,33 +13,46 @@ import (
)
func main() {
var inputFilePath string
var version string
var outputFilePath string
flag.StringVar(&inputFilePath, "input", "./td_api.tl", "tl schema file")
flag.StringVar(&version, "version", "", "TDLib version")
flag.StringVar(&outputFilePath, "output", "./td_api.json", "json schema file")
flag.Parse()
file, err := os.OpenFile(inputFilePath, os.O_RDONLY, os.ModePerm)
resp, err := http.Get("https://raw.githubusercontent.com/tdlib/td/" + version + "/td/generate/scheme/td_api.tl")
if err != nil {
log.Fatalf("open file error: %s", err)
log.Fatalf("http.Get error: %s", err)
return
}
defer file.Close()
defer resp.Body.Close()
schema, err := tlparser.Parse(file)
schema, err := tlparser.Parse(resp.Body)
if err != nil {
log.Fatalf("schema parse error: %s", err)
return
}
resp, err = http.Get("https://raw.githubusercontent.com/tdlib/td/" + version + "/td/telegram/Td.cpp")
if err != nil {
log.Fatalf("http.Get error: %s", err)
return
}
defer resp.Body.Close()
err = tlparser.ParseCode(resp.Body, schema)
if err != nil {
log.Fatalf("parse code error: %s", err)
return
}
err = os.MkdirAll(filepath.Dir(outputFilePath), os.ModePerm)
if err != nil {
log.Fatalf("make dir error: %s", filepath.Dir(outputFilePath))
}
file, err = os.OpenFile(outputFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
file, err := os.OpenFile(outputFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Fatalf("open file error: %s", err)
return

File diff suppressed because it is too large Load diff

74
tlparser/code.go Normal file
View file

@ -0,0 +1,74 @@
package tlparser
import (
"bufio"
"fmt"
"io"
"strings"
)
func ParseCode(reader io.Reader, schema *Schema) error {
var prevLine string
var curLine string
userMethods := map[string]bool{}
botMethods := map[string]bool{}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
prevLine = curLine
curLine = scanner.Text()
if strings.Contains(curLine, "CHECK_IS_USER();") {
fields := strings.Fields(prevLine)
for _, field := range fields {
var methodName string
n, err := fmt.Sscanf(field, "td_api::%s", &methodName)
if err == nil && n > 0 {
userMethods[methodName] = true
}
}
}
if strings.Contains(curLine, "CHECK_IS_BOT();") {
fields := strings.Fields(prevLine)
for _, field := range fields {
var methodName string
n, err := fmt.Sscanf(field, "td_api::%s", &methodName)
if err == nil && n > 0 {
botMethods[methodName] = true
}
}
}
}
err := scanner.Err()
if err != nil {
return err
}
var ok bool
for index, _ := range schema.Functions {
hasType := false
_, ok = userMethods[schema.Functions[index].Name]
if ok {
schema.Functions[index].Type = FUNCTION_TYPE_USER
hasType = true
}
_, ok = botMethods[schema.Functions[index].Name]
if ok {
schema.Functions[index].Type = FUNCTION_TYPE_BOT
hasType = true
}
if !hasType {
schema.Functions[index].Type = FUNCTION_TYPE_COMMON
}
ok = false
}
return nil
}

View file

@ -42,10 +42,12 @@ func Parse(reader io.Reader) (*Schema, error) {
class := strings.TrimRight(bodyFields[len(bodyFields)-1], ";")
if hitFunctions {
schema.Functions = append(schema.Functions, &Function{
Name: name,
Description: "",
Class: class,
Properties: []*Property{},
Name: name,
Description: "",
Class: class,
Properties: []*Property{},
IsSynchronous: false,
Type: FUNCTION_TYPE_UNKNOWN,
})
} else {
if name == "vector" {
@ -84,6 +86,7 @@ func parseFunction(firstLine string, scanner *bufio.Scanner) *Function {
Class: class,
Properties: properties,
IsSynchronous: isSynchronous,
Type: FUNCTION_TYPE_UNKNOWN,
}
}

View file

@ -18,12 +18,22 @@ type Class struct {
Description string `json:"description"`
}
type FunctionType int
const (
FUNCTION_TYPE_UNKNOWN FunctionType = iota
FUNCTION_TYPE_COMMON
FUNCTION_TYPE_USER
FUNCTION_TYPE_BOT
)
type Function struct {
Name string `json:"name"`
Description string `json:"description"`
Class string `json:"class"`
Properties []*Property `json:"properties"`
IsSynchronous bool `json:"is_synchronous"`
Name string `json:"name"`
Description string `json:"description"`
Class string `json:"class"`
Properties []*Property `json:"properties"`
IsSynchronous bool `json:"is_synchronous"`
Type FunctionType `json:"type"`
}
type Property struct {