Skip to content
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

Allow arbitrary extra data in API objects that support this loose format #104

Merged
merged 6 commits into from
Mar 21, 2018

Conversation

rokob
Copy link
Contributor

@rokob rokob commented Feb 15, 2018

In the API docs some of the elements in the payload body support arbitrary extra data with a comment that starts like // Can contain any arbitrary keys.. This PR adds back the ability to include this extra data with the various objects that the API understands, namely: Server, Message, Request. Client already supports this. Additionally, the docs don't mention it but we have always supported arbitrary extra data with Person so this adds that capacity as well.

The general form is to use the Builder for each type and to pass in a Map<String, Object> to the metadata function. This map will get merged into the final JSON for that object before the standard keys are added. That means that keys that are standard such as body for the Message type will be overwritten if they exist in this metadata object. See the tests added herein for examples of this behaviour.

Fixes #86

@rokob
Copy link
Contributor Author

rokob commented Feb 15, 2018

This is so weird. The test that is failing on the push should ALWAYS be failing but it passes because the JVM is pretty fast. We are comparing two different calls to the current system time for equality, so I'll fix that.

@@ -25,6 +25,8 @@

public class Factory {

private static final long CURRENT_TIME = System.currentTimeMillis();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

private Person(Builder builder) {
this.id = builder.id;
this.username = builder.username;
this.email = builder.email;
this.metadata = builder.metadata != null ? unmodifiableMap(builder.metadata) : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not be a big deal, but unmodifiableMap() doesn't guarantee that the map is immutable. So this change actually changes Person from an immutable object to a mutable one (though it can only be mutated by mutating the map passed to the builder).

FWIW.

@mattdrees
Copy link
Contributor

@rokob is there anything I can do to help here? I am guessing you're wanting to solve the NPath complexity warnings that Codacy raised. If that's true, I can work on a solution.

@rokob rokob merged commit d58988d into master Mar 21, 2018
@rokob rokob deleted the allow-arbitrary-keys branch December 18, 2018 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants