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
- Variables
- func TypeName(t tl.Type) string
- func UnknownName(id uint32) string
- type Bytes
- type Decoder
- type Def
- type Error
- type Field
- type Long
- type Object
- type Schema
- func (s *Schema) Add(d *Def)
- func (s *Schema) Constructor(id uint32) (*Def, bool)
- func (s *Schema) Function(id uint32) (*Def, bool)
- func (s *Schema) FunctionByName(name string) (*Def, bool)
- func (s *Schema) Lookup(id uint32, function bool) (*Def, bool)
- func (s *Schema) Name(id uint32) string
- func (s *Schema) Parse(r io.Reader) error
Constants ¶
const ( DefaultMaxDepth = 64 // MaxUnpacked limits the size of inflated gzip_packed data. MaxUnpacked = 32 << 20 )
Limits of the decoder.
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).
const RedactedMarker uint32 = 0x7EDAC7ED
RedactedMarker follows the constructor id of an object whose body was redacted by the tracer: [id]RedactedMarker[id][zeros...].
Variables ¶
var SchemaFiles = []string{"api.tl", "mtproto.tl"}
SchemaFiles are the file names LoadDir looks for.
Functions ¶
func UnknownName ¶
UnknownName formats an unknown constructor id.
Types ¶
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.
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 ¶
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.
type Long ¶
type Long int64
Long is a TL long, encoded as a decimal JSON string to avoid float loss.
func (Long) MarshalJSON ¶
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 (*Object) MarshalJSON ¶
MarshalJSON implements json.Marshaler, keeping the field order.
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 (*Schema) Constructor ¶
Constructor returns a constructor by id.
func (*Schema) FunctionByName ¶
FunctionByName returns a function by full name.
func (*Schema) Lookup ¶
Lookup returns a constructor or a function by id, preferring functions if function is true.