fix(core): fix bare types and mt_message writing

This commit is contained in:
teidesu 2021-08-19 23:09:44 +03:00
parent 9b5ca0cb2a
commit 49eda8f0e3
5 changed files with 44 additions and 26 deletions

View file

@ -176,7 +176,7 @@ export class SerializationCounter implements ITlBinaryWriter {
vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void { vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void {
this.count += bare ? 4 : 8 this.count += bare ? 4 : 8
items.forEach((it) => fn.call(this, it)) items.forEach((it) => fn.call(this, it, bare))
} }
} }
@ -301,11 +301,11 @@ export class BinaryWriter implements ITlBinaryWriter {
this._objectMap[obj._].call(this, obj, bare) this._objectMap[obj._].call(this, obj, bare)
} }
vector(fn: TlBinaryWriterFunction, val: unknown[]): void { vector(fn: TlBinaryWriterFunction, val: unknown[], bare?: boolean): void {
this.uint32(0x1cb5c415) if (!bare) this.uint32(0x1cb5c415)
this.uint32(val.length) this.uint32(val.length)
val.forEach((it) => fn.call(this, it)) val.forEach((it) => fn.call(this, it, bare))
} }
result(): Buffer { result(): Buffer {

View file

@ -50,7 +50,7 @@ export interface ITlBinaryWriter {
* *
* @param fn Writer function * @param fn Writer function
* @param items Items to be written * @param items Items to be written
* @param bare Whether the vector is bare (i.e. vector ID should not be written) * @param bare Whether the vector is bare (i.e. object ID should not be written)
*/ */
vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void
} }

View file

@ -80,6 +80,9 @@ function writeNamespace(nsType) {
write(`${cls.id}: function () {`) write(`${cls.id}: function () {`)
tab() tab()
let is_mt_message =
nsType === 'mtproto' && cls.name === 'message'
if (cls.arguments?.length) { if (cls.arguments?.length) {
write(`var ret = {};`) write(`var ret = {};`)
write(`ret._ = '${prefix}${cls.name}';`) write(`ret._ = '${prefix}${cls.name}';`)
@ -106,15 +109,19 @@ function writeNamespace(nsType) {
) )
} }
} else { } else {
write( if (is_mt_message && arg.name === 'body') {
`ret.${ write('ret.body = this.raw(ret.bytes)')
arg.name } else {
} = this.${getFunctionCallByTypeName( write(
baseTypePrefix, `ret.${
arg.type, arg.name
arg } = this.${getFunctionCallByTypeName(
)};` baseTypePrefix,
) arg.type,
arg
)};`
)
}
} }
}) })

View file

@ -120,13 +120,17 @@ function writeNamespace(nsType) {
} else { } else {
write(`_assert_has(obj, '${arg.name}')`) write(`_assert_has(obj, '${arg.name}')`)
} }
write( if (is_mt_message && arg.name === 'body') {
`this.${getFunctionCallByTypeName( write('this.raw(obj.body)')
baseTypePrefix, } else {
arg.type, write(
`obj.${arg.name}` `this.${getFunctionCallByTypeName(
)}` baseTypePrefix,
) arg.type,
`obj.${arg.name}`
)}`
)
}
if (arg.optional) { if (arg.optional) {
untab() untab()
} }

View file

@ -162,6 +162,9 @@ const writeSingleSchemaEntry = (type) => {
ts.tab() ts.tab()
ts.write(`readonly _: '${prefix}${cls.name}',`) ts.write(`readonly _: '${prefix}${cls.name}',`)
let is_mt_message =
type === 'mtproto' && cls.name === 'message'
if (cls.arguments && cls.arguments.length) { if (cls.arguments && cls.arguments.length) {
cls.arguments.forEach((arg) => { cls.arguments.forEach((arg) => {
if (arg.type === '$FlagsBitField') return if (arg.type === '$FlagsBitField') return
@ -170,11 +173,15 @@ const writeSingleSchemaEntry = (type) => {
arg.type = 'boolean' arg.type = 'boolean'
if (arg.description) ts.comment(arg.description) if (arg.description) ts.comment(arg.description)
ts.write( if (is_mt_message && arg.name === 'body') {
`readonly ${arg.name}${ ts.write('readonly body: Buffer;')
arg.optional ? '?' : '' } else {
}: ${fullTypeName(arg.type)};` ts.write(
) `readonly ${arg.name}${
arg.optional ? '?' : ''
}: ${fullTypeName(arg.type)};`
)
}
}) })
} }