tallstoat / pbparser Goto Github PK
View Code? Open in Web Editor NEWGolang library for parsing protocol buffer (.proto) files
License: MIT License
Golang library for parsing protocol buffer (.proto) files
License: MIT License
Enhance the parser to stop complaining about external dependencies(imports) if the invoker so chooses.
Hi,
According to the documentation, the package declaration is optional.
https://developers.google.com/protocol-buffers/docs/proto3#packages
The verify() method makes an undefined package an error.
When a package is defined across more than one file, pbparser should account for parsed messages defined in all files when performing verification.
Currenty pbparser parses all messages however only retains a reference to the first dependancy protofile. Only messages defined in the main package protofile and the first dependancy protofile are checked when performing verification.
The following fails:
dependent.proto:
syntax = "proto3";
package dep;
import "dependency.proto";
message Dependent {
SamePackageDependencyMessage field = 1;
}
dependency.proto:
syntax = "proto3";
package dep;
message SamePackageDependencyMessage {
string field = 1;
}
If the two files are defined in different packages, this works well. If they are defined under the same package, it fails with error "Datatype: 'SamePackageDependencyMessage' referenced in field: 'field' is not defined".
The following is a legitimate protobuf file that works as expected when used over the wire:
syntax = "proto3";
package XYZ;
service CityService {
rpc GetCitiesAndDistricts(Query) returns (Result) {}
}
message Query {}
message Result {
message Countries {
repeated City cities = 2;
}
message City {}
}
Note how a Countries
contains repeated City
's.
Trying to parse this with pbparser gives the following error:
unable to parse photo file: Datatype: 'City' referenced in field 'cities' is not defined
When I move the City
outside Result
, so at the same level as Result
, pbparser can parse it correctly.
I will be searching through the source code and trying to fix this as I need the functionality, but if you think it's a small task and would take you a tenth of the time to fix it please let me know :)
It looks like the library cannot handle rpc options - specifically I am using the grpc gateway annotations:
rpc CreateWorkspace(CreateWorkspaceRequest) returns (CreateWorkspaceResponse) {
option (google.api.http) = { post: "/v1/workspaces" body: "*" };
}
also it will complain about external dependencies(imports) which I'm not interested in.
The program will enter an infinite loop, when parse proto file with comments like this \********* test ********\
.
For example, parser the following proto file:
\****** test *****\
syntax = "proto3";
suggest replace readMultiLineComment#parser.go from
func (p *parser) readMultiLineComment() string {
var buf bytes.Buffer
for {
c := p.read()
if c != '*' {
_, _ = buf.WriteRune(c)
} else {
c2 := p.read()
if c2 == '/' {
break
}
_, _ = buf.WriteRune(c2)
}
}
str := buf.String()
return strings.TrimSpace(str)
}
to
func (p *parser) readMultiLineComment() string {
var buf bytes.Buffer
for {
c := p.read()
if c == '*' {
if c2 := p.read(); c2 == '/' {
break
}
p.unread()
}
_, _ = buf.WriteRune(c)
}
str := buf.String()
return strings.TrimSpace(str)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.