From a67a3b1c0b37e20bf50e182ea388381a2352c8a1 Mon Sep 17 00:00:00 2001 From: David Overton Date: Mon, 18 Dec 2023 14:07:38 +1100 Subject: [PATCH] Use newline-delimited JSON instead of CSV for test data files --- Cargo.lock | 22 ------------------- Dockerfile | 5 +++-- ndc-reference/Cargo.toml | 1 - ndc-reference/articles.csv | 4 ---- ndc-reference/authors.csv | 3 --- ndc-reference/bin/reference/main.rs | 22 +++---------------- specification/src/tutorial/getting-started.md | 6 ++--- 7 files changed, 9 insertions(+), 54 deletions(-) delete mode 100644 ndc-reference/articles.csv delete mode 100644 ndc-reference/authors.csv diff --git a/Cargo.lock b/Cargo.lock index 9362ec6f..b5aaf209 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -386,27 +386,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "csv" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" -dependencies = [ - "csv-core", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "csv-core" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" -dependencies = [ - "memchr", -] - [[package]] name = "darling" version = "0.20.1" @@ -988,7 +967,6 @@ version = "0.1.0" dependencies = [ "async-trait", "axum", - "csv", "goldenfile", "indexmap 2.1.0", "ndc-client", diff --git a/Dockerfile b/Dockerfile index 8e3ffb6a..9eb8f5f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,7 @@ RUN cargo build --release --all-targets FROM debian:buster-slim as ndc-reference COPY --from=build /app/target/release/ndc-reference ./ndc-reference -COPY --from=build /app/ndc-reference/articles.csv ./articles.csv -COPY --from=build /app/ndc-reference/authors.csv ./authors.csv +COPY --from=build /app/ndc-reference/articles.json ./articles.json +COPY --from=build /app/ndc-reference/authors.json ./authors.json +COPY --from=build /app/ndc-reference/universities.json ./universities.json CMD ["sh", "-c", "./ndc-reference"] diff --git a/ndc-reference/Cargo.toml b/ndc-reference/Cargo.toml index 411525fb..1c43a873 100644 --- a/ndc-reference/Cargo.toml +++ b/ndc-reference/Cargo.toml @@ -11,7 +11,6 @@ path = "bin/reference/main.rs" ndc-client = { path = "../ndc-client" } axum = "^0.6.20" -csv = "^1.3.0" indexmap = { version = "^2", features = ["serde"] } prometheus = "^0.13.3" regex = "^1.10.2" diff --git a/ndc-reference/articles.csv b/ndc-reference/articles.csv deleted file mode 100644 index 1b0b3c0e..00000000 --- a/ndc-reference/articles.csv +++ /dev/null @@ -1,4 +0,0 @@ -id,title,author_id -1,The Next 700 Programming Languages,1 -2,Why Functional Programming Matters,2 -3,The Design And Implementation Of Programming Languages,2 \ No newline at end of file diff --git a/ndc-reference/authors.csv b/ndc-reference/authors.csv deleted file mode 100644 index b7253267..00000000 --- a/ndc-reference/authors.csv +++ /dev/null @@ -1,3 +0,0 @@ -id,first_name,last_name -1,Peter,Landin -2,John,Hughes \ No newline at end of file diff --git a/ndc-reference/bin/reference/main.rs b/ndc-reference/bin/reference/main.rs index 62307bfd..3a8fd69e 100644 --- a/ndc-reference/bin/reference/main.rs +++ b/ndc-reference/bin/reference/main.rs @@ -36,22 +36,6 @@ pub struct AppState { pub metrics: Metrics, } // ANCHOR_END: app-state -// ANCHOR: read_csv -fn read_csv(path: &str) -> core::result::Result, Box> { - let mut rdr = csv::Reader::from_path(path)?; - let mut records: BTreeMap = BTreeMap::new(); - for row in rdr.deserialize() { - let row: BTreeMap = row?; - let id = row - .get("id") - .ok_or("'id' field not found in csv file")? - .as_i64() - .ok_or("'id' field was not an integer in csv file")?; - records.insert(id, row); - } - Ok(records) -} -// ANCHOR_END: read_csv // ANCHOR: read_json_lines fn read_json_lines(path: &str) -> core::result::Result, Box> { @@ -125,9 +109,9 @@ async fn metrics_middleware( // ANCHOR_END: metrics_middleware // ANCHOR: init_app_state fn init_app_state() -> AppState { - // Read the CSV data files - let articles = read_csv("articles.csv").unwrap(); - let authors = read_csv("authors.csv").unwrap(); + // Read the JSON data files + let articles = read_json_lines("articles.json").unwrap(); + let authors = read_json_lines("authors.json").unwrap(); let universities = read_json_lines("universities.json").unwrap(); let metrics = Metrics::new().unwrap(); diff --git a/specification/src/tutorial/getting-started.md b/specification/src/tutorial/getting-started.md index 05fe516a..98966bb6 100644 --- a/specification/src/tutorial/getting-started.md +++ b/specification/src/tutorial/getting-started.md @@ -1,8 +1,8 @@ # Getting Started -The reference implementation will serve queries and mutations based on in-memory data read from CSV files. +The reference implementation will serve queries and mutations based on in-memory data read from newline-delimited JSON files. -First, we will define some types to represent the data in the CSV files. Rows of CSV data will be stored in memory as ordered maps: +First, we will define some types to represent the data in the newline-delimited JSON files. Rows of JSON data will be stored in memory as ordered maps: ```rust,no_run,noplayground {{#include ../../../ndc-reference/bin/reference/main.rs:row-type}} @@ -14,7 +14,7 @@ Our application state will consist of collections of various types of rows: {{#include ../../../ndc-reference/bin/reference/main.rs:app-state}} ``` -In our `main` function, the data connector reads the initial data from the CSV files, and creates the `AppState`: +In our `main` function, the data connector reads the initial data from the newline-delimited JSON files, and creates the `AppState`: ```rust,no-run,noplayground {{#include ../../../ndc-reference/bin/reference/main.rs:init_app_state}}