Go导入范围查找protobuf类型

huangapple go评论78阅读模式
英文:

Go import scope lookup for protobuf types

问题

我正在构建一个转译器,需要了解protobuf/go的作用域查找系统。我一直在尝试在Google上查找文档,但没有找到什么有用的信息。

问题:在Go/protobuf中,是否有一种共享的包作用域查找方式,可以在导入类型时使用?

以下是我对此问题的疑问的示例:

proto1:

package cosmos.crypto.keyring.v1;
...

message Ledger {
    hd.v1.BIP44Params path = 1;
}

proto2:

package cosmos.crypto.hd.v1;
message BIP44Params {
    ...
}

到目前为止,我看到了两种有意义的语法:

完全作用域

message Ledger {
    cosmos.crypto.hd.v1.BIP44Params path = 1;
}

或者我也看到了这样的版本

完全无作用域

message Ledger {
   BIP44Params path = 1;
}

部分作用域?

但是我看到的风格是部分作用域的

message Ledger {
    hd.v1.BIP44Params path = 1;
}

他们省略cosmos.crypto的原因是因为这两个包在其包名的根目录中共享了cosmos.crypto吗?

还是基于导入的更通用的作用域查找?

任何见解或阅读链接都会很有帮助 Go导入范围查找protobuf类型

英文:

I'm building a transpiler and need to understand the protobuf/go scope lookup system. I've been trying to google the docs and finding no luck.

Q: Is there a shared package scope lookup that you can do when importing Types in Go/protobufs?

Here is the example that I'm questioning:

proto1:

package cosmos.crypto.keyring.v1;
...

message Ledger {
    hd.v1.BIP44Params path = 1;
}

proto2:

package cosmos.crypto.hd.v1;
message BIP44Params {
    ...
}

There are two syntaxes I've seen that do make sense so far:

full scope

message Ledger {
    cosmos.crypto.hd.v1.BIP44Params path = 1;
}

Or I’ve also seen versions like this

completely unscoped

message Ledger {
   BIP44Params path = 1;
}

partially scoped?

But the style I'm seeing is partially scoped

message Ledger {
    hd.v1.BIP44Params path = 1;
}

Is the reason they leave off the cosmos.crypto because these two packages share cosmos.crypto in the root of their package name?

Or is it a more generic scope lookup based on the import?

Any insight or reading links appreciated Go导入范围查找protobuf类型

答案1

得分: 1

我不确定我完全理解问题,但我会尝试回答。如果需要更改,请告诉我。

这是两者的结合。你需要拥有该包并导入.proto文件。让我解释一下。如果你有两个文件定义如下:

proto1.proto

syntax = "proto3";
package cosmos.crypto.keyring.v1;

message Ledger {
    hd.v1.BIP44Params path = 1;
}

proto2.proto

syntax = "proto3";
package cosmos.crypto.hd.v1;

message BIP44Params {}

尝试编译会告诉你"hd.v1.BIP44Params"未定义。这是因为proto1.proto不知道其他定义。现在,如果你在proto1.proto中导入"proto2.proto",它将知道BIP44Params的定义,并注意到包的定义。

有了这个包定义,它将能够访问以下类型定义:

  • cosmos.crypto.hd.v1.BIP44Params - 这是很容易理解的
  • hd.v1.BIP44Params - 因为两个包在hd部分之前匹配

但它应该能够访问:

  • BIP44Params - 因为在cosmos.crypto.keyring.v1包中没有定义这样的类型

希望这样清楚了。

英文:

I'm not sure I fully get the question but I will try to answer. Let me know if you need me to change that.

This is a combination of both. You need to have the package and import the .proto file. Let me explain. If you have two file define like this:

proto1.proto

syntax = "proto3";
package cosmos.crypto.keyring.v1;

message Ledger {
    hd.v1.BIP44Params path = 1;
}

proto2.proto

syntax = "proto3";
package cosmos.crypto.hd.v1;

message BIP44Params {}

trying to compile will tell you that "hd.v1.BIP44Params" is not defined. This is because the proto1.proto is not aware of other definitions. Now, if you import "proto2.proto"; in the proto1.proto, it will be aware of the BIP44Params definition and will notice the package definition.

With this package definition, it will be able to access the following type definition:

  • cosmos.crypto.hd.v1.BIP44Params - which is pretty self explanatory
  • hd.v1.BIP44Params - because the two package matches before the hd part.

but it should be able to access:

  • BIP44Params - because there is no such type defined in cosmos.crypto.keyring.v1 package

Hope that's clear

huangapple
  • 本文由 发表于 2022年5月14日 01:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/72233302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定