-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
027395b
commit f6aa489
Showing
5 changed files
with
163 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use anyhow::Context; | ||
use proto_flow::flow; | ||
use std::collections::BTreeMap; | ||
|
||
pub struct State { | ||
val: Skim, | ||
merge_patch: bool, | ||
} | ||
|
||
impl State { | ||
pub fn reduce(&mut self, update: &flow::ConnectorState) -> anyhow::Result<()> { | ||
let flow::ConnectorState { | ||
merge_patch, | ||
updated_json, | ||
} = update; | ||
|
||
let update: BTreeMap<String, models::RawValue> = serde_json::from_str(updated_json) | ||
.context("failed to decode connector state as JSON object")?; | ||
|
||
if !*merge_patch { | ||
//self.val = update; | ||
self.merge_patch = false; | ||
} else if !self.merge_patch { | ||
// apply merge-patch to fully reduced value | ||
} else { | ||
// apply merge-patch to patch. | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn merge_patch(doc: &mut Skim, patch: Skim, delete_null: bool) { | ||
let Skim::Parsed(patch) = patch else { | ||
*doc = patch; // Patch is not an object. Stop now, taking its value. | ||
return; | ||
}; | ||
|
||
if let None = doc.as_object() { | ||
// `doc` is not already an object. Convert into an empty one. | ||
*doc = Skim::Parsed(BTreeMap::new()); | ||
} | ||
let doc = doc.as_object().unwrap(); | ||
|
||
/* | ||
for (key, value) in patch { | ||
if value.is_null() && delete_null { | ||
doc.remove(&key); | ||
} else { | ||
merge_patch( | ||
doc.entry(key).or_insert_with(Skim::default), | ||
value, | ||
delete_null, | ||
); | ||
} | ||
} | ||
*/ | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, serde::Serialize)] | ||
#[serde(untagged)] | ||
pub enum Skim { | ||
Parsed(BTreeMap<String, Skim>), | ||
Raw(Box<serde_json::value::RawValue>), | ||
} | ||
|
||
impl Skim { | ||
/* | ||
fn is_null(&self) -> bool { | ||
match self { | ||
Self::Raw(value) => value.is_null(), | ||
Self::Parsed(_) => false, | ||
} | ||
} | ||
*/ | ||
|
||
fn as_object(&mut self) -> Option<&mut BTreeMap<String, Skim>> { | ||
match self { | ||
Self::Parsed(parsed) => Some(parsed), | ||
Self::Raw(raw) => match serde_json::from_str(raw.get()) { | ||
Ok(parsed) => { | ||
*self = Self::Parsed(parsed); | ||
self.as_object() | ||
} | ||
Err(_) => None, | ||
}, | ||
} | ||
} | ||
|
||
/* | ||
fn shallow(input: &str) -> serde_json::Result<Self> { | ||
#[derive(serde::Deserialize)] | ||
#[serde(untagged)] | ||
pub enum Shallow { | ||
Raw(models::RawValue), | ||
Parsed(BTreeMap<String, Shallow>), | ||
} | ||
let skim: BTreeMap<String>::RawValue = serde_json::from_str(input)?; | ||
} | ||
*/ | ||
} | ||
|
||
/* | ||
impl Default for Skim { | ||
fn default() -> Self { | ||
Self::Raw(models::RawValue::default()) | ||
} | ||
} | ||
*/ | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::Skim; | ||
use serde_json::json; | ||
|
||
#[derive(Debug, serde::Deserialize, serde::Serialize)] | ||
#[serde(untagged)] | ||
pub enum Foo { | ||
//Parsed(BTreeMap<String, Skim>), | ||
Raw(Box<serde_json::value::RawValue>), | ||
} | ||
|
||
#[test] | ||
fn foobar() { | ||
let fixture = json!({"hello": {"world": 42, "foo": "bar"}}).to_string(); | ||
let out: Foo = serde_json::from_str(&fixture).unwrap(); | ||
|
||
insta::assert_debug_snapshot!(out, @""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import json, sys, base64, typing as t | ||
|
||
members : t.List[dict] = json.load(sys.stdin)["kvs"] | ||
members.sort(key = lambda member: member["create_revision"]) | ||
|
||
for m in members: | ||
broker = base64.b64decode(m["key"]).decode('utf8')[30:] | ||
spec = repr(base64.b64decode(m["value"])) | ||
lease = "%x" % m["lease"] | ||
print(f"{broker} CREATE {m['create_revision']} LEASE {lease} MOD {m['mod_revision']} SPEC {spec}") |