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

Improve DX for Query method #66

Merged
merged 7 commits into from
Mar 11, 2022
Merged

Improve DX for Query method #66

merged 7 commits into from
Mar 11, 2022

Conversation

Shiranuit
Copy link
Contributor

@Shiranuit Shiranuit commented Feb 28, 2022

What does this PR do ?

Improves the DX of the Query method

  • Add a RawJson object that designate a string that should represent a Json Object and should not be escaped, RawJson can be given to sdk.query or be put anywhere in a map or object
  • Add RequestPayload class, mostly used in Kotlin to instantiate new query without having to use map.put multiples times in row
  • The serializer is now capable of serializing Java Objects directly

Before

Kotlin

val kuzzleQuery = HashMap<String?, Any?>()
kuzzleQuery.put("controller", "document")
kuzzleQuery.put("action", "create")
kuzzleQuery.put("index", "myIndex")
kuzzleQuery.put("collection", "myCollection")

val body = HashMap<String?, Any?>()
body.put("fieldA", "value")
body.put("fieldB", 42)

kuzzleQuery.put("body", body)
sdk.query(kuzzleQuery )

Java

HashMap<String, Object> kuzzleQuery = new HashMap<String, Object>();
kuzzleQuery.put("controller", "document");
kuzzleQuery.put("action", "create");
kuzzleQuery.put("index", "myIndex");
kuzzleQuery.put("collection", "myCollection");

HashMap<String, Object> body = new HashMap<String, Object>();
body.put("fieldA", "value");
body.put("fieldB", 42);

kuzzleQuery.put("body", body);
sdk.query(kuzzleQuery);

After

Kotlin

Using Maps

val kuzzleQuery = HashMap<String?, Any?>()
kuzzleQuery["controller"] = "document"
kuzzleQuery["action"] = "create"
kuzzleQuery["index"] = "myIndex"
kuzzleQuery["collection"] = "myCollection"

val body = HashMap<String?, Any?>()
body["fieldA"] = "value"
body["fieldB"] = 42

kuzzleQuery["body"] = body
sdk.query(kuzzleQuery )

Using Maps with RawJson (RawJson can be used anywhere an object is needed)

val kuzzleQuery = HashMap<String?, Any?>()
kuzzleQuery["controller"] = "document"
kuzzleQuery["action"] = "create"
kuzzleQuery["index"] = "myIndex"
kuzzleQuery["collection"] = "myCollection"

val body = """
{
  "fieldA": "value",
  "fieldB": 42
}
""".trimIndent()

kuzzleQuery["body"] = RawJson(body)
sdk.query(kuzzleQuery )

Using POJO

data class Document(val fieldA: String, val fieldB: Int)

val kuzzleQuery = HashMap<String?, Any?>()
kuzzleQuery["controller"] = "document"
kuzzleQuery["action"] = "create"
kuzzleQuery["index"] = "myIndex"
kuzzleQuery["collection"] = "myCollection"

kuzzleQuery["body"] = Document("value", 42) // POJO will be automatically serialized based on their properties
sdk.query(kuzzleQuery )

Calling query with String / RawJson (Causes deserialization + reserialization, should be avoided with big payloads)

val queryString = """
    {
        "controller": "document",
        "action": "create",
        "index": "myIndex",
        "collection": "myCollection",
        "body": {
            "fieldA": "value",
            "fieldB": 42
        }
    }
""".trimIndent()
sdk.query(queryString); // Using String
sdk.query(RawJson(queryString)); // Using RawJson

Using RequestPayload object

data class Document(val fieldA: String, val fieldB: Int)

val queryPayload = RequestPayload(
  controller = "document",
  action = "create",
  index = "myIndex",
  collection = "myCollection",
  body = Document(
    fieldA = "value",
    fieldB = 42
  )
)
sdk.query(queryPayload)

Java

Using Maps

HashMap<String, Object> kuzzleQuery = new HashMap<String, Object>();
kuzzleQuery.put("controller", "document");
kuzzleQuery.put("action", "create");
kuzzleQuery.put("index", "myIndex");
kuzzleQuery.put("collection", "myCollection");

HashMap<String, Object> body = new HashMap<String, Object>();
body.put("fieldA", "value");
body.put("fieldB", 42);

kuzzleQuery.put("body", body);
sdk.query(kuzzleQuery);

Using Maps with RawJson (RawJson can be used anywhere an object is needed)

HashMap<String, Object> kuzzleQuery = new HashMap<String, Object>();
kuzzleQuery.put("controller", "document");
kuzzleQuery.put("action", "create");
kuzzleQuery.put("index", "myIndex");
kuzzleQuery.put("collection", "myCollection");

String body = """
{
  "fieldA": "value",
  "fieldB": 42
}
""";

kuzzleQuery.put("body", new RawJson(body))
sdk.query(kuzzleQuery);

Using POJO

class Document {
  public String fieldA;
  public int fieldB;

  public Document(String fieldA, int fieldB) {
    this.fieldA = fieldA;
    this.fieldB = fieldB;
  }
}

HashMap<String, Object> kuzzleQuery = new HashMap<String, Object>();
kuzzleQuery.put("controller", "document");
kuzzleQuery.put("action", "create");
kuzzleQuery.put("index", "myIndex");
kuzzleQuery.put("collection", "myCollection");

kuzzleQuery.put("body", new Document("value", 42)) // POJO will be automatically serialized based on their properties
sdk.query(kuzzleQuery);

Calling query with String / RawJson (Causes deserialization + reserialization, should be avoided with big payloads)

String queryString = """
    {
        "controller": "document",
        "action": "create",
        "index": "myIndex",
        "collection": "myCollection",
        "body": {
            "fieldA": "value",
            "fieldB": 42
        }
    }
""";
sdk.query(queryString); // Using String
sdk.query(new RawJson(queryString)); // Using RawJson

Using RequestPayload object (less useful than for Kotlin, loosing named parameters)

class Document {
  public String fieldA;
  public int fieldB;

  public Document(String fieldA, int fieldB) {
    this.fieldA = fieldA;
    this.fieldB = fieldB;
  }
}

val queryPayload = new RequestPayload();

queryPayload.setController("document");
queryPayload.setAction("create");
queryPayload.setIndex("myIndex");
queryPayload.setCollection("myCollection");
queryPayload.setBody(new Document("value", 42));

sdk.query(queryPayload);

@Shiranuit Shiranuit self-assigned this Feb 28, 2022
@Shiranuit Shiranuit marked this pull request as draft February 28, 2022 15:16
@Shiranuit Shiranuit linked an issue Feb 28, 2022 that may be closed by this pull request
@Shiranuit Shiranuit marked this pull request as ready for review February 28, 2022 16:09
@Aschen Aschen merged commit 0860d9f into 1-dev Mar 11, 2022
@Aschen Aschen deleted the support-raw-json branch March 11, 2022 13:47
@Shiranuit Shiranuit mentioned this pull request Mar 14, 2022
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.

Improve DX for Query method
2 participants