square / protoparser Goto Github PK
View Code? Open in Web Editor NEWJava parser for .proto schema declarations.
Home Page: http://square.github.io/protoparser/
License: Apache License 2.0
Java parser for .proto schema declarations.
Home Page: http://square.github.io/protoparser/
License: Apache License 2.0
This would be very handy.
Also please add the relevant static ProtoSchemaParser.parse( .. ) helper methods.
package validation;
extend google.protobuf.FieldOptions {
optional Range range = 12345;
}
message Range {
optional double min = 1;
optional double max = 2;
}
package things;
message Foo {
optional int32 bar = 1 [
(validation.range).min = 1,
(validation.range).max = 100,
default = 20
];
}
Fails upon hitting the .
after the first (validation.range)
in ProtoSchemaParser
's readName
.
cc: @swankjesse @danrice-square
Easy fix would be to add something like this:
c = peekChar();
if (c == '.') {
pos++;
optionName += "." + readWord();
}
Which turns the Field
's extension map into {validation.range.min: 1, validation.range.max: 100, default: 20}
.
But we should probably be smarter about the type and turn it into {validation.range: Range{min:1, max:100}, default: 20}
, right?
Throw exception on:
message Foo {
required string first_name = 1;
required string last_name = 1;
}
alpha version is out here
https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-2
It would be nice to have javadocs available online. I usually do this on gh-pages branch for github, but I am sure there are other ways. Can then link javadocs from README.
Providing a List<Option>
is accurate to the source, but annoying for downstream consumers. Converting to a Map<String, Object>
is also a pain for consumers. There needs to be an easy map-like lookup of option values.
Maybe something like this?
public final class OptionList implements List<Option> {
public Object get(String key) { .. }
public String getString(String key) { .. }
public Option getOption(String key) { .. }
}
Simple solution would be to offer
public static Object getOption(List<Option> options, String key)
that iterates every time.
file.proto
package example.simple;
message Message1 {
optional string bestHuman = 1;
}
this proto file will generate example.simple.file.Message1
by google proto compiler
but square/protoparser will
getFullyQualifiedName as example.simple.Message1
will us to support this feature?
The primitive types should be available as string constants somewhere. Maybe on a Types
class which also has convenience methods like isPrimitive(String)
.
idl code as follow:
"package tutorial;" +
"option java_package = "com.example.tutorial";"
"option java_outer_classname = "AddressBookProtos";"
" message PhoneNumber {"
"required int32 number = 1 [default = 1];"
"}";
while call field.getDefault(); will throw cast exception due to integer can not cast to String
public String getDefault() {
Option defaultOption = Option.findByName(options, "default");
return defaultOption != null ? (String) defaultOption.getValue() : null;
}
It's tedious constructing a message, for example. The tests are littered with a bunch of default values for everything.
First of all, thank you for this great library. It has worked like a charm so far; I use it for Jackson protobuf data format backend (https://github.com/FasterXML/jackson-dataformat-protobuf).
So. It looks like handling of isPacked()
method in MessageType.Field
broke between 3.1.4 and 3.1.5.
I noticed when trying to upgrade version, when couple of unit tests on my end started failing. Looking at changelog and set of tests failing, it looked like somehow 'packed' option was not being detected.
However: looks like parser actually finds options as expected. The problem is rather that it seems to coerce type into Boolean.TRUE
instead of String "true". Unfortunately isPacked()
assumes a String, and equals() fails between the two representations.
I would guess isDeprecated()
is broken similarly.
I can work around the problem locally by using "getOptions()", doing explicit checks, but it would make sense to fix the convenient accessor as well.
Hi, first of all, thank you for your contribution
The thing is when I try to check a MessageType.Field has default or not
I use => if (field.getDefault() != null)
but I get exception:
com.squareup.protoparser.EnumType$Value cannot be cast to java.lang.String
the original .proto defined is
optional PhoneType type = 2 [default = HOME];
(same as official example Person message: https://developers.google.com/protocol-buffers/docs/javatutorial)
is any misunderstanding or mistake in my code?
(my source code https://github.com/cecol/protobuf-to-avro/blob/dev/src/main/java/wilber/com/transform/ProtoSchema.java)
comment should like official protobuf with heading comment and trailing comment
/**
* <code>optional string leading_comments = 3;</code>
*
* <pre>
* If this SourceCodeInfo represents a complete declaration, these are any
* comments appearing before and after the declaration which appear to be
* attached to the declaration.
* A series of line comments appearing on consecutive lines, with no other
* tokens appearing on those lines, will be treated as a single comment.
* Only the comment content is provided; comment markers (e.g. //) are
* stripped out. For block comments, leading whitespace and an asterisk
* will be stripped from the beginning of each line other than the first.
* Newlines are included in the output.
* Examples:
* optional int32 foo = 1; // Comment attached to foo.
* // Comment attached to bar.
* optional int32 bar = 2;
* optional string baz = 3;
* // Comment attached to baz.
* // Another line attached to baz.
* // Comment attached to qux.
* //
* // Another line attached to qux.
* optional double qux = 4;
* optional string corge = 5;
* /* Block comment attached
* * to corge. Leading asterisks
* * will be removed. */
* /* Block comment attached to
* * grault. */
* optional int32 grault = 6;
* </pre>
*/
Added in 2.6.0.
https://developers.google.com/protocol-buffers/docs/proto#oneof
The implementation from #77 expects the syntax declaration to be in this form:
syntax "proto3";
while the proto3 documentation states that the syntax declaration is
syntax = "proto3";
If I use protoparser to parse the proto3 message from the proto3 spec it fails with the following exception
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
Exception in thread "main" java.lang.AssertionError
at com.squareup.protoparser.ProtoParser.readQuotedString(ProtoParser.java:602)
at com.squareup.protoparser.ProtoParser.readDeclaration(ProtoParser.java:134)
at com.squareup.protoparser.ProtoParser.readProtoFile(ProtoParser.java:92)
// A --> B
// \
// C
message Test {}
Should result in:
A --> B
\
C
and not:
A --> B
\
C
We need to be less-eager about nom-ing all the leading whitespace. There are only a few leading versions of comments so we can be a bit more smart. Something like this:
^\s+(// ?|/\*\* ?|\* ?)
I know groups are deprecated but are still part of the spec. Can we add support for them for the sake of compatibility with existing proto schemas?
message Foo {
optional int32 bar = 1;
optional int32 quix = 1;
}
Compiles just fine.
This should only be allowed if allow_alias = true.
message Foo {
option allow_alias = true;
optional int32 bar = 1;
optional int32 quix = 1;
}
And model property. Probably an enum.
Valid values:
syntax = "proto3"
syntax = "proto2"
Hi
The following source files are without license headers:
./src/main/java/com/squareup/protoparser/DataType.java
./src/main/java/com/squareup/protoparser/EnumConstantElement.java
./src/main/java/com/squareup/protoparser/EnumElement.java
./src/main/java/com/squareup/protoparser/ExtendElement.java
./src/main/java/com/squareup/protoparser/ExtensionsElement.java
./src/main/java/com/squareup/protoparser/FieldElement.java
./src/main/java/com/squareup/protoparser/MessageElement.java
./src/main/java/com/squareup/protoparser/OneOfElement.java
./src/main/java/com/squareup/protoparser/OptionElement.java
./src/main/java/com/squareup/protoparser/ProtoFile.java
./src/main/java/com/squareup/protoparser/ProtoParser.java
./src/main/java/com/squareup/protoparser/RpcElement.java
./src/main/java/com/squareup/protoparser/ServiceElement.java
./src/main/java/com/squareup/protoparser/TypeElement.java
./src/test/java/com/squareup/protoparser/DataTypeTest.java
./src/test/java/com/squareup/protoparser/EnumConstantElementTest.java
./src/test/java/com/squareup/protoparser/EnumElementTest.java
./src/test/java/com/squareup/protoparser/ExtendElementTest.java
./src/test/java/com/squareup/protoparser/ExtensionsElementTest.java
./src/test/java/com/squareup/protoparser/FieldElementTest.java
./src/test/java/com/squareup/protoparser/MessageElementTest.java
./src/test/java/com/squareup/protoparser/OneOfElementTest.java
./src/test/java/com/squareup/protoparser/OptionElementTest.java
./src/test/java/com/squareup/protoparser/ParsingTester.java
./src/test/java/com/squareup/protoparser/ProtoFileTest.java
./src/test/java/com/squareup/protoparser/RpcElementTest.java
./src/test/java/com/squareup/protoparser/ServiceElementTest.java
./src/test/java/com/squareup/protoparser/TestUtils.java
./src/test/java/com/squareup/protoparser/UtilsTest.java
Please, confirm the licensing of code and/or content/s, and add license headers
https://fedoraproject.org/wiki/Packaging:LicensingGuidelines?rd=Packaging/LicensingGuidelines#License_Clarification
The following source files contains this license header
// Copyright 2013 Square, Inc.
./src/main/java/com/squareup/protoparser/Utils.java
./src/test/java/com/squareup/protoparser/ProtoParserTest.java
can You change with
/*
* Copyright 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
?
Thanks in advance
Regards
Here is a simple example. Thanks.
option java_package = "com.test";
option java_generic_services = false;
option java_outer_classname = "ClientCode";
enum ClientCode {
option allow_alias = true;
T_1 = 1;
T_0 = 1;
}
Failing test:
@Test public void extendInMessage() throws Exception {
String proto = ""
+ "message Bar {\n"
+ " extend Foo {\n"
+ " optional int32 bar = 126;\n"
+ " }\n"
+ "}";
List<ExtendDeclaration> extendDeclarations = new ArrayList<ExtendDeclaration>();
extendDeclarations.add(new ExtendDeclaration("Foo", "Foo", "",
Arrays.asList(new MessageType.Field(Label.OPTIONAL, "int32", "bar", 126, "", NO_OPTIONS))));
Type messageType =
new MessageType("Bar", "Bar", "", NO_FIELDS, NO_TYPES, NO_EXTENSIONS, NO_OPTIONS);
ProtoFile expected =
new ProtoFile("descriptor.proto", null, NO_STRINGS, NO_STRINGS, Arrays.asList(messageType),
NO_SERVICES, NO_OPTIONS, extendDeclarations);
assertThat(ProtoSchemaParser.parse("descriptor.proto", proto))
.isEqualTo(expected);
}
Enum values are scoped as siblings to the enum, not children. As such, value tags must be unique within the parent scope.
Throw an exception on:
message Foo {
enum Bar {
HELLO = 1;
}
enum Baz {
GOODBYE = 1;
}
}
String proto = ""
+ "package example;"
+ ""
+ "message A {"
+ " message B {}"
+ "}"
+ "message C {"
+ " optional A.B ab = 1;"
+ "}";
ProtoSchemaParser.parse("test.proto", proto);
java.lang.IllegalStateException: Syntax error in test.proto at 1:74: unexpected end of file
at com.squareup.protoparser.ProtoSchemaParser.unexpected(ProtoSchemaParser.java:739)
at com.squareup.protoparser.ProtoSchemaParser.peekChar(ProtoSchemaParser.java:515)
at com.squareup.protoparser.ProtoSchemaParser.readMessage(ProtoSchemaParser.java:216)
at com.squareup.protoparser.ProtoSchemaParser.readDeclaration(ProtoSchemaParser.java:163)
at com.squareup.protoparser.ProtoSchemaParser.readProtoFile(ProtoSchemaParser.java:119)
at com.squareup.protoparser.ProtoSchemaParser.parse(ProtoSchemaParser.java:42)
When parsing:
message DescriptorProto {
optional string name = 1;
optional string doc = 8; // Doc string for generated code.
repeated FieldDescriptorProto field = 2;
}
the doc string "Doc string for generated code." is associated with the field 'field' instead of the field 'doc'.
The javadoc at https://square.github.io/protoparser/ is out of date; it still talks about ProtoSchemaParser
rather than ProtoParser
.
Thank you for making this package.
I think the FieldElement type should hold information about whether the value is of a repeated
type. (in protobuf lib called cardinality
).
Let me know if you accept a PR for this (and if you agree that it is missing)
Declared as map<string, string>
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.