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}';`)
@ -105,6 +108,9 @@ function writeNamespace(nsType) {
)}` )}`
) )
} }
} else {
if (is_mt_message && arg.name === 'body') {
write('ret.body = this.raw(ret.bytes)')
} else { } else {
write( write(
`ret.${ `ret.${
@ -116,6 +122,7 @@ function writeNamespace(nsType) {
)};` )};`
) )
} }
}
}) })
write('return ret;') write('return ret;')

View file

@ -120,6 +120,9 @@ function writeNamespace(nsType) {
} else { } else {
write(`_assert_has(obj, '${arg.name}')`) write(`_assert_has(obj, '${arg.name}')`)
} }
if (is_mt_message && arg.name === 'body') {
write('this.raw(obj.body)')
} else {
write( write(
`this.${getFunctionCallByTypeName( `this.${getFunctionCallByTypeName(
baseTypePrefix, baseTypePrefix,
@ -127,6 +130,7 @@ function writeNamespace(nsType) {
`obj.${arg.name}` `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)
if (is_mt_message && arg.name === 'body') {
ts.write('readonly body: Buffer;')
} else {
ts.write( ts.write(
`readonly ${arg.name}${ `readonly ${arg.name}${
arg.optional ? '?' : '' arg.optional ? '?' : ''
}: ${fullTypeName(arg.type)};` }: ${fullTypeName(arg.type)};`
) )
}
}) })
} }