edenapi: make JSON representation of DataRequest into an object

Summary:
EdenAPI's `make_req` tools allows developers to create ad-hoc CBOR request payloads for debugging purposes (e.g., for use with `curl`). The tool generates requests from human-created JSON, which are particularly useful in Mercurial and Mononoke's integration tests.

Later in this stack, the use of this JSON format will be extended beyond just this one tool. As such, it is important that the representation be sufficiently extensible so accommodate future changes to the request structs. In the case of the JSON representation of `DataRequest`, this means changing from an array to a single-attribute object, so that additional fields can potentially be added in the future.

Reviewed By: quark-zju

Differential Revision: D22319314

fbshipit-source-id: 5931bc7ab01ca48ceab5ffd1c9177dd3035b643c
This commit is contained in:
Arun Kulshreshtha 2020-07-01 22:57:47 -07:00 committed by Facebook GitHub Bot
parent 765a5b840a
commit 632596e947
3 changed files with 27 additions and 14 deletions

View File

@ -33,10 +33,12 @@ Start up EdenAPI server.
Create and send file data request.
$ edenapi_make_req data > req.cbor <<EOF
> [
> ["test.txt", "$TEST_FILENODE"],
> ["copy.txt", "$COPY_FILENODE"]
> ]
> {
> "keys": [
> ["test.txt", "$TEST_FILENODE"],
> ["copy.txt", "$COPY_FILENODE"]
> ]
> }
> EOF
Reading from stdin
Generated request: DataRequest {

View File

@ -33,10 +33,12 @@ Start up EdenAPI server.
Create and send tree request.
$ edenapi_make_req data > req.cbor <<EOF
> [
> ["", "$ROOT_MFID_1"],
> ["", "$ROOT_MFID_2"]
> ]
> {
> "keys": [
> ["", "$ROOT_MFID_1"],
> ["", "$ROOT_MFID_2"]
> ]
> }
> EOF
Reading from stdin
Generated request: DataRequest {

View File

@ -68,16 +68,25 @@ fn main() -> Result<()> {
/// Example request:
///
/// ```json
/// [
/// ["path/to/file_1", "48f43af456d770b6a78e1ace628319847e05cc24"],
/// ["path/to/file_2", "7dcd6ede35eaaa5b1b16a341b19993e59f9b0dbf"],
/// ["path/to/file_3", "218d708a9f8c3e37cfd7ab916c537449ac5419cd"],
/// ]
/// {
/// "keys": [
/// ["path/to/file_1", "48f43af456d770b6a78e1ace628319847e05cc24"],
/// ["path/to/file_2", "7dcd6ede35eaaa5b1b16a341b19993e59f9b0dbf"],
/// ["path/to/file_3", "218d708a9f8c3e37cfd7ab916c537449ac5419cd"],
/// ]
/// }
/// ```
///
fn parse_data_req(json: &Value) -> Result<DataRequest> {
let json = json
.as_object()
.ok_or_else(|| anyhow!("input must be a JSON object"))?;
let keys = json
.get("keys")
.ok_or_else(|| anyhow!("missing field: keys"))?;
Ok(DataRequest {
keys: parse_keys(json)?,
keys: parse_keys(keys)?,
})
}