mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-21 20:20:17 +01:00
init
This commit is contained in:
commit
3b23208ee0
23 changed files with 49288 additions and 0 deletions
83
cmd/generate-code.go
Normal file
83
cmd/generate-code.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/zelenin/go-tdlib/codegen"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
schemaFilePath string
|
||||
outputDirPath string
|
||||
packageName string
|
||||
functionFileName string
|
||||
typeFileName string
|
||||
unmarshalerFileName string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var config config
|
||||
|
||||
flag.StringVar(&config.schemaFilePath, "schema", "./td_api.tl", ".tl schema file")
|
||||
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")
|
||||
flag.StringVar(&config.typeFileName, "typeFile", "type.go", "types filename")
|
||||
flag.StringVar(&config.unmarshalerFileName, "unmarshalerFile", "unmarshaler.go", "unmarshalers filename")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
schemaFile, err := os.OpenFile(config.schemaFilePath, os.O_RDONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("schemaFile open error: %s", err)
|
||||
}
|
||||
defer schemaFile.Close()
|
||||
|
||||
schema, err := tlparser.Parse(schemaFile)
|
||||
if err != nil {
|
||||
log.Fatalf("schema parse error: %s", err)
|
||||
}
|
||||
|
||||
err = os.MkdirAll(config.outputDirPath, 0755)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating %s: %s", config.outputDirPath, err)
|
||||
}
|
||||
|
||||
functionFilePath := filepath.Join(config.outputDirPath, config.functionFileName)
|
||||
|
||||
os.Remove(functionFilePath)
|
||||
functionFile, err := os.OpenFile(functionFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("functionFile open error: %s", err)
|
||||
}
|
||||
defer functionFile.Close()
|
||||
|
||||
bufio.NewWriter(functionFile).Write(codegen.GenerateFunctions(schema, config.packageName))
|
||||
|
||||
typeFilePath := filepath.Join(config.outputDirPath, config.typeFileName)
|
||||
|
||||
os.Remove(typeFilePath)
|
||||
typeFile, err := os.OpenFile(typeFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("typeFile open error: %s", err)
|
||||
}
|
||||
defer typeFile.Close()
|
||||
|
||||
bufio.NewWriter(typeFile).Write(codegen.GenerateTypes(schema, config.packageName))
|
||||
|
||||
unmarshalerFilePath := filepath.Join(config.outputDirPath, config.unmarshalerFileName)
|
||||
|
||||
os.Remove(unmarshalerFilePath)
|
||||
unmarshalerFile, err := os.OpenFile(unmarshalerFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("unmarshalerFile open error: %s", err)
|
||||
}
|
||||
defer unmarshalerFile.Close()
|
||||
|
||||
bufio.NewWriter(unmarshalerFile).Write(codegen.GenerateUnmarshalers(schema, config.packageName))
|
||||
}
|
||||
53
cmd/generate-json.go
Normal file
53
cmd/generate-json.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var inputFilePath string
|
||||
var outputFilePath string
|
||||
|
||||
flag.StringVar(&inputFilePath, "input", "./td_api.tl", "tl schema file")
|
||||
flag.StringVar(&outputFilePath, "output", "./td_api.json", "json schema file")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
file, err := os.OpenFile(inputFilePath, os.O_RDONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("open file error: %s", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
schema, err := tlparser.Parse(file)
|
||||
if err != nil {
|
||||
log.Fatalf("schema parse 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)
|
||||
if err != nil {
|
||||
log.Fatalf("open file error: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(schema, "", strings.Repeat(" ", 4))
|
||||
if err != nil {
|
||||
log.Fatalf("json marshal error: %s", err)
|
||||
return
|
||||
}
|
||||
bufio.NewWriter(file).Write(data)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue