Skip to content
/ tl Public

TL (Type Language) parser for telegram's mtproto

License

Notifications You must be signed in to change notification settings

gotd/tl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tl

PkgGoDev

Package tl implements TL (Type Language) schema parser and writer. Inspired by grammers parser.

Used by gotd/td in code generation pipeline.

go get github.com/gotd/tl

Parsing

This program parses schema from stdin and prints all definitions with their names and types.

package main

import (
	"fmt"
	"os"

	"github.com/gotd/tl"
)

func main() {
	schema, err := tl.Parse(os.Stdin)
	if err != nil {
		panic(err)
	}
	for _, d := range schema.Definitions {
		fmt.Printf("%s#%x = %s;\n", d.Definition.Name, d.Definition.ID, d.Definition.Type)
	}
}

You can use it like that:

$ curl -s "https://raw.githubusercontent.com/tdlib/td/master/td/generate/scheme/td_api.tl" \
    | go run github.com/gotd/tl/cmd/tl-print \
    | less

Output:

double#2210c154 = Double;
string#b5286e24 = String;
int32#5cb934fa = Int32;
int53#6781c7ee = Int53;
int64#5d9ed744 = Int64;
bytes#e937bb82 = Bytes;
boolFalse#bc799737 = Bool;
boolTrue#997275b5 = Bool;
error#9bdd8f1a = Error;
ok#d4edbe69 = Ok;
tdlibParameters#d29c1d7b = TdlibParameters;

//...

Generating

You can also generate .tl file from tl.Schema. Any WriteTo result is valid input for Parse.

package main

import (
	"os"

	"github.com/gotd/tl"
)

func main() {
	def := tl.Definition{
		Name: "error",
		Type: tl.Type{Name: "Error"},
		// Currently you should always pass explicit ID.
		ID: 0x9bdd8f1a,
		Params: []tl.Parameter{
			{
				Name: "code",
				Type: tl.Type{Name: "int32", Bare: true},
			},
			{
				Name: "message",
				Type: tl.Type{Name: "string", Bare: true},
			},
		},
	}
	_, _ = tl.Schema{
		Definitions: []tl.SchemaDefinition{
			{
				Category:   tl.CategoryType,
				Definition: def,
			},
		},
	}.WriteTo(os.Stdout)
}

Output

error#9bdd8f1a code:int32 message:string = Error;