Created
December 19, 2024 16:17
-
-
Save jerrylususu/dfb0f8c0507b371f716305de1fe47238 to your computer and use it in GitHub Desktop.
protobuf js magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createWithNestedDefaults = (messageType) => { | |
const message = messageType.create(); | |
console.log(message); | |
// 遍历消息的属性 | |
for (const [key, field] of Object.entries(messageType.fields)) { | |
// 检查该属性是否是嵌套的消息类型 | |
console.log(field) | |
console.log(Boolean(field), Boolean(field.resolvedType), Boolean(field.resolvedType instanceof protobuf.Type)); | |
if (field && field.resolvedType && field.resolvedType instanceof protobuf.Type) { | |
// 如果该属性是 null,则创建一个新的嵌套对象 | |
if (message[key] === null) { | |
console.log("need create"); | |
message[key] = createWithNestedDefaults(field.resolvedType); | |
} | |
} | |
} | |
return message; | |
} | |
b = createWithNestedDefaults(a.$type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment