From 0e52c1fb0ad928c7babc0ca6311d1c3243fe20a4 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 4 Jun 2023 22:08:46 -0400 Subject: [PATCH] more json schema --- veilid-core/src/veilid_api/api.rs | 8 ++ veilid-core/src/veilid_api/json_api/mod.rs | 37 +++++- .../src/veilid_api/json_api/table_db.rs | 109 ++++++++++++++++++ veilid-flutter/lib/veilid_ffi.dart | 10 +- veilid-flutter/lib/veilid_js.dart | 2 +- veilid-flutter/lib/veilid_table_db.dart | 2 +- veilid-flutter/rust/src/dart_ffi.rs | 4 +- 7 files changed, 159 insertions(+), 13 deletions(-) diff --git a/veilid-core/src/veilid_api/api.rs b/veilid-core/src/veilid_api/api.rs index 33d4ed91..cd6d96dc 100644 --- a/veilid-core/src/veilid_api/api.rs +++ b/veilid-core/src/veilid_api/api.rs @@ -295,4 +295,12 @@ impl VeilidAPI { pub async fn cancel_tunnel(&self, _tunnel_id: TunnelId) -> VeilidAPIResult { panic!("unimplemented"); } + + //////////////////////////////////////////////////////////////// + // JSON API + + #[instrument(level = "debug", skip(self))] + pub async fn json_request(&self, request: json_api::Request) -> json_api::Response { + panic!("unimplemented"); + } } diff --git a/veilid-core/src/veilid_api/json_api/mod.rs b/veilid-core/src/veilid_api/json_api/mod.rs index 9fab5eae..f435b102 100644 --- a/veilid-core/src/veilid_api/json_api/mod.rs +++ b/veilid-core/src/veilid_api/json_api/mod.rs @@ -19,6 +19,13 @@ pub struct Request { op: RequestOp, } +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(tag = "type")] +pub enum RecvMessage { + Response(Response), + Update(VeilidUpdate), +} + #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct Response { /// Operation Id (pairs with Request, or empty if unidirectional) @@ -72,6 +79,7 @@ pub enum RequestOp { name: String, }, TableDb(TableDbRequest), + TableDbTransaction(TableDbTransactionRequest), // Crypto GetCryptoSystem { #[schemars(with = "String")] @@ -120,9 +128,6 @@ pub struct NewPrivateRouteResult { #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(tag = "op")] pub enum ResponseOp { - Update { - value: VeilidUpdate, - }, GetState { #[serde(flatten)] result: ApiResult, @@ -171,6 +176,7 @@ pub enum ResponseOp { result: ApiResult, }, TableDb(TableDbResponse), + TableDbTransaction(TableDbTransactionResponse), // Crypto GetCryptoSystem { #[serde(flatten)] @@ -250,6 +256,25 @@ pub enum ApiResultWithVecU8 { error: VeilidAPIError, }, } +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(transparent)] +pub struct VecU8 { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + value: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(untagged)] +pub enum ApiResultWithVecVecU8 { + Ok { + #[schemars(with = "Vec")] + value: Vec, + }, + Err { + error: VeilidAPIError, + }, +} #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(untagged)] @@ -268,7 +293,7 @@ where pub fn emit_schemas(out: &mut HashMap) { let schema_request = schema_for!(Request); - let schema_response = schema_for!(Response); + let schema_recv_message = schema_for!(RecvMessage); out.insert( "Request".to_owned(), @@ -276,7 +301,7 @@ pub fn emit_schemas(out: &mut HashMap) { ); out.insert( - "Response".to_owned(), - serde_json::to_string_pretty(&schema_response).unwrap(), + "RecvMessage".to_owned(), + serde_json::to_string_pretty(&schema_recv_message).unwrap(), ); } diff --git a/veilid-core/src/veilid_api/json_api/table_db.rs b/veilid-core/src/veilid_api/json_api/table_db.rs index 62190e83..483c5d71 100644 --- a/veilid-core/src/veilid_api/json_api/table_db.rs +++ b/veilid-core/src/veilid_api/json_api/table_db.rs @@ -18,9 +18,118 @@ pub struct TableDbResponse { #[serde(tag = "db_op")] pub enum TableDbRequestOp { Release, + GetColumnCount, + GetKeys { + col: i32, + }, + Transact, + Store { + col: i32, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + key: Vec, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + value: Vec, + }, + Load { + col: i32, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + key: Vec, + }, + Delete { + col: i32, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + key: Vec, + }, } #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(tag = "db_op")] pub enum TableDbResponseOp { Release, + GetColumnCount { + value: i32, + }, + GetKeys { + #[serde(flatten)] + #[schemars(with = "ApiResult>")] + result: ApiResultWithVecVecU8, + }, + Transact { + value: String, + }, + Store { + #[serde(flatten)] + result: ApiResult<()>, + }, + Load { + #[serde(flatten)] + #[schemars(with = "ApiResult>")] + result: ApiResult>, + }, + Delete { + #[serde(flatten)] + #[schemars(with = "ApiResult>")] + result: ApiResult>, + }, +} + +////////////////////////////////////////////////////////////////////////////////////////////////////// + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct TableDbTransactionRequest { + tx_id: String, + #[serde(flatten)] + tx_op: TableDbTransactionRequestOp, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct TableDbTransactionResponse { + tx_id: String, + #[serde(flatten)] + tx_op: TableDbTransactionResponseOp, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(tag = "tx_op")] +pub enum TableDbTransactionRequestOp { + Commit, + Rollback, + Store { + col: i32, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + key: Vec, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + value: Vec, + }, + Delete { + col: i32, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + key: Vec, + }, +} +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(tag = "tx_op")] +pub enum TableDbTransactionResponseOp { + Commit { + #[serde(flatten)] + result: ApiResult<()>, + }, + Rollback { + #[serde(flatten)] + result: ApiResult<()>, + }, + Store { + #[serde(flatten)] + result: ApiResult<()>, + }, + Delete { + #[serde(flatten)] + result: ApiResult<()>, + }, } diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 5f17e7ec..a0a9bdcd 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -797,7 +797,7 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { } @override - Future delete(int col, Uint8List key) { + Future delete(int col, Uint8List key) { final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_transaction_delete"); @@ -888,7 +888,7 @@ class VeilidTableDBFFI extends VeilidTableDB { } @override - Future delete(int col, Uint8List key) { + Future delete(int col, Uint8List key) async { final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_delete"); @@ -899,7 +899,11 @@ class VeilidTableDBFFI extends VeilidTableDB { col, nativeEncodedKey, ); - return processFuturePlain(recvPort.first); + String? out = await processFuturePlain(recvPort.first); + if (out == null) { + return null; + } + return base64UrlNoPadDecode(out); } } diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 8a323516..139c10a5 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -368,7 +368,7 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { } @override - Future delete(int col, Uint8List key) { + Future delete(int col, Uint8List key) { final encodedKey = base64UrlNoPadEncode(key); return _wrapApiPromise(js_util.callMethod( diff --git a/veilid-flutter/lib/veilid_table_db.dart b/veilid-flutter/lib/veilid_table_db.dart index 84923fd0..19bee7f2 100644 --- a/veilid-flutter/lib/veilid_table_db.dart +++ b/veilid-flutter/lib/veilid_table_db.dart @@ -8,7 +8,7 @@ abstract class VeilidTableDBTransaction { Future commit(); Future rollback(); Future store(int col, Uint8List key, Uint8List value); - Future delete(int col, Uint8List key); + Future delete(int col, Uint8List key); Future storeJson(int col, Uint8List key, Object? object, {Object? Function(Object? nonEncodable)? toEncodable}) async { diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index d18d0e30..238f1805 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -908,8 +908,8 @@ pub extern "C" fn table_db_transaction_delete(port: i64, id: u32, col: u32, key: tdbt.clone() }; - let out = tdbt.delete(col, &key); - APIResult::Ok(out) + tdbt.delete(col, &key); + APIRESULT_VOID }); }