Comments (5)
@japersik, nice job finding all of the bugs in this package! This is indeed a tiny bug related to uninterpreted options, which is what you usually get when you're not linking (because the link step is needed for it to interpret most options, including all custom options).
Thanks for the repro case! I will use that to make a new test and can open a PR with the fix soon.
from protoreflect.
code exapmle
package main
import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"strings"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/desc/protoprint"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
//go:embed example.proto
var textProtoFile string
var parsedFileName = "example.proto"
func main() {
//create builtins map
var builtins = map[string]protoreflect.FileDescriptor{
"google/protobuf/descriptor.proto": descriptorpb.File_google_protobuf_descriptor_proto,
}
var builtInDeps = make(map[string]string)
{
var fds []*descriptorpb.FileDescriptorProto
for _, value := range builtins {
fd := protodesc.ToFileDescriptorProto(value)
fds = append(fds, fd)
}
fdMap, err := desc.CreateFileDescriptors(fds)
if err != nil {
panic(err)
}
printer := protoprint.Printer{OmitComments: protoprint.CommentsAll}
for key, value := range fdMap {
var writer strings.Builder
err = printer.PrintProtoFile(value, &writer)
if err != nil {
panic(err)
}
builtInDeps[key] = writer.String()
}
}
// parse
var p = protoparse.Parser{
ValidateUnlinkedFiles: true,
Accessor: func(filename string) (io.ReadCloser, error) {
var contents string
if filename == parsedFileName {
contents = textProtoFile
}
if contents == "" {
contents = builtInDeps[filename]
}
if contents == "" {
return nil, os.ErrNotExist
}
return io.NopCloser(strings.NewReader(contents)), nil
},
}
parsed, err := p.ParseFilesButDoNotLink(parsedFileName, "google/protobuf/descriptor.proto")
if err != nil {
panic(err)
}
var writer bytes.Buffer
var printer = protoprint.Printer{
SortElements: true,
OmitComments: protoprint.CommentsAll,
ForceFullyQualifiedNames: true,
}
files, err := protodesc.NewFiles(&descriptorpb.FileDescriptorSet{File: parsed})
if err != nil {
panic(err)
}
descriptorFile, err := files.FindFileByPath(parsedFileName)
if err != nil {
panic(err)
}
descriptorWrapped, err := desc.WrapFile(descriptorFile)
if err != nil {
panic(err)
}
// print
err = printer.PrintProtoFile(descriptorWrapped, &writer)
if err != nil {
panic(err)
}
fmt.Println(writer.String())
}
from protoreflect.
I'm using
ParseFilesButDoNotLink(...)
because the extension may be unknown
I don't quite follow this. Do you mean the source might be importing a file that is unknown? That is the only way for the file to be valid source and the extension to be unknown. And in that case (if you don't have access to the imports), I don't see how you could create a desc.Descriptor
to pass to the protoprint package.
In any event, I've opened a PR with the fix, since it was intended that this could work with uninterpreted options -- there just wasn't adequate test coverage to verify it was right. In practice though, you should really be using ParseFiles
. If you had problems getting that to work, open another issue and I may be able to help.
from protoreflect.
Thanks
I probably wrote something wrong (I've been working with protobuf not so long ago and might not understand some things). But I found proto2 files that use extended options that are defined in another file, but are not imported. In this case, I can't use ParseFiles because I get an error message like that: field Full.Path.To.field: unknown extension .Full.Path.To.extension
from protoreflect.
But I found proto2 files that use extended options that are defined in another file, but are not imported.
Hmm, that's quite suspicious. If they use custom options that are not imported then they are not valid source files. No other compiler (particularly the official protoc
compiler) will accept that. Is it possible that the extensions used to be defined in one of the files it imports, but something changed in the dependencies? Likely those sources should be fixed to import the custom option definitions.
from protoreflect.
Related Issues (20)
- possible regression from v1.15.0 HOT 5
- can't handle the import proto HOT 1
- Selecting marshaling method for known field types HOT 1
- Calling client.AllExtensionNumbersForType when no extensions exist returns an error HOT 2
- Support for reading proto files from jar / zip files HOT 4
- How to get comments from result of ParseFilesButDoNotLink HOT 1
- Any method for setting nested MessageKind field HOT 4
- Cannot add nested message and a field with same name using `builder` HOT 2
- 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
- Upgrade protocompile to v0.7.0 HOT 3
- go build error HOT 3
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.