This commit is contained in:
Aleksandr Zelenin 2018-08-30 17:55:42 +03:00
commit 3b23208ee0
23 changed files with 49288 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.idea
/.tdlib
/.cmd

18
Makefile Normal file
View file

@ -0,0 +1,18 @@
TAG := v1.2.0
schema-update:
curl https://raw.githubusercontent.com/tdlib/td/${TAG}/td/generate/scheme/td_api.tl 2>/dev/null > ./data/td_api.tl
generate-json:
go run ./cmd/generate-json.go \
-input "./data/td_api.tl" \
-output "./data/td_api.json"
generate-code:
go run ./cmd/generate-code.go \
-schema "./data/td_api.tl" \
-outputDir "./client" \
-package client \
-functionFile function.go \
-typeFile type.go \
-unmarshalerFile unmarshaler.go

117
README.md Normal file
View file

@ -0,0 +1,117 @@
# go-tdlib
Go wrapper for [TDLib (Telegram Database Library)](https://github.com/tdlib/td) with full support of TDLib v1.2.0
## TDLib installation
### Ubuntu 18.04 / Debian 9
apt-get update -y
apt-get install -y \
build-essential \
ca-certificates \
ccache \
cmake \
git \
gperf \
libssl-dev \
libreadline-dev \
zlib1g-dev
git clone --depth 1 -b "v1.2.0" "https://github.com/tdlib/td.git" ./tdlib-src
mkdir ./tdlib-src/build
cd ./tdlib-src/build
cmake -j$(getconf _NPROCESSORS_ONLN) -DCMAKE_BUILD_TYPE=Release ..
cmake -j$(getconf _NPROCESSORS_ONLN) --build .
make -j$(getconf _NPROCESSORS_ONLN) install
rm -rf ./../../tdlib-src
## Usage
### Client
[Register an application](https://my.telegram.org/apps) to obtain an api_id and api_hash
```go
package main
import (
"log"
"github.com/zelenin/go-tdlib/client"
)
func main() {
client.SetLogVerbosityLevel(1)
client.SetLogFilePath("/dev/stderr")
// client authorizer
authorizer := client.ClientAuthorizer()
go client.CliInteractor(authorizer)
// or bot authorizer
botToken := "000000000:gsVCGG5YbikxYHC7bP5vRvmBqJ7Xz6vG6td"
authorizer := client.BotAuthorizer(botToken)
const (
apiId = 00000
apiHash = "8pu9yg32qkuukj83ozaqo5zzjwhkxhnk"
)
authorizer.TdlibParameters <- &client.TdlibParameters{
UseTestDc: false,
DatabaseDirectory: "./.tdlib/database",
FilesDirectory: "./.tdlib/files",
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseMessageDatabase: true,
UseSecretChats: false,
ApiId: apiId,
ApiHash: apiHash,
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
EnableStorageOptimizer: true,
IgnoreFileNames: false,
}
tdlibClient, err := client.NewClient(authorizer)
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
me, err := tdlibClient.GetMe()
if err != nil {
log.Fatalf("GetMe error: %s", err)
}
log.Printf("Me: %s %s [%s]", me.FirstName, me.LastName, me.Username)
}
```
### Receive updates
```go
responses := make(chan client.Type, 100)
tdlibClient, err := client.NewClient(authorizer, client.WithListener(responses))
if err != nil {
log.Fatalf("NewClient error: %s", err)
}
for response := range responses {
if response.GetClass() == client.ClassUpdate {
log.Printf("%#v", response)
}
}
```
## Notes
* WIP. Library API can be changed in the future
* The package includes a .tl-parser and generated [json-schema](https://github.com/zelenin/go-tdlib/tree/master/data) for creating libraries in other languages
## Author
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [aleksandr@zelenin.me](mailto:aleksandr@zelenin.me)

179
client/authorization.go Normal file
View file

@ -0,0 +1,179 @@
package client
import (
"errors"
"fmt"
"time"
)
var ErrNotSupportedAuthorizationState = errors.New("not supported state")
type AuthorizationStateHandler interface {
Handle(client *Client, state AuthorizationState) error
}
func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandler) error {
for {
state, err := client.GetAuthorizationState()
if err != nil {
return err
}
err = authorizationStateHandler.Handle(client, state)
if err != nil {
return err
}
if state.AuthorizationStateType() == TypeAuthorizationStateReady {
// dirty hack for db flush after authorization
time.Sleep(1 * time.Second)
return nil
}
}
}
type clientAuthorizer struct {
TdlibParameters chan *TdlibParameters
PhoneNumber chan string
Code chan string
State chan AuthorizationState
}
func ClientAuthorizer() *clientAuthorizer {
return &clientAuthorizer{
TdlibParameters: make(chan *TdlibParameters, 1),
PhoneNumber: make(chan string, 1),
Code: make(chan string, 1),
State: make(chan AuthorizationState, 10),
}
}
func (stateHandler *clientAuthorizer) Handle(client *Client, state AuthorizationState) error {
stateHandler.State <- state
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
return err
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(nil)
return err
case TypeAuthorizationStateWaitPhoneNumber:
_, err := client.SetAuthenticationPhoneNumber(<-stateHandler.PhoneNumber, false, false)
return err
case TypeAuthorizationStateWaitCode:
_, err := client.CheckAuthenticationCode(<-stateHandler.Code, "", "")
return err
case TypeAuthorizationStateWaitPassword:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateReady:
close(stateHandler.TdlibParameters)
close(stateHandler.PhoneNumber)
close(stateHandler.Code)
close(stateHandler.State)
return nil
case TypeAuthorizationStateLoggingOut:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosing:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosed:
return ErrNotSupportedAuthorizationState
}
return ErrNotSupportedAuthorizationState
}
func CliInteractor(clientAuthorizer *clientAuthorizer) {
for {
select {
case state := <-clientAuthorizer.State:
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitPhoneNumber:
fmt.Println("Enter phone number: ")
var phoneNumber string
fmt.Scanln(&phoneNumber)
clientAuthorizer.PhoneNumber <- phoneNumber
case TypeAuthorizationStateWaitCode:
fmt.Println("Enter code: ")
var code string
fmt.Scanln(&code)
clientAuthorizer.Code <- code
case TypeAuthorizationStateReady:
return
}
}
}
}
type botAuthorizer struct {
TdlibParameters chan *TdlibParameters
Token chan string
State chan AuthorizationState
}
func BotAuthorizer(token string) *botAuthorizer {
botAuthorizer := &botAuthorizer{
TdlibParameters: make(chan *TdlibParameters, 1),
Token: make(chan string, 1),
State: make(chan AuthorizationState, 10),
}
botAuthorizer.Token <- token
return botAuthorizer
}
func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationState) error {
stateHandler.State <- state
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
return err
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(nil)
return err
case TypeAuthorizationStateWaitPhoneNumber:
_, err := client.CheckAuthenticationBotToken(<-stateHandler.Token)
return err
case TypeAuthorizationStateWaitCode:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateWaitPassword:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateReady:
close(stateHandler.TdlibParameters)
close(stateHandler.Token)
close(stateHandler.State)
return nil
case TypeAuthorizationStateLoggingOut:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosing:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosed:
return ErrNotSupportedAuthorizationState
}
return ErrNotSupportedAuthorizationState
}

111
client/client.go Normal file
View file

@ -0,0 +1,111 @@
package client
import (
"errors"
"sync"
"time"
)
type Client struct {
jsonClient *JsonClient
extraGenerator ExtraGenerator
catcher chan *Response
listeners []chan Type
catchersStore *sync.Map
}
type Option func(*Client)
func WithExtraGenerator(extraGenerator ExtraGenerator) Option {
return func(client *Client) {
client.extraGenerator = extraGenerator
}
}
func WithListener(listener chan Type) Option {
return func(client *Client) {
client.listeners = append(client.listeners, listener)
}
}
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
catchersListener := make(chan *Response, 1000)
client := &Client{
jsonClient: NewJsonClient(),
catcher: catchersListener,
listeners: []chan Type{},
catchersStore: &sync.Map{},
}
for _, option := range options {
option(client)
}
if client.extraGenerator == nil {
client.extraGenerator = UuidV4Generator()
}
go client.receive()
go client.catch(catchersListener)
err := Authorize(client, authorizationStateHandler)
if err != nil {
return nil, err
}
return client, nil
}
func (client *Client) receive() {
for {
resp, err := client.jsonClient.Receive(10)
if err != nil {
continue
}
client.catcher <- resp
typ, err := UnmarshalType(resp.Data)
if err != nil {
continue
}
for _, listener := range client.listeners {
listener <- typ
}
}
}
func (client *Client) catch(updates chan *Response) {
for update := range updates {
if update.Extra != "" {
value, ok := client.catchersStore.Load(update.Extra)
if ok {
value.(chan *Response) <- update
}
}
}
}
func (client *Client) Send(req Request) (*Response, error) {
req.Extra = client.extraGenerator()
catcher := make(chan *Response, 1)
client.catchersStore.Store(req.Extra, catcher)
defer func() {
close(catcher)
client.catchersStore.Delete(req.Extra)
}()
client.jsonClient.Send(req)
select {
case response := <-catcher:
return response, nil
case <-time.After(10 * time.Second):
return nil, errors.New("timeout")
}
}

20
client/extra.go Normal file
View file

@ -0,0 +1,20 @@
package client
import (
"fmt"
"math/rand"
)
type ExtraGenerator func() string
func UuidV4Generator() ExtraGenerator {
return func() string {
var uuid [16]byte
rand.Read(uuid[:])
uuid[6] = (uuid[6] & 0x0f) | 0x40
uuid[8] = (uuid[8] & 0x3f) | 0x80
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
}

6482
client/function.go Executable file

File diff suppressed because it is too large Load diff

188
client/tdlib.go Normal file
View file

@ -0,0 +1,188 @@
package client
// #cgo linux CFLAGS: -I/usr/local/include
// #cgo darwin CFLAGS: -I/usr/local/include
// #cgo windows CFLAGS: -IC:/src/td -IC:/src/td/build
// #cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
// #cgo darwin LDFLAGS: -L/usr/local/lib -L/usr/local/opt/openssl/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
// #cgo windows LDFLAGS: -LC:/src/td/build/Debug -ltdjson
// #include <stdlib.h>
// #include <td/telegram/td_json_client.h>
// #include <td/telegram/td_log.h>
import "C"
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"unsafe"
)
type JsonClient struct {
jsonClient unsafe.Pointer
}
func NewJsonClient() *JsonClient {
jsonClient := &JsonClient{
jsonClient: C.td_json_client_create(),
}
return jsonClient
}
// Sends request to the TDLib client. May be called from any thread.
func (jsonClient *JsonClient) Send(req Request) {
data, _ := json.Marshal(req)
query := C.CString(string(data))
defer C.free(unsafe.Pointer(query))
C.td_json_client_send(jsonClient.jsonClient, query)
}
// Receives incoming updates and request responses from the TDLib client. May be called from any thread, but
// shouldn't be called simultaneously from two different threads.
// Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute
// in the same thread, so it can't be used after that.
func (jsonClient *JsonClient) Receive(timeout float64) (*Response, error) {
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(timeout))
if result == nil {
return nil, errors.New("timeout")
}
data := []byte(C.GoString(result))
var resp Response
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
resp.Data = data
return &resp, nil
}
// Synchronously executes TDLib request. May be called from any thread.
// Only a few requests can be executed synchronously.
// Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute
// in the same thread, so it can't be used after that.
func (jsonClient *JsonClient) Execute(req Request) (*Response, error) {
data, _ := json.Marshal(req)
query := C.CString(string(data))
defer C.free(unsafe.Pointer(query))
result := C.td_json_client_execute(jsonClient.jsonClient, query)
if result == nil {
return nil, errors.New("request can't be parsed")
}
data = []byte(C.GoString(result))
var resp Response
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
resp.Data = data
return &resp, nil
}
// Destroys the TDLib client instance. After this is called the client instance shouldn't be used anymore.
func (jsonClient *JsonClient) DestroyInstance() {
C.td_json_client_destroy(jsonClient.jsonClient)
}
// Sets the path to the file where the internal TDLib log will be written.
// By default TDLib writes logs to stderr or an OS specific log.
// Use this method to write the log to a file instead.
func SetLogFilePath(filePath string) {
query := C.CString(filePath)
defer C.free(unsafe.Pointer(query))
C.td_set_log_file_path(query)
}
// Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated.
// Unused if log is not written to a file. Defaults to 10 MB.
func SetLogMaxFileSize(maxFileSize int64) {
C.td_set_log_max_file_size(C.longlong(maxFileSize))
}
// Sets the verbosity level of the internal logging of TDLib.
// By default the TDLib uses a log verbosity level of 5
func SetLogVerbosityLevel(newVerbosityLevel int) {
C.td_set_log_verbosity_level(C.int(newVerbosityLevel))
}
type meta struct {
Type string `json:"@type"`
Extra string `json:"@extra"`
}
type Request struct {
meta
Data map[string]interface{}
}
func (req Request) MarshalJSON() ([]byte, error) {
req.Data["@type"] = req.Type
req.Data["@extra"] = req.Extra
return json.Marshal(req.Data)
}
type Response struct {
meta
Data json.RawMessage
}
type ResponseError struct {
Err *Error
}
func (responseError ResponseError) Error() string {
return fmt.Sprintf("Code: %d. Message: %s", responseError.Err.Code, responseError.Err.Message)
}
func buildResponseError(data json.RawMessage) error {
respErr, err := UnmarshalError(data)
if err != nil {
return err
}
return ResponseError{
Err: respErr,
}
}
// JsonInt64 alias for int64, in order to deal with json big number problem
type JsonInt64 int64
// MarshalJSON marshals to json
func (jsonInt64 *JsonInt64) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(int64(*jsonInt64), 10)), nil
}
// UnmarshalJSON unmarshals from json
func (jsonInt64 *JsonInt64) UnmarshalJSON(data []byte) error {
jsonBigInt, err := strconv.ParseInt(string(data[1:len(data)-1]), 10, 64)
if err != nil {
return err
}
*jsonInt64 = JsonInt64(jsonBigInt)
return nil
}
type Type interface {
GetType() string
GetClass() string
}

17279
client/type.go Executable file

File diff suppressed because it is too large Load diff

7349
client/unmarshaler.go Executable file

File diff suppressed because it is too large Load diff

83
cmd/generate-code.go Normal file
View 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
View 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)
}

112
codegen/function.go Normal file
View file

@ -0,0 +1,112 @@
package codegen
import (
"github.com/zelenin/go-tdlib/tlparser"
"fmt"
"strings"
"bytes"
)
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
buf := bytes.NewBufferString("")
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
buf.WriteString(`import (
"errors"
)`)
buf.WriteString("\n")
for _, function := range schema.Functions {
buf.WriteString("\n")
buf.WriteString("// " + function.Description)
buf.WriteString("\n")
if len(function.Properties) > 0 {
buf.WriteString("//")
buf.WriteString("\n")
}
propertiesParts := []string{}
for _, property := range function.Properties {
tdlibFunctionProperty := TdlibFunctionProperty(property.Name, property.Type, schema)
buf.WriteString(fmt.Sprintf("// @param %s %s", tdlibFunctionProperty.ToGoName(), property.Description))
buf.WriteString("\n")
propertiesParts = append(propertiesParts, tdlibFunctionProperty.ToGoName()+" "+tdlibFunctionProperty.ToGoType())
}
tdlibFunction := TdlibFunction(function.Name, schema)
tdlibFunctionReturn := TdlibFunctionReturn(function.Class, schema)
buf.WriteString(fmt.Sprintf("func (client *Client) %s(%s) (%s, error) {\n", tdlibFunction.ToGoName(), strings.Join(propertiesParts, ", "), tdlibFunctionReturn.ToGoReturn()))
sendMethod := "Send"
if function.IsSynchronous {
sendMethod = "jsonClient.Execute"
}
if len(function.Properties) > 0 {
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
meta: meta{
Type: "%s",
},
Data: map[string]interface{}{
`, sendMethod, function.Name))
for _, property := range function.Properties {
tdlibFunctionProperty := TdlibFunctionProperty(property.Name, property.Type, schema)
buf.WriteString(fmt.Sprintf(" \"%s\": %s,\n", property.Name, tdlibFunctionProperty.ToGoName()))
}
buf.WriteString(` },
})
`)
} else {
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
meta: meta{
Type: "%s",
},
Data: map[string]interface{}{},
})
`, sendMethod, function.Name))
}
buf.WriteString(` if err != nil {
return nil, err
}
if result.Type == "error" {
return nil, buildResponseError(result.Data)
}
`)
if tdlibFunctionReturn.IsClass() {
buf.WriteString(" switch result.Type {\n")
for _, subType := range tdlibFunctionReturn.GetClass().GetSubTypes() {
buf.WriteString(fmt.Sprintf(` case %s:
return Unmarshal%s(result.Data)
`, subType.ToTypeConst(), subType.ToGoType()))
}
buf.WriteString(` default:
return nil, errors.New("invalid type")
`)
buf.WriteString(" }\n")
} else {
buf.WriteString(fmt.Sprintf(` return Unmarshal%s(result.Data)
`, tdlibFunctionReturn.ToGoType()))
}
buf.WriteString("}\n")
}
return buf.Bytes()
}

3
codegen/header.go Normal file
View file

@ -0,0 +1,3 @@
package codegen
const header = "// AUTOGENERATED"

26
codegen/string.go Normal file
View file

@ -0,0 +1,26 @@
package codegen
import (
"strings"
"unicode"
)
func firstUpper(str string) string {
for i, r := range str {
return string(unicode.ToUpper(r)) + str[i+1:]
}
return str
}
func firstLower(str string) string {
for i, r := range str {
return string(unicode.ToLower(r)) + str[i+1:]
}
return str
}
func underscoreToCamelCase(s string) string {
return strings.Replace(strings.Title(strings.Replace(strings.ToLower(s), "_", " ", -1)), " ", "", -1)
}

487
codegen/tdlib.go Normal file
View file

@ -0,0 +1,487 @@
package codegen
import (
"github.com/zelenin/go-tdlib/tlparser"
"strings"
"log"
)
type tdlibFunction struct {
name string
schema *tlparser.Schema
}
func TdlibFunction(name string, schema *tlparser.Schema) *tdlibFunction {
return &tdlibFunction{
name: name,
schema: schema,
}
}
func (entity *tdlibFunction) ToGoName() string {
return firstUpper(entity.name)
}
type tdlibFunctionReturn struct {
name string
schema *tlparser.Schema
}
func TdlibFunctionReturn(name string, schema *tlparser.Schema) *tdlibFunctionReturn {
return &tdlibFunctionReturn{
name: name,
schema: schema,
}
}
func (entity *tdlibFunctionReturn) IsType() bool {
return isType(entity.name, func(entity *tlparser.Type) string {
return entity.Class
}, entity.schema)
}
func (entity *tdlibFunctionReturn) GetType() *tdlibType {
return getType(entity.name, func(entity *tlparser.Type) string {
return entity.Class
}, entity.schema)
}
func (entity *tdlibFunctionReturn) IsClass() bool {
return isClass(entity.name, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionReturn) GetClass() *tdlibClass {
return getClass(entity.name, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionReturn) ToGoReturn() string {
if strings.HasPrefix(entity.name, "vector<") {
log.Fatal("vectors are not supported")
}
if entity.IsClass() {
return entity.GetClass().ToGoType()
}
if entity.GetType().IsInternal() {
return entity.GetType().ToGoType()
}
return "*" + entity.GetType().ToGoType()
}
func (entity *tdlibFunctionReturn) ToGoType() string {
if strings.HasPrefix(entity.name, "vector<") {
log.Fatal("vectors are not supported")
}
if entity.IsClass() {
return entity.GetClass().ToGoType()
}
return entity.GetType().ToGoType()
}
type tdlibFunctionProperty struct {
name string
propertyType string
schema *tlparser.Schema
}
func TdlibFunctionProperty(name string, propertyType string, schema *tlparser.Schema) *tdlibFunctionProperty {
return &tdlibFunctionProperty{
name: name,
propertyType: propertyType,
schema: schema,
}
}
func (entity *tdlibFunctionProperty) GetPrimitive() string {
primitive := entity.propertyType
for strings.HasPrefix(primitive, "vector<") {
primitive = strings.TrimSuffix(strings.TrimPrefix(primitive, "vector<"), ">")
}
return primitive
}
func (entity *tdlibFunctionProperty) IsType() bool {
primitive := entity.GetPrimitive()
return isType(primitive, func(entity *tlparser.Type) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionProperty) GetType() *tdlibType {
primitive := entity.GetPrimitive()
return getType(primitive, func(entity *tlparser.Type) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionProperty) IsClass() bool {
primitive := entity.GetPrimitive()
return isClass(primitive, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionProperty) GetClass() *tdlibClass {
primitive := entity.GetPrimitive()
return getClass(primitive, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibFunctionProperty) ToGoName() string {
name := firstLower(underscoreToCamelCase(entity.name))
if name == "type" {
name += "Param"
}
return name
}
func (entity *tdlibFunctionProperty) ToGoType() string {
tdlibType := entity.propertyType
goType := ""
for strings.HasPrefix(tdlibType, "vector<") {
goType = goType + "[]"
tdlibType = strings.TrimSuffix(strings.TrimPrefix(tdlibType, "vector<"), ">")
}
if entity.IsClass() {
return goType + entity.GetClass().ToGoType()
}
if entity.GetType().IsInternal() {
return goType + entity.GetType().ToGoType()
}
return goType + "*" + entity.GetType().ToGoType()
}
type tdlibType struct {
name string
schema *tlparser.Schema
}
func TdlibType(name string, schema *tlparser.Schema) *tdlibType {
return &tdlibType{
name: name,
schema: schema,
}
}
func (entity *tdlibType) IsInternal() bool {
switch entity.name {
case "double":
return true
case "string":
return true
case "int32":
return true
case "int53":
return true
case "int64":
return true
case "bytes":
return true
case "boolFalse":
return true
case "boolTrue":
return true
case "vector<t>":
return true
}
return false
}
func (entity *tdlibType) GetType() *tlparser.Type {
name := normalizeEntityName(entity.name)
for _, typ := range entity.schema.Types {
if typ.Name == name {
return typ
}
}
return nil
}
func (entity *tdlibType) ToGoType() string {
if strings.HasPrefix(entity.name, "vector<") {
log.Fatal("vectors are not supported")
}
switch entity.name {
case "double":
return "float64"
case "string":
return "string"
case "int32":
return "int32"
case "int53":
return "int64"
case "int64":
return "JsonInt64"
case "bytes":
return "[]byte"
case "boolFalse":
return "bool"
case "boolTrue":
return "bool"
}
return firstUpper(entity.name)
}
func (entity *tdlibType) ToType() string {
return entity.ToGoType() + "Type"
}
func (entity *tdlibType) HasClass() bool {
className := entity.GetType().Class
for _, class := range entity.schema.Classes {
if class.Name == className {
return true
}
}
return false
}
func (entity *tdlibType) GetClass() *tlparser.Class {
className := entity.GetType().Class
for _, class := range entity.schema.Classes {
if class.Name == className {
return class
}
}
return nil
}
func (entity *tdlibType) HasClassProperties() bool {
for _, prop := range entity.GetType().Properties {
tdlibTypeProperty := TdlibTypeProperty(prop.Name, prop.Type, entity.schema)
if tdlibTypeProperty.IsClass() && !tdlibTypeProperty.IsList() {
return true
}
}
return false
}
func (entity *tdlibType) IsList() bool {
return strings.HasPrefix(entity.name, "vector<")
}
func (entity *tdlibType) ToClassConst() string {
if entity.HasClass() {
return "Class" + TdlibClass(entity.GetType().Class, entity.schema).ToGoType()
}
return "Class" + entity.ToGoType()
}
func (entity *tdlibType) ToTypeConst() string {
return "Type" + entity.ToGoType()
}
type tdlibClass struct {
name string
schema *tlparser.Schema
}
func TdlibClass(name string, schema *tlparser.Schema) *tdlibClass {
return &tdlibClass{
name: name,
schema: schema,
}
}
func (entity *tdlibClass) ToGoType() string {
return firstUpper(entity.name)
}
func (entity *tdlibClass) ToType() string {
return entity.ToGoType() + "Type"
}
func (entity *tdlibClass) GetSubTypes() []*tdlibType {
types := []*tdlibType{}
for _, t := range entity.schema.Types {
if t.Class == entity.name {
types = append(types, TdlibType(t.Name, entity.schema))
}
}
return types
}
func (entity *tdlibClass) ToClassConst() string {
return "Class" + entity.ToGoType()
}
type tdlibTypeProperty struct {
name string
propertyType string
schema *tlparser.Schema
}
func TdlibTypeProperty(name string, propertyType string, schema *tlparser.Schema) *tdlibTypeProperty {
return &tdlibTypeProperty{
name: name,
propertyType: propertyType,
schema: schema,
}
}
func (entity *tdlibTypeProperty) IsList() bool {
return strings.HasPrefix(entity.propertyType, "vector<")
}
func (entity *tdlibTypeProperty) GetPrimitive() string {
primitive := entity.propertyType
for strings.HasPrefix(primitive, "vector<") {
primitive = strings.TrimSuffix(strings.TrimPrefix(primitive, "vector<"), ">")
}
return primitive
}
func (entity *tdlibTypeProperty) IsType() bool {
primitive := entity.GetPrimitive()
return isType(primitive, func(entity *tlparser.Type) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibTypeProperty) GetType() *tdlibType {
primitive := entity.GetPrimitive()
return getType(primitive, func(entity *tlparser.Type) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibTypeProperty) IsClass() bool {
primitive := entity.GetPrimitive()
return isClass(primitive, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibTypeProperty) GetClass() *tdlibClass {
primitive := entity.GetPrimitive()
return getClass(primitive, func(entity *tlparser.Class) string {
return entity.Name
}, entity.schema)
}
func (entity *tdlibTypeProperty) ToGoName() string {
return firstUpper(underscoreToCamelCase(entity.name))
}
func (entity *tdlibTypeProperty) ToGoFunctionPropertyName() string {
name := firstLower(underscoreToCamelCase(entity.name))
if name == "type" {
name += "Param"
}
return name
}
func (entity *tdlibTypeProperty) ToGoType() string {
tdlibType := entity.propertyType
goType := ""
for strings.HasPrefix(tdlibType, "vector<") {
goType = goType + "[]"
tdlibType = strings.TrimSuffix(strings.TrimPrefix(tdlibType, "vector<"), ">")
}
if entity.IsClass() {
return goType + entity.GetClass().ToGoType()
}
if entity.GetType().IsInternal() {
return goType + entity.GetType().ToGoType()
}
return goType + "*" + entity.GetType().ToGoType()
}
func isType(name string, field func(entity *tlparser.Type) string, schema *tlparser.Schema) bool {
name = normalizeEntityName(name)
for _, entity := range schema.Types {
if name == field(entity) {
return true
}
}
return false
}
func getType(name string, field func(entity *tlparser.Type) string, schema *tlparser.Schema) *tdlibType {
name = normalizeEntityName(name)
for _, entity := range schema.Types {
if name == field(entity) {
return TdlibType(entity.Name, schema)
}
}
return nil
}
func isClass(name string, field func(entity *tlparser.Class) string, schema *tlparser.Schema) bool {
name = normalizeEntityName(name)
for _, entity := range schema.Classes {
if name == field(entity) {
return true
}
}
return false
}
func getClass(name string, field func(entity *tlparser.Class) string, schema *tlparser.Schema) *tdlibClass {
name = normalizeEntityName(name)
for _, entity := range schema.Classes {
if name == field(entity) {
return TdlibClass(entity.Name, schema)
}
}
return nil
}
func normalizeEntityName(name string) string {
if name == "Bool" {
name = "boolFalse"
}
return name
}

176
codegen/type.go Normal file
View file

@ -0,0 +1,176 @@
package codegen
import (
"bytes"
"fmt"
"github.com/zelenin/go-tdlib/tlparser"
)
func GenerateTypes(schema *tlparser.Schema, packageName string) []byte {
buf := bytes.NewBufferString("")
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
buf.WriteString(`import (
"encoding/json"
)
`)
buf.WriteString("const (\n")
for _, entity := range schema.Classes {
tdlibClass := TdlibClass(entity.Name, schema)
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibClass.ToClassConst(), entity.Name))
}
for _, entity := range schema.Types {
tdlibType := TdlibType(entity.Name, schema)
if tdlibType.IsInternal() || tdlibType.HasClass() {
continue
}
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibType.ToClassConst(), entity.Class))
}
buf.WriteString(")")
buf.WriteString("\n\n")
buf.WriteString("const (\n")
for _, entity := range schema.Types {
tdlibType := TdlibType(entity.Name, schema)
if tdlibType.IsInternal() {
continue
}
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibType.ToTypeConst(), entity.Name))
}
buf.WriteString(")")
buf.WriteString("\n\n")
for _, class := range schema.Classes {
tdlibClass := TdlibClass(class.Name, schema)
buf.WriteString(fmt.Sprintf(`// %s
type %s interface {
%sType() string
}
`, class.Description, tdlibClass.ToGoType(), tdlibClass.ToGoType()))
}
for _, typ := range schema.Types {
tdlibType := TdlibType(typ.Name, schema)
if tdlibType.IsInternal() {
continue
}
buf.WriteString("// " + typ.Description + "\n")
if len(typ.Properties) > 0 {
buf.WriteString(`type ` + tdlibType.ToGoType() + ` struct {
meta
`)
for _, property := range typ.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
buf.WriteString(fmt.Sprintf(" // %s\n", property.Description))
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), property.Name))
}
buf.WriteString("}\n\n")
} else {
buf.WriteString(`type ` + tdlibType.ToGoType() + ` struct{
meta
}
`)
}
buf.WriteString(fmt.Sprintf(`func (entity *%s) MarshalJSON() ([]byte, error) {
entity.meta.Type = entity.GetType()
type stub %s
return json.Marshal((*stub)(entity))
}
`, tdlibType.ToGoType(), tdlibType.ToGoType()))
buf.WriteString(fmt.Sprintf(`func (*%s) GetClass() string {
return %s
}
func (*%s) GetType() string {
return %s
}
`, tdlibType.ToGoType(), tdlibType.ToClassConst(), tdlibType.ToGoType(), tdlibType.ToTypeConst()))
if tdlibType.HasClass() {
tdlibClass := TdlibClass(tdlibType.GetClass().Name, schema)
buf.WriteString(fmt.Sprintf(`func (*%s) %sType() string {
return %s
}
`, tdlibType.ToGoType(