CNode

protobufjs对import 其它proto文件的处理问题

问答
Mmrluoming发布于10 年前最后回复10 年前5 回复12872 浏览0 收藏

前提: 1、定义了2个.proto文件 1)common.proto

package common;
 message item
{
required int32 index = 1;
required string score = 2;
}

2)lm.helloworld.proto(引用了common.proto)

import "common.proto";
package lm;
message hellounit
{
    required int32 index = 1;
    required string score = 2;
}
message helloworld 
{ 
   required int32     id = 1;  // ID 
   required string    str = 2;  // str
   repeated hellounit     helloArr = 3;  //optional field
}

3)使用pbjs命令:pbjs proto/lm.helloworld.proto -t commonjs > proto_to_js/lm.helloworld.js 生成对应的js代码:

module.exports = require("protobufjs").newBuilder({})['import']({
    "package": null,
    "messages": [
        {
            "name": "common",
            "fields": [],
            "messages": [
                {
                    "name": "item",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "index",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "score",
                            "id": 2
                        }
                    ]
                }
            ]
        },
        {
            "name": "lm",
            "fields": [],
            "messages": [
                {
                    "name": "hellounit",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "index",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "score",
                            "id": 2
                        }
                    ]
                },
                {
                    "name": "helloworld",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "id",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "str",
                            "id": 2
                        },
                        {
                            "rule": "repeated",
                            "type": "hellounit",
                            "name": "helloArr",
                            "id": 3
                        }
                    ]
                }
            ]
        }
    ]
}).build();

问题: 被引入的common.proto文件的内容在最终的js文件被展开了,如果common.proto文件有新的定义增加进来,则凡是引入该文件的proto对应的js代码都需要更新; 有没有办法让common.proto文件的内容在相关的js文件不被展开,而只是require 'common.js'就可以了,这样关联的js代码不需要更新。

查看回复

回复 (5)

A
alsotang#1·10 年前
M
mrluoming#2·10 年前
引用 alsotang换这个用? https://github.com/dcodeIO/protobuf.js

@alsotang 我就是基于这个库出现这个问题的

A
alsotang#3·10 年前
引用 mrluoming@alsotang 我就是基于这个库出现这个问题的

@mrluoming 用这个库的话,不用 pbjs 提前编译啊。不是有这个 api 可以当场编译吗? ProtoBuf.loadProtoFile("path/to/file.proto");

参与回复
登录后即可参与回复。登录