Code Monkey home page Code Monkey logo

Comments (5)

jhump avatar jhump commented on June 2, 2024 1

@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.

japersik avatar japersik commented on June 2, 2024

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.

jhump avatar jhump commented on June 2, 2024

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.

japersik avatar japersik commented on June 2, 2024

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.

jhump avatar jhump commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.