Skip to content

Commit 842a788

Browse files
authored
Merge branch 'tulir:main' into main
2 parents 4bea1dc + 329c233 commit 842a788

16 files changed

+2135
-987
lines changed

proto/parse-proto.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ function flattenWithBlankLines(...items) {
260260
.flatMap(item => item)
261261
}
262262

263-
function protoifyChildren(container) {
263+
function protoifyChildren(container, proto3) {
264264
return flattenWithBlankLines(
265265
...Object.values(container.enums).map(protoifyEnum),
266-
...Object.values(container.messages).map(protoifyMessage),
266+
...Object.values(container.messages).map(msg => protoifyMessage(msg, proto3)),
267267
)
268268
}
269269

@@ -289,6 +289,14 @@ function protoifyEnum(enumDef) {
289289

290290
const {TYPES, TYPE_MASK, FLAGS} = requireModule("WAProtoConst")
291291

292+
function mapFieldTypeName(ref, parentModule, parentPath) {
293+
if (typeof ref === "object") {
294+
return fieldTypeName(TYPES.MESSAGE, ref, parentModule, parentPath)
295+
} else {
296+
return fieldTypeName(ref, undefined, parentModule, parentPath)
297+
}
298+
}
299+
292300
function fieldTypeName(typeID, typeRef, parentModule, parentPath) {
293301
switch (typeID) {
294302
case TYPES.INT32:
@@ -323,6 +331,8 @@ function fieldTypeName(typeID, typeRef, parentModule, parentPath) {
323331
namePath.push(...typeRef.__path__.slice(pathStartIndex))
324332
namePath.push(typeRef.__name__)
325333
return namePath.join(".")
334+
case TYPES.MAP:
335+
return `map<${mapFieldTypeName(typeRef[0], parentModule, parentPath)}, ${mapFieldTypeName(typeRef[1], parentModule, parentPath)}>`
326336
case TYPES.FIXED64:
327337
return "fixed64"
328338
case TYPES.SFIXED64:
@@ -374,16 +384,19 @@ function fixFieldName(name) {
374384
.replace("Sha256", "SHA256")
375385
}
376386

377-
function protoifyField(name, [index, flags, typeRef], parentModule, parentPath, isOneOf) {
387+
function protoifyField(name, [index, flags, typeRef], parentModule, parentPath, isOneOf, proto3) {
378388
const preflags = []
379389
const postflags = [""]
390+
const isMap = (flags & TYPE_MASK) === TYPES.MAP
380391
if (!isOneOf) {
381392
if ((flags & FLAGS.REPEATED) !== 0) {
382393
preflags.push("repeated")
383-
} else if ((flags & FLAGS.REQUIRED) === 0) {
384-
preflags.push("optional")
385-
} else {
386-
preflags.push("required")
394+
} else if (!proto3) {
395+
if ((flags & FLAGS.REQUIRED) === 0) {
396+
preflags.push("optional")
397+
} else {
398+
preflags.push("required")
399+
}
387400
}
388401
}
389402
preflags.push(fieldTypeName(flags & TYPE_MASK, typeRef, parentModule, parentPath))
@@ -393,12 +406,12 @@ function protoifyField(name, [index, flags, typeRef], parentModule, parentPath,
393406
return `${preflags.join(" ")} ${fixFieldName(name)} = ${index}${postflags.join(" ")};`
394407
}
395408

396-
function protoifyFields(fields, parentModule, parentPath, isOneOf) {
397-
return Object.entries(fields).map(([name, definition]) => protoifyField(name, definition, parentModule, parentPath, isOneOf))
409+
function protoifyFields(fields, parentModule, parentPath, isOneOf, proto3) {
410+
return Object.entries(fields).map(([name, definition]) => protoifyField(name, definition, parentModule, parentPath, isOneOf, proto3))
398411
}
399412

400-
function protoifyMessage(message) {
401-
const sections = [protoifyChildren(message)]
413+
function protoifyMessage(message, proto3) {
414+
const sections = [protoifyChildren(message, proto3)]
402415
const spec = message.message
403416
const fullMessagePath = message.__path__.concat([message.__name__])
404417
for (const [name, fieldNames] of Object.entries(spec.__oneofs__ ?? {})) {
@@ -407,24 +420,33 @@ function protoifyMessage(message) {
407420
delete spec[fieldName]
408421
return [fieldName, def]
409422
}))
410-
sections.push([`oneof ${name} ` + "{", ...indent(protoifyFields(fields, message.__module__, fullMessagePath, true)), "}"])
423+
sections.push([`oneof ${name} ` + "{", ...indent(protoifyFields(fields, message.__module__, fullMessagePath, true, proto3)), "}"])
411424
}
412425
if (spec.__reserved__) {
413426
console.warn("Found reserved keys:", message.__name__, spec.__reserved__)
414427
}
415428
delete spec.__oneofs__
416429
delete spec.__reserved__
417-
sections.push(protoifyFields(spec, message.__module__, fullMessagePath, false))
430+
sections.push(protoifyFields(spec, message.__module__, fullMessagePath, false, proto3))
418431
return [`message ${message.__name__} ` + "{", ...indent(flattenWithBlankLines(...sections)), "}"]
419432
}
420433

421434
function goPackageName(name) {
422435
return name.replace(/^WA/, "wa").replace("WebProtobufs", "").replace("Protobufs", "")
423436
}
424437

438+
const needsProto3 = {
439+
"WAWebProtobufsReporting": true
440+
}
441+
425442
function protoifyModule(module) {
426443
const output = []
427-
output.push(`syntax = "proto2";`)
444+
const proto3 = needsProto3[module.__name__]
445+
if (proto3) {
446+
output.push(`syntax = "proto3";`)
447+
} else {
448+
output.push(`syntax = "proto2";`)
449+
}
428450
output.push(`package ${module.__name__};`)
429451
output.push(`option go_package = "go.mau.fi/whatsmeow/proto/${goPackageName(module.__name__)}";`)
430452
output.push("")
@@ -434,7 +456,7 @@ function protoifyModule(module) {
434456
}
435457
output.push("")
436458
}
437-
const children = protoifyChildren(module)
459+
const children = protoifyChildren(module, proto3)
438460
children.push("")
439461
return output.concat(children)
440462
}

0 commit comments

Comments
 (0)