Comments (5)
@gabrielerzinger, I am assuming you are getting an error when you try to create the descriptor (not when creating the message), right?
IIUC, you are trying to use desc.CreateFileDescriptor(...)
and are supplying the descriptor proto for proto A. Is that right? If so, take a close look at the signature for desc.CreateFileDescriptor
: it accepts both the descriptor proto and all of its dependencies (which must already be upgraded to "rich" desc.FileDescriptor
instances).
To make it more concrete, let's assume we have the following situation:
- We want a descriptor for
file-a.proto
. - This file imports
file-b.proto
andfile-c.proto
. - The lattermost file, c, imports
file-d.proto
.
In order to create a descriptor for a, we have to construct descriptors for its dependencies first (in reverse topological order: leaves first).
So the following sequence would be necessary for the given example:
// for brevity, omitting error handling (but real code should
// always check the error returned)
fdD, _ := desc.CreateFileDescriptor(fileDProto)
fdC, _ := desc.CreateDescriptor(fileCProto, fdD)
fdB, _ := desc.CreateDescriptor(fileBProto)
fdA, _ := desc.CreateDescriptor(fileAProto, fdB, fdC)
So as you can see, we had to supply the dependencies in that last call, when creating a descriptor for file-a.proto
.
from protoreflect.
If you have the fully-qualified name of the message, this should be easy:
md, err := desc.LoadMessageDescriptor(msgName)
If that isn't working, I suspect the error message is different.
The most common issue is when you've compiled your protos (to .pb.go
files) in a way that the path you supplied to protoc
differs from the path you use to import the proto from other files. You can read more about this issue here: golang/protobuf#567. There's also an existing issue in this library to provide API that allows users to work-around these issues (#147).
To resolve, you will need to modify your invocations of protoc
such that the path you supply to the source protos is identical to how you then import those same files from others. For example, if you have a file foo/bar/baz.proto
and it has an import statement for foo/bar/snafu.proto
, then you should make sure that the invocation of the latter uses foo/bar/snafu.proto
as the argument to protoc
(and adjust proto path arguments and/or PWD for the invocation so it works).
from protoreflect.
This trully should be added to golang/protobuff sometime
Most of the functionality in this repo will become part of the core runtime. Here's a design doc for v2 API for the core protobuf library, and here's the in-progress work.
sorry if it was not appropriate for an issue :)
No worries!
from protoreflect.
First of all, thank you very much for the quick response. Do you think there is any way I could make it so I could extract the dependencies from .pb.go
? Because what I have right now, is the ability to, given a proto message name, retrieve it's descriptor (as follow) and build a dynamic message from it (not a big deal, simply using your lib and reflection):
[...]
protoReflectTypePointer := proto.MessageType(protoName)
protoReflectType := protoReflectTypePointer.Elem()
protoValue := reflect.New(protoReflectType)
descriptorMethod, ok := protoReflectTypePointer.MethodByName("Descriptor")
descriptorValue := descriptorMethod.Func.Call([]reflect.Value{protoValue})
protoDescriptor := descriptorValue[0].Bytes()
return protoDescriptor
If it were possible to get those dependencies from .pb.go
, this method could work for any proto message. But maybe this isnt the right place to ask this question, unless you think this would be a good feature for the lib, and I could try to do it and make a PR. What are your thoughts on that?
Again, tyvm for the attention
from protoreflect.
The most common issue is when you've compiled your protos (to
.pb.go
files) in a way that the path you supplied toprotoc
differs from the path you use to import the proto from other files. You can read more about this issue here: [golang/protobuf#567]
I've runned some pre-tests and Im almost sure that was the problem! Thank you very much and thank you for the great work with this lib. (This trully should be added to golang/protobuff sometime)!
I think we can close this and sorry if it was not appropriate for an issue :)
from protoreflect.
Related Issues (20)
- panic: invalid type: got *dynamicpb.Message when GetExtension HOT 6
- can not FindMessage from linked files HOT 2
- Breaking changes in the protocompile/ast dependency
- EnumBuilder panics if it contains EnumValue with explicitly set Number HOT 1
- might not be bug: false duplication report due to use of relative path instead of absolute path HOT 6
- SIGSEGV: panic: runtime error: invalid memory address or nil pointer dereference in v1.15.2 HOT 8
- Protoreflect doesn't fall back to to v1alpha when a gRPC unimplemented response is returned HOT 1
- String escaping in protoprint is wrong HOT 1
- First enum value must be 0 in proto3 [protoprint] HOT 2
- missing `{}` after printing option HOT 5
- Upgrade protocompile to v0.7.0 HOT 3
- go build error HOT 3
- Regression upgrading from v1.14.1 to v1.15.4: extensions are resolved recursively instead of non-recursively HOT 1
- Regression upgrading from v1.14.1 to v1.15.4: absolute paths no longer accepted by parser.ParseFilesButDoNotLink HOT 3
- Regression upgrading from v1.14.1 to v1.15.4: new mustBeSource constraint/check HOT 5
- Stub structure and Methods will relay on protobuf API V2 HOT 15
- Fail to compile proto file HOT 2
- Tests broken with google.golang.org/protobuf v1.33.0
- If there are messages nested in the proto file, the numbers will be recognised as strings HOT 2
- invalid memory address or nil pointer dereference HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protoreflect.