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

Concept map JSON serialization #404

Merged
merged 13 commits into from
Mar 23, 2023

Conversation

dmitrii-ubskii
Copy link
Member

@dmitrii-ubskii dmitrii-ubskii commented Mar 22, 2023

What is the goal of this PR?

We implement the JSON serialization of concept maps according to typedb/typedb-behaviour#238.

@typedb-bot
Copy link
Member

typedb-bot commented Mar 22, 2023

PR Review Checklist

Do not edit the content of this comment. The PR reviewer should simply update this comment by ticking each review item below, as they get completed.


Trivial Change

  • This change is trivial and does not require a code or architecture review.

Code

  • Packages, classes, and methods have a single domain of responsibility.
  • Packages, classes, and methods are grouped into cohesive and consistent domain model.
  • The code is canonical and the minimum required to achieve the goal.
  • Modules, libraries, and APIs are easy to use, robust (foolproof and not errorprone), and tested.
  • Logic and naming has clear narrative that communicates the accurate intent and responsibility of each module (e.g. method, class, etc.).
  • The code is algorithmically efficient and scalable for the whole application.

Architecture

  • Any required refactoring is completed, and the architecture does not introduce technical debt incidentally.
  • Any required build and release automations are updated and/or implemented.
  • Any new components follows a consistent style with respect to the pre-existing codebase.
  • The architecture intuitively reflects the application domain, and is easy to understand.
  • The architecture has a well-defined hierarchy of encapsulated components.
  • The architecture is extensible and scalable.

@dmitrii-ubskii dmitrii-ubskii changed the title Concept map json Concept map JSON serialization Mar 23, 2023
Comment on lines +44 to +49
@CheckReturnValue
default JsonObject toJSON() {
JsonObject object = Json.object();
map().forEach((resVar, resConcept) -> object.add(resVar, resConcept.toJSON()));
return object;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

All implementations of toJSON() are done in the api package. Otherwise the implementation would be duplicated between e.g. ThingImpl and ThingImpl.Remote, since ...Impl.Remote don't extend their enclosing counterparts.

Comment on lines +59 to +77
private boolean JSONValuesAreEqual(JsonValue left, JsonValue right) {
if (Objects.equals(left, right)) return true;
if (left == null || right == null) return false;
if (left.isObject() && right.isObject()) return JSONObjectsAreEqual(left.asObject(), right.asObject());
if (left.isArray() && right.isArray()) return JSONArraysAreEqual(left.asArray(), right.asArray());
return false;
}

private boolean JSONObjectsAreEqual(JsonObject left, JsonObject right) {
if (left.size() != right.size()) return false;
return left.names().stream().allMatch((name) -> JSONValuesAreEqual(left.get(name), right.get(name)));
}

private boolean JSONArraysAreEqual(JsonArray left, JsonArray right) {
if (left.size() != right.size()) return false;
for (int i = 0; i < left.size(); i++)
if (!JSONValuesAreEqual(left.get(i), right.get(i))) return false;
return true;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

JsonObjects are ordered, and by default the comparison would respect the order of keys. Since we don't specify the order, we have to define our own equality check for JsonValues.

@dmitrii-ubskii dmitrii-ubskii merged commit 4381fbb into typedb:master Mar 23, 2023
plugin = "pretty",
glue = "com.vaticle.typedb.client.test.behaviour",
features = "external/vaticle_typedb_behaviour/concept/serialization.feature",
tags = "not @ignore and not @ignore-typedb"
Copy link
Member

Choose a reason for hiding this comment

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

Can you double check the @ignore-typedb is correct here? I think we have one for client-java or clients specifically or something? Might as well do a drive-by of the other BDD tests in this repo while you're at it

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.

6 participants