Skip to content

Commit

Permalink
prep. 1.3.0, simplify CreateRow without explicit '.into()'
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Oct 31, 2023
1 parent 7902be4 commit 8200a41
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
## [1.3.0]

Better support for using composite primary keys:

* New enum in this crate: `tables::PrimaryKey`
* Single(string): `let sng_pk: PrimaryKey = "hello world".into()`
* Composite(BTreeMap<String, String>: `let cmp_pk: PrimaryKey = [("evt_tx_hash","hello".to_string()),("evt_index","world".to_string())].into()`
* `Single(String)`: `let single: PrimaryKey = "hello world".into()`
* `Composite(BTreeMap<String, String>)`: `let composite: PrimaryKey = [("evt_tx_hash","hello".to_string()),("evt_index","world".to_string())].into()`

Breaking changes:

* The `Rows` struct now requires pks to be of that new `PrimaryKey` type.
* create_row(), update_row() and delete_row() now require a `PrimaryKey` instead of a String.
* The `Rows.pks` field is not public anymore.
* `create_row()`, `update_row()` and `delete_row()` now require a `PrimaryKey` instead of a `String`. This should work directly with a `String`, `&String` or `&str`.

## [1.2.1]

Expand Down
52 changes: 40 additions & 12 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ impl Tables {
}
}

pub fn create_row(&mut self, table: &str, key: PrimaryKey) -> &mut Row {
pub fn create_row<K: Into<PrimaryKey>>(&mut self, table: &str, key: K) -> &mut Row {
let rows = self.tables.entry(table.to_string()).or_insert(Rows::new());
let key_debug = format!("{:?}", key);
let row = rows.pks.entry(key).or_insert(Row::new());
let k = key.into();
let key_debug = format!("{:?}", k);
let row = rows.pks.entry(k).or_insert(Row::new());
match row.operation {
Operation::Unspecified => {
row.operation = Operation::Create;
Expand All @@ -40,10 +41,11 @@ impl Tables {
row
}

pub fn update_row(&mut self, table: &str, key: PrimaryKey) -> &mut Row {
pub fn update_row<K: Into<PrimaryKey>>(&mut self, table: &str, key: K) -> &mut Row {
let rows = self.tables.entry(table.to_string()).or_insert(Rows::new());
let key_debug = format!("{:?}", key);
let row = rows.pks.entry(key).or_insert(Row::new());
let k = key.into();
let key_debug = format!("{:?}", k);
let row = rows.pks.entry(k).or_insert(Row::new());
match row.operation {
Operation::Unspecified => {
row.operation = Operation::Update;
Expand All @@ -60,9 +62,9 @@ impl Tables {
row
}

pub fn delete_row(&mut self, table: &str, key: PrimaryKey) -> &mut Row {
pub fn delete_row<K: Into<PrimaryKey>>(&mut self, table: &str, key: PrimaryKey) -> &mut Row {
let rows = self.tables.entry(table.to_string()).or_insert(Rows::new());
let row = rows.pks.entry(key).or_insert(Row::new());
let row = rows.pks.entry(key.into()).or_insert(Row::new());
match row.operation {
Operation::Unspecified => {
row.operation = Operation::Delete;
Expand Down Expand Up @@ -130,6 +132,12 @@ impl From<&str> for PrimaryKey {
}
}

impl From<&String> for PrimaryKey {
fn from(x: &String) -> Self {
Self::Single(x.clone())
}
}

impl From<String> for PrimaryKey {
fn from(x: String) -> Self {
Self::Single(x)
Expand All @@ -150,7 +158,7 @@ impl<K: AsRef<str>, const N: usize> From<[(K, String); N]> for PrimaryKey {
#[derive(Debug)]
pub struct Rows {
// Map of primary keys within this table, to the fields within
pub pks: HashMap<PrimaryKey, Row>,
pks: HashMap<PrimaryKey, Row>,
}

impl Rows {
Expand Down Expand Up @@ -272,6 +280,7 @@ mod test {
use crate::pb::database::table_change::PrimaryKey;
use crate::pb::database::CompositePrimaryKey;
use crate::pb::database::{DatabaseChanges, TableChange};
use crate::tables::PrimaryKey as TablesPrimaryKey;
use crate::tables::Tables;
use crate::tables::ToDatabaseValue;
use std::collections::HashMap;
Expand All @@ -287,10 +296,30 @@ mod test {
);
}

#[test]
fn create_row_single_pk_direct() {
let mut tables = Tables::new();
tables.create_row("myevent", TablesPrimaryKey::Single("myhash".to_string()));

assert_eq!(
tables.to_database_changes(),
DatabaseChanges {
table_changes: [TableChange {
table: "myevent".to_string(),
ordinal: 0,
operation: 1,
fields: [].into(),
primary_key: Some(PrimaryKey::Pk("myhash".to_string())),
}]
.to_vec(),
}
);
}

#[test]
fn create_row_single_pk() {
let mut tables = Tables::new();
tables.create_row("myevent", "myhash".into());
tables.create_row("myevent", "myhash");

assert_eq!(
tables.to_database_changes(),
Expand All @@ -315,8 +344,7 @@ mod test {
[
("evt_tx_hash", "hello".to_string()),
("evt_index", "world".to_string()),
]
.into(),
],
);

assert_eq!(
Expand Down

0 comments on commit 8200a41

Please sign in to comment.