-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: add field annotations merging functionality #83
Changes from 1 commit
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 |
---|---|---|
|
@@ -19,12 +19,14 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
// This class does not intend to do a fully-functional merge of two proto models, instead it focuses | ||
// on merging currently known potential discrepancies between old and new proto files: | ||
// - Missing field in a new proto file | ||
// - Missing values in enums | ||
// - Mismatching service method google.api.method_signature option values | ||
// - missing field in a new proto file; | ||
// - missing values in enums; | ||
// - mismatching service method google.api.method_signature option values (old overwrite new); | ||
// - mismatching field annotations (old overwrite new). | ||
// | ||
// Additional merging functionality is expected to be added to this class as needed. | ||
public class ProtoMerger { | ||
|
@@ -79,32 +81,50 @@ private void mergeMessages(Map<String, Message> newMessages, Map<String, Message | |
mergeFields(newMessage, oldMessage, newMessages); | ||
|
||
// Merge enums | ||
Map<String, Message> newEnumsMap = new HashMap<>(); | ||
for (Message nestedEnum : newMessage.getEnums()) { | ||
newEnumsMap.put(nestedEnum.getName(), nestedEnum); | ||
} | ||
for (Message oldEnum : oldMessage.getEnums()) { | ||
Message newEnum = newEnumsMap.get(oldEnum.getName()); | ||
mergeFields(newEnum, oldEnum, newMessages); | ||
} | ||
mergeEnums(newMessages, oldMessage, newMessage); | ||
} | ||
} | ||
|
||
private void mergeEnums( | ||
Map<String, Message> newMessages, Message oldMessage, Message newMessage) { | ||
Map<String, Message> newEnumsMap = new HashMap<>(); | ||
for (Message nestedEnum : newMessage.getEnums()) { | ||
newEnumsMap.put(nestedEnum.getName(), nestedEnum); | ||
} | ||
for (Message oldEnum : oldMessage.getEnums()) { | ||
Message newEnum = newEnumsMap.get(oldEnum.getName()); | ||
mergeFields(newEnum, oldEnum, newMessages); | ||
} | ||
} | ||
|
||
private void mergeFields( | ||
Message newMessage, Message oldMessage, Map<String, Message> newMessages) { | ||
// Merge fields | ||
Map<String, Field> newFieldsMap = | ||
newMessage.getFields().stream().collect(Collectors.toMap(ProtoElement::getName, f -> f)); | ||
|
||
for (Field oldField : oldMessage.getFields()) { | ||
// compareTo() will be used by contains() to search for the field, not equals(). | ||
if (!newMessage.getFields().contains(oldField)) { | ||
newMessage.getFields().add(copyField(oldField, newMessages)); | ||
// Copy removed field | ||
newMessage.getFields().add(copyField(null, oldField, newMessages)); | ||
} else { | ||
Field newField = newFieldsMap.get(oldField.getName()); | ||
// Copy missing options if a new field has less options than old 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. nitty nits: s/less/fewer/; s/new field/newField/ s/old field/oldField/ 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. This is a very nitty nit =) |
||
// This is a very primitive merge logic. Add a proper merge logic if ever needed. | ||
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. Consider making this comment a TODO. 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. The merger is basically written (and it matches the comment at the top of the merger class), that it merges only what is required and there are many potentially missing functions, which are expected to be added if needed. This specific case is not special compared to others, that is why I guess having todo for this one but not having for others would be weird. |
||
if (oldField.getOptions().size() > newField.getOptions().size() | ||
|| oldField.isOptional() != newField.isOptional()) { | ||
newMessage.getFields().remove(oldField); | ||
newMessage.getFields().add(copyField(newField, oldField, newMessages)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// We need to replace references for message types from oldMessages to newMessages. | ||
// Despite message types having the same names, they are two independently created | ||
// sets of objects. | ||
private Field copyField(Field oldField, Map<String, Message> newMessages) { | ||
private Field copyField(Field newField, Field oldField, Map<String, Message> newMessages) { | ||
Message valueType = Message.PRIMITIVES.get(oldField.getValueType().getName()); | ||
if (valueType == null) { | ||
valueType = newMessages.get(oldField.getValueType().getName()); | ||
|
@@ -123,7 +143,7 @@ private Field copyField(Field oldField, Map<String, Message> newMessages) { | |
oldField.isRepeated(), | ||
oldField.isOptional(), | ||
keyType, | ||
oldField.getDescription(), | ||
newField != null ? newField.getDescription() : oldField.getDescription(), | ||
oldField.isFirstInOrder()); | ||
|
||
// Do not do deep copy for options because they are all generic immutable objects (strings or | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,7 +327,7 @@ message DeleteAddressRequest { | |
// For example, consider a situation where you make an initial request and the request times out. If you make the request again with the same request ID, the server can check if original operation with the same request ID was received, and if so, will ignore the second request. This prevents clients from accidentally creating duplicate commitments. | ||
// | ||
// The request ID must be a valid UUID with the exception that zero UUID is not supported (00000000-0000-0000-0000-000000000000). | ||
optional string request_id = 37109963; | ||
optional string request_id = 37109963 [(google.api.field_behavior) = REQUIRED]; | ||
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 made the mistake of not removing the 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. (totally meant to send this comment this morning but didn't press the button) 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. This logic just preserves the old situation - if it was optional, then it will remain optional, if it idid not have optional, it will remain non-optional. This specific thing is just a test. I modified the test data to represent better the actual case in GCE. 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. Awesome, thanks! |
||
|
||
} | ||
|
||
|
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.
Aren't these all "old overwrites new"? Maybe we should just have a mention right before the list: "carrying over elements from the old version of the proto into the new version when doing a pure conversion would result in those elements being missing or mismatched"