-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 type handlers for SimpleUri and BlockUri #5061
Conversation
public SimpleUri getFromString(String representation) { | ||
return new SimpleUri(representation); |
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.
Lets add a check of uri.isValid()
here. It's a cheap check, and doing it here instead of leaving it until later will make it easier to identify what the source string is and where it came from.
As for what we do if it doesn't pass the check... uh, if we think the deserialization code is trying to avoid throwing exceptions, I guess we log the error but still return the invalid SimpleUri instance?
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.
Looking at the implementation of StringRepresentationTypeHandler
returning null would yield Optional.empty
, which is our equivalent to "could not parse"... That probably better than returning an invlid SimpleUri
...
Line 22 in b367a18
return Optional.ofNullable(getFromString(data.getAsString())); |
public SimpleUri getFromString(String representation) { | ||
SimpleUri uri = new SimpleUri(representation); | ||
if (!uri.isValid()) { | ||
logger.error("Failed to create valid SimpleURI from string '{}'", representation); |
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.
logger.error("Failed to create valid SimpleURI from string '{}'", representation); | |
logger.error("Failed to create valid SimpleURI from string '{}'", representation); | |
// StringRepresentationTypeHandler will turn this 'null' value into an empty Optional | |
return null; |
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.
done ✔️
also added tests
- will result in empty optional indicating parsing / deserialization error
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.
[Oops, duplicate comment.]
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.
I think this looks good.
I was thinking a bit about what kind of test would be appropriate. I don't think SimpleUriTypeHandler would benefit from any additional tests to its methods, but maybe we could add an integration test one level higher up that demonstrates we can use SimpleUri with the default TypeHandlerLibrary? Would that be useful?
[Approving but not merging yet because the Release Candidate has already been cut and I'm not clear on which things need to go in this release.]
Could be but I'd argue that that would be a suitable consideration for the upcoming type handler improvements when we look at the contracts of the type handlers. |
As discussed in the contributor meeting, I added a type handler for |
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.
one small correction; feel free to merge after adding that if it works with your ChangingBlocks use case.
@@ -113,6 +115,7 @@ private static void populateWithDefaultHandlers(TypeHandlerLibrary serialization | |||
serializationLibrary.addTypeHandlerFactory(new AssetTypeHandlerFactory()); | |||
|
|||
serializationLibrary.addTypeHandler(Name.class, new NameTypeHandler()); | |||
serializationLibrary.addTypeHandler(SimpleUri.class, new SimpleUriTypeHandler()); |
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.
We'll want to add BlockUri's handler here too, right?
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.
good catch, thank you! will add that later today 👍
correction added and tested, works fine for the ChangingBlocks use case using ChangingBlocks#15 and PlantPack#20 👍 |
Contains
New type handlers to support de-/serializing to/from
SimpleUri
andBlockUri
.How to test
Can be tested in combination with Terasology/ChangingBlocks#15.