Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help. What is the solution similar to protobuf.wrappers in rust-protobuf? #722

Open
ape-casear opened this issue Apr 30, 2024 · 0 comments

Comments

@ape-casear
Copy link

ape-casear commented Apr 30, 2024

background:
I have a message A that is used everywhere.

message A {
      u32 define_type = 1;
      bytes sub_message = 2;
}

and there is a map that maintains the relationship between id and sub_message.
problem:
when I use parse_from_bytes to get a message that contains message A. I want to get the real message behind the bytes sub_message.

The place where MessageA is used looks like this

// normal
message B {
      u32 foo = 1;
      A  a = 2;
}
// nested
message C {
      u32 foo = 1;
      B b = 2;
}

In js. protobuf.wrappers solves my problem.

const protobuf = require('protobufjs')
protobuf.wrappers['.pkg.A'] = {
    toObject(message, options) {
        const originOutput = this.toObject(message, options)
        if (map.has(originOutput.define_type)) {
            const message_type = map.get(originOutput.define_type;
            originOutput.sub_message = message_type.decode(Buffer.from(originOutput.sub_message, 'base64'))
        }
        return originOutput
    },
    fromObject(object) {
        return this.fromObject(object)
    },
}

What is the solution similar to protobuf.wrappers in rust-protobuf?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant