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

protoc.exe编译proto文件失败 #1

Open
yjshi2015 opened this issue Jul 19, 2022 · 3 comments
Open

protoc.exe编译proto文件失败 #1

yjshi2015 opened this issue Jul 19, 2022 · 3 comments

Comments

@yjshi2015
Copy link

玺哥,在编写protobuf协议的demo时遇到了如下3个问题,麻烦有时间指导下,感谢!

  • ①编译之后给属性赋值时,属性名不能自动带出,也不能访问,所以即使拼写错误,在代码运行前是无法发现的;

  • ②如果属性是个对象如何赋值?(比如代码中的searchSort)
    1

  • ③在我升级了protoc.exe之后(3.21版本),再编译proto文件,结果只编译出了一半的内容,结果如下图,这是什么原因?
    image

@lixi5338619
Copy link
Owner

1、“属性赋值时,属性名不能自动带出,也不能访问”是指没有代码提示吗,确实没有,不过能根据属性点进去。
2、没有proto文件的截图,看不出来。 这里的对象是指map还是object ,可根据网站中的参数类型复写。
3、protoc和protobuf版本是否对应,也可能proto文件有问题。

@yjshi2015
Copy link
Author

1、“属性赋值时,属性名不能自动带出,也不能访问”是指没有代码提示吗,确实没有,不过能根据属性点进去。 2、没有proto文件的截图,看不出来。 这里的对象是指map还是object ,可根据网站中的参数类型复写。 3、protoc和protobuf版本是否对应,也可能proto文件有问题。


  • 1、对,是没有代码提示,只有SearchService对象有提示,它下个层级的SearchRequest/Commonrequest以及它们的子属性都没有,并且不能根据属性点进去。我用的是IDEA,插件是: Protocol Buffers ,你的代码,然而并没用,不知道是不是要换成Pycharm?
  • 2、searchSort是个对象,js中的proto描述内容如下:
       proto.SearchService.SearchSort.toObject = function(e, t) {
                  var a = {
                      field: r.Message.getFieldWithDefault(t, 1, ""),
                      order: r.Message.getFieldWithDefault(t, 2, 0)
                  };
                  return e && (a.$jspbMessageInstance = t),
                  a
              }
              ),
         proto.SearchService.SearchSort.serializeBinaryToWriter = function(e, t) {
            var a = void 0;
            a = e.getField(),
            a.length > 0 && t.writeString(1, a),
            a = e.getOrder(),
            0 !== a && t.writeEnum(2, a)
        }
        proto.SearchService.SearchSort.deserializeBinary = function(e) {
            var t = new r.BinaryReader(e)
              , a = new proto.SearchService.SearchSort;
            return proto.SearchService.SearchSort.deserializeBinaryFromReader(a, t)
        }
        ,
        proto.SearchService.SearchSort.deserializeBinaryFromReader = function(e, t) {
            while (t.nextField()) {
                if (t.isEndGroup())
                    break;
                var a = t.getFieldNumber();
                switch (a) {
                case 1:
                    var r = t.readString();
                    e.setField(r);
                    break;
                case 2:
                    r = t.readEnum();
                    e.setOrder(r);
                    break;
                default:
                    t.skipField();
                    break
                }
            }
            return e
        }
        ,
        proto.SearchService.SearchSort.prototype.serializeBinary = function() {
            var e = new r.BinaryWriter;
            return proto.SearchService.SearchSort.serializeBinaryToWriter(this, e),
            e.getResultBuffer()
        }

在python代码中,我是如下方式定义以及赋值的:

# ①定义如下
syntax = 'proto3';

message SearchService {

  message SearchSort {
    string field = 1;
    int32 order = 2;
  }

  message CommonRequest {
    string searchType = 1;
    string searchWord = 2;
    SearchSort searchSort = 3;
    #其他属性省略……
  }

  message SearchRequest {
    CommonRequest commonRequest = 1;
    int32 interfaceType = 2;
  }
}

# ②赋值逻辑
import request_pb2 as pb

search_request = pb.SearchService.SearchRequest()

#SearchSort赋值
search_sort = pb.SearchService.SearchSort()
search_sort.field = ""
search_sort.order = 1

search_request.commonRequest.searchType = "paper"
search_request.commonRequest.searchWord = "哔站"
#SearchSort赋值
search_request.commonRequest.searchSort = search_sort
bytes_body = search_request.SerializeToString()
print(bytes_body)
报错信息如下,所以这个searchSort属性应该怎么赋值?

Traceback (most recent call last): File "D:\idea_workspace\PythonDemo\com\syj\lx\chapter3\wanfang\WanfangPB.py", line 16, in <module> search_request.commonRequest.searchSort = search_sort AttributeError: Assignment not allowed to field "searchSort" in protocol message object.

  • 3、protoc.exe升级后的版本是win64下的3.21,protobuf包也升级到了最新版4.21.0,文件里syntax='proto3',不知道这个最新版的究竟是什么问题,编译总是一半,并且没有任何错误信息……

@lixi5338619
Copy link
Owner

pycharm专业版,没用插件。
Proto文件没有问题,试试这个写法。
`
search_sort = pb.SearchService.SearchSort()
search_sort.field = "1"
search_sort.order = 0

search_request = pb.SearchService.CommonRequest()
search_request.searchType = "paper"
search_request.searchWord = "111"
search_request.searchSort.CopyFrom(search_sort)
`
为啥只编译一半我也不清楚 = =

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

2 participants