-
Notifications
You must be signed in to change notification settings - Fork 937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional info for google.proto types #6153
Changes from 3 commits
41dc06f
c863988
22bbc07
68fb08c
32fa834
63924ef
80f212b
da2d4f2
dd4cc09
4c997bb
7740c9e
daa55b8
6334464
ac1ec53
1997946
a024ee4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,13 +368,14 @@ private static Map<String, Field> buildFields(Descriptor desc, | |
case BYTE_STRING: | ||
case ENUM: | ||
// Use field name which is specified in proto file. | ||
builder.put(key, new Field(field, parentNames, field.getJavaType())); | ||
builder.put(key, new Field(field, parentNames, field.getJavaType(), false)); | ||
break; | ||
case MESSAGE: | ||
@Nullable | ||
final JavaType wellKnownFieldType = getJavaTypeForWellKnownTypes(field); | ||
final boolean isOptional = isOptionalFieldType(field); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Let me know if I might be misunderstanding something 🙇 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realized that all fields are optional in proto3: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for providing the documentation references. |
||
if (wellKnownFieldType != null) { | ||
builder.put(key, new Field(field, parentNames, wellKnownFieldType)); | ||
builder.put(key, new Field(field, parentNames, wellKnownFieldType, isOptional)); | ||
break; | ||
} | ||
|
||
|
@@ -408,7 +409,7 @@ private static Map<String, Field> buildFields(Descriptor desc, | |
throw e; | ||
} | ||
|
||
builder.put(key, new Field(field, parentNames, JavaType.MESSAGE)); | ||
builder.put(key, new Field(field, parentNames, JavaType.MESSAGE, isOptional)); | ||
} | ||
break; | ||
} | ||
|
@@ -418,6 +419,22 @@ private static Map<String, Field> buildFields(Descriptor desc, | |
return builder.buildKeepingLast(); | ||
} | ||
|
||
private static boolean isOptionalFieldType(FieldDescriptor field) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That name is much better. |
||
final String fullName = field.getFullName(); | ||
if (DoubleValue.getDescriptor().getFullName().equals(fullName) || | ||
FloatValue.getDescriptor().getFullName().equals(fullName) || | ||
Int64Value.getDescriptor().getFullName().equals(fullName) || | ||
UInt64Value.getDescriptor().getFullName().equals(fullName) || | ||
Int32Value.getDescriptor().getFullName().equals(fullName) || | ||
UInt32Value.getDescriptor().getFullName().equals(fullName) || | ||
BoolValue.getDescriptor().getFullName().equals(fullName) || | ||
StringValue.getDescriptor().getFullName().equals(fullName) || | ||
BytesValue.getDescriptor().getFullName().equals(fullName)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Nullable | ||
private static JavaType getJavaTypeForWellKnownTypes(FieldDescriptor fd) { | ||
// MapField can be sent only via HTTP body. | ||
|
@@ -606,7 +623,8 @@ public HttpEndpointSpecification httpEndpointSpecification(Route route) { | |
spec.originalFields.entrySet().stream().collect( | ||
toImmutableMap(Entry::getKey, | ||
fieldEntry -> new Parameter(fieldEntry.getValue().type(), | ||
fieldEntry.getValue().isRepeated()))); | ||
fieldEntry.getValue().isRepeated(), | ||
fieldEntry.getValue().isOptional()))); | ||
return new HttpEndpointSpecification(spec.order, | ||
route, | ||
paramNames, | ||
|
@@ -981,11 +999,17 @@ static final class Field { | |
private final FieldDescriptor descriptor; | ||
private final List<String> parentNames; | ||
private final JavaType javaType; | ||
private final boolean isOptional; | ||
|
||
private Field(FieldDescriptor descriptor, List<String> parentNames, JavaType javaType) { | ||
private Field(FieldDescriptor descriptor, | ||
List<String> parentNames, | ||
JavaType javaType, | ||
boolean isOptional | ||
) { | ||
this.descriptor = descriptor; | ||
this.parentNames = parentNames; | ||
this.javaType = javaType; | ||
this.isOptional = isOptional; | ||
} | ||
|
||
JavaType type() { | ||
|
@@ -999,6 +1023,10 @@ String name() { | |
boolean isRepeated() { | ||
return descriptor.isRepeated(); | ||
} | ||
|
||
boolean isOptional() { | ||
return isOptional; | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we can do
this.isOptional = isOptional || isRepeated;
hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. I've applied it 👍