tljson

package
v0.167.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tljson is a schema-driven generic TL decoder: it decodes binary TL into ordered JSON objects using api.tl and mtproto.tl parsed with github.com/gotd/tl, so it works for any layer without generated code.

It handles flags (including flags2 and true bits), boxed and bare vectors, strings and bytes, int128/int256, double, generic queries (invokeWithLayer, initConnection, invokeAfterMsg, ...), rpc_result with the result type resolved by req_msg_id, msg_container, gzip_packed, the tracer redaction marker and unknown constructors (partial output).

Output values: "_" is the constructor or function name, TL long is a decimal string, bytes are base64, int128/int256 are hex.

Index

Constants

View Source
const (
	DefaultMaxDepth = 64
	// MaxUnpacked limits the size of inflated gzip_packed data.
	MaxUnpacked = 32 << 20
)

Limits of the decoder.

View Source
const (
	IDVector       uint32 = 0x1cb5c415
	IDRPCResult    uint32 = 0xf35c6d01
	IDRPCError     uint32 = 0x2144ca19
	IDMsgContainer uint32 = 0x73f1f8dc
	IDGzipPacked   uint32 = 0x3072cfa1
	IDMsgCopy      uint32 = 0xe06046b2
	IDBoolTrue     uint32 = 0x997275b5
	IDBoolFalse    uint32 = 0xbc799737
)

Well-known constructor ids that are parsed manually (they are commented out or special in the schema).

View Source
const RedactedMarker uint32 = 0x7EDAC7ED

RedactedMarker follows the constructor id of an object whose body was redacted by the tracer: [id]RedactedMarker[id][zeros...].

Variables

View Source
var SchemaFiles = []string{"api.tl", "mtproto.tl"}

SchemaFiles are the file names LoadDir looks for.

Functions

func TypeName

func TypeName(t tl.Type) string

TypeName returns the full name of a type, without generic arguments.

func UnknownName

func UnknownName(id uint32) string

UnknownName formats an unknown constructor id.

Types

type Bytes

type Bytes []byte

Bytes is a TL bytes value, encoded as base64.

type Decoder

type Decoder struct {
	Schema *Schema
	// MaxBytes replaces TL bytes values longer than this with
	// {"_":"bytes","len":N,"sha256":"<first 8 bytes>"}. Zero means no limit.
	MaxBytes int
	// MaxDepth limits nesting, DefaultMaxDepth if zero.
	MaxDepth int
	// ResultType resolves the result type of rpc_result by req_msg_id (the
	// return type of the request method). If it returns false, the result
	// is decoded as a boxed Object.
	ResultType func(reqMsgID int64) (tl.Type, bool)
}

Decoder decodes TL binary data into Objects using a Schema.

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte, function bool) (any, error)

Decode decodes a boxed object (function or constructor) from data.

If function is true, function ids take precedence over constructor ids (outgoing messages).

func (*Decoder) DecodeAs

func (d *Decoder) DecodeAs(data []byte, t tl.Type) (any, error)

DecodeAs decodes a value of type t (boxed or bare) from data.

type Def

type Def struct {
	// Name is the full name, with namespace: "messages.getDialogs".
	Name     string
	ID       uint32
	Params   []tl.Parameter
	Type     tl.Type
	Function bool
	// Generic is the set of generic parameter names ({X:Type}).
	Generic []string
}

Def is a schema definition (constructor or function).

func (*Def) GenericParam

func (d *Def) GenericParam() (string, bool)

GenericParam returns the name of the `!X` parameter of a generic function (the wrapped query of invokeWithLayer, initConnection, ...).

type Error

type Error struct {
	// Unknown is the unknown constructor id that stopped decoding, if any.
	Unknown uint32
	Msg     string
}

Error is returned when decoding stopped before the end of the data. The returned value is still usable and is marked "partial":true.

func (*Error) Error

func (e *Error) Error() string

type Field

type Field struct {
	Key   string
	Value any
}

Field is a key-value pair of an Object.

type Long

type Long int64

Long is a TL long, encoded as a decimal JSON string to avoid float loss.

func (Long) MarshalJSON

func (l Long) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Object

type Object struct {
	Fields []Field
}

Object is a decoded TL object with ordered fields. The "_" field holds the constructor or function name.

Values are: int32 (TL int), uint32 (flags), Long (TL long), float64, string, Bytes, bool, []any (vectors) and *Object.

func NewObject

func NewObject(name string) *Object

NewObject creates an object with the "_" field set to name.

func (*Object) Get

func (o *Object) Get(key string) any

Get returns the value of a field, or nil.

func (*Object) MarshalJSON

func (o *Object) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, keeping the field order.

func (*Object) Name

func (o *Object) Name() string

Name returns the "_" field.

func (*Object) Set

func (o *Object) Set(key string, value any)

Set sets a field, replacing the existing value.

type Schema

type Schema struct {
	// Layer is the "// LAYER N" annotation, if present.
	Layer int
	// Files are the sha256 (hex) of the parsed sources, keyed by base name.
	Files map[string]string
	// Skipped counts definition lines the parser could not handle (built-in
	// definitions like `int128 4*[ int ] = Int128`).
	Skipped int
	// contains filtered or unexported fields
}

Schema is a set of TL definitions indexed for decoding.

func LoadDir

func LoadDir(dir string) (*Schema, error)

LoadDir loads api.tl and mtproto.tl from dir.

func LoadFiles

func LoadFiles(paths ...string) (*Schema, error)

LoadFiles parses TL files (e.g. api.tl and mtproto.tl) into one schema.

func NewSchema

func NewSchema() *Schema

NewSchema returns an empty schema.

func (*Schema) Add

func (s *Schema) Add(d *Def)

Add adds a definition, replacing the previous one with the same id.

func (*Schema) Constructor

func (s *Schema) Constructor(id uint32) (*Def, bool)

Constructor returns a constructor by id.

func (*Schema) Function

func (s *Schema) Function(id uint32) (*Def, bool)

Function returns a function by id.

func (*Schema) FunctionByName

func (s *Schema) FunctionByName(name string) (*Def, bool)

FunctionByName returns a function by full name.

func (*Schema) Lookup

func (s *Schema) Lookup(id uint32, function bool) (*Def, bool)

Lookup returns a constructor or a function by id, preferring functions if function is true.

func (*Schema) Name

func (s *Schema) Name(id uint32) string

Name returns the name of a constructor or function id, or "#xxxxxxxx".

func (*Schema) Parse

func (s *Schema) Parse(r io.Reader) error

Parse adds definitions from a TL source.

Parsing is line based, like tl.Parse, but lines that tl.Definition can't parse are skipped (and counted) instead of failing the whole file: the tdesktop mtproto.tl has built-in definitions like `int128 4*[ int ]`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL