Skip to content

Commit

Permalink
Use newline-delimited JSON instead of CSV for test data files
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoverton committed Dec 18, 2023
1 parent 8483798 commit a67a3b1
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 54 deletions.
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
1 change: 0 additions & 1 deletion ndc-reference/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions ndc-reference/articles.csv

This file was deleted.

3 changes: 0 additions & 3 deletions ndc-reference/authors.csv

This file was deleted.

22 changes: 3 additions & 19 deletions ndc-reference/bin/reference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BTreeMap<i64, Row>, Box<dyn Error>> {
let mut rdr = csv::Reader::from_path(path)?;
let mut records: BTreeMap<i64, Row> = BTreeMap::new();
for row in rdr.deserialize() {
let row: BTreeMap<String, serde_json::Value> = 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<BTreeMap<i64, Row>, Box<dyn Error>> {
Expand Down Expand Up @@ -125,9 +109,9 @@ async fn metrics_middleware<T>(
// 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();
Expand Down
6 changes: 3 additions & 3 deletions specification/src/tutorial/getting-started.md
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -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}}
Expand Down

0 comments on commit a67a3b1

Please sign in to comment.