Skip to content

Commit 70ac998

Browse files
authored
Move KV test suite to worker-sandbox and fix bug in KV put metadata. (#550)
* Move worker-kv test suite to worker-sandbox * Create explicit struct for KV PutOptions * Use JSON serializer for KV options
1 parent c76fdc4 commit 70ac998

File tree

17 files changed

+233
-612
lines changed

17 files changed

+233
-612
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

worker-kv/src/builder.rs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
use js_sys::{ArrayBuffer, Function, Object, Promise, Uint8Array};
22
use serde::{de::DeserializeOwned, Serialize};
33
use serde_json::Value;
4+
use serde_wasm_bindgen::Serializer;
45
use wasm_bindgen::{JsCast, JsValue};
56
use wasm_bindgen_futures::JsFuture;
67

78
use crate::{KvError, ListResponse};
89

910
/// A builder to configure put requests.
10-
#[derive(Debug, Clone, Serialize)]
11+
#[derive(Debug, Clone)]
1112
#[must_use = "PutOptionsBuilder does nothing until you 'execute' it"]
1213
pub struct PutOptionsBuilder {
13-
#[serde(skip)]
1414
pub(crate) this: Object,
15-
#[serde(skip)]
1615
pub(crate) put_function: Function,
17-
#[serde(skip)]
1816
pub(crate) name: JsValue,
19-
#[serde(skip)]
2017
pub(crate) value: JsValue,
21-
#[serde(skip_serializing_if = "Option::is_none")]
2218
pub(crate) expiration: Option<u64>,
19+
pub(crate) expiration_ttl: Option<u64>,
20+
pub(crate) metadata: Option<Value>,
21+
}
22+
23+
#[derive(Serialize)]
24+
struct PutOptions {
25+
#[serde(skip_serializing_if = "Option::is_none")]
26+
expiration: Option<u64>,
2327
#[serde(skip_serializing_if = "Option::is_none")]
2428
#[serde(rename = "expirationTtl")]
25-
pub(crate) expiration_ttl: Option<u64>,
29+
expiration_ttl: Option<u64>,
2630
#[serde(skip_serializing_if = "Option::is_none")]
27-
pub(crate) metadata: Option<Value>,
31+
metadata: Option<Value>,
2832
}
2933

3034
impl PutOptionsBuilder {
@@ -46,7 +50,15 @@ impl PutOptionsBuilder {
4650
}
4751
/// Puts the value in the kv store.
4852
pub async fn execute(self) -> Result<(), KvError> {
49-
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
53+
let ser = Serializer::json_compatible();
54+
let options_object = PutOptions {
55+
expiration: self.expiration,
56+
expiration_ttl: self.expiration_ttl,
57+
metadata: self.metadata,
58+
}
59+
.serialize(&ser)
60+
.map_err(JsValue::from)?;
61+
5062
let promise: Promise = self
5163
.put_function
5264
.call3(&self.this, &self.name, &self.value, &options_object)?
@@ -59,13 +71,18 @@ impl PutOptionsBuilder {
5971
}
6072

6173
/// A builder to configure list requests.
62-
#[derive(Debug, Clone, Serialize)]
74+
#[derive(Debug, Clone)]
6375
#[must_use = "ListOptionsBuilder does nothing until you 'execute' it"]
6476
pub struct ListOptionsBuilder {
65-
#[serde(skip)]
6677
pub(crate) this: Object,
67-
#[serde(skip)]
6878
pub(crate) list_function: Function,
79+
pub(crate) limit: Option<u64>,
80+
pub(crate) cursor: Option<String>,
81+
pub(crate) prefix: Option<String>,
82+
}
83+
84+
#[derive(Serialize)]
85+
struct ListOptions {
6986
#[serde(skip_serializing_if = "Option::is_none")]
7087
pub(crate) limit: Option<u64>,
7188
#[serde(skip_serializing_if = "Option::is_none")]
@@ -93,7 +110,15 @@ impl ListOptionsBuilder {
93110
}
94111
/// Lists the key value pairs in the kv store.
95112
pub async fn execute(self) -> Result<ListResponse, KvError> {
96-
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
113+
let ser = Serializer::json_compatible();
114+
let options_object = ListOptions {
115+
limit: self.limit,
116+
cursor: self.cursor,
117+
prefix: self.prefix,
118+
}
119+
.serialize(&ser)
120+
.map_err(JsValue::from)?;
121+
97122
let promise: Promise = self
98123
.list_function
99124
.call1(&self.this, &options_object)?
@@ -106,17 +131,19 @@ impl ListOptionsBuilder {
106131
}
107132

108133
/// A builder to configure get requests.
109-
#[derive(Debug, Clone, Serialize)]
134+
#[derive(Debug, Clone)]
110135
#[must_use = "GetOptionsBuilder does nothing until you 'get' it"]
111136
pub struct GetOptionsBuilder {
112-
#[serde(skip)]
113137
pub(crate) this: Object,
114-
#[serde(skip)]
115138
pub(crate) get_function: Function,
116-
#[serde(skip)]
117139
pub(crate) get_with_meta_function: Function,
118-
#[serde(skip)]
119140
pub(crate) name: JsValue,
141+
pub(crate) cache_ttl: Option<u64>,
142+
pub(crate) value_type: Option<GetValueType>,
143+
}
144+
145+
#[derive(Serialize)]
146+
struct GetOptions {
120147
#[serde(rename = "cacheTtl", skip_serializing_if = "Option::is_none")]
121148
pub(crate) cache_ttl: Option<u64>,
122149
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
@@ -141,8 +168,19 @@ impl GetOptionsBuilder {
141168
self
142169
}
143170

171+
fn options(&self) -> Result<JsValue, KvError> {
172+
let ser = Serializer::json_compatible();
173+
Ok(GetOptions {
174+
cache_ttl: self.cache_ttl,
175+
value_type: self.value_type,
176+
}
177+
.serialize(&ser)
178+
.map_err(JsValue::from)?)
179+
}
180+
144181
async fn get(self) -> Result<JsValue, KvError> {
145-
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
182+
let options_object = self.options()?;
183+
146184
let promise: Promise = self
147185
.get_function
148186
.call2(&self.this, &self.name, &options_object)?
@@ -185,7 +223,8 @@ impl GetOptionsBuilder {
185223
where
186224
M: DeserializeOwned,
187225
{
188-
let options_object = serde_wasm_bindgen::to_value(&self).map_err(JsValue::from)?;
226+
let options_object = self.options()?;
227+
189228
let promise: Promise = self
190229
.get_with_meta_function
191230
.call2(&self.this, &self.name, &options_object)?
@@ -257,7 +296,7 @@ impl GetOptionsBuilder {
257296
}
258297
}
259298

260-
#[derive(Debug, Clone, Serialize)]
299+
#[derive(Debug, Clone, Serialize, Copy)]
261300
#[serde(rename_all = "camelCase")]
262301
pub(crate) enum GetValueType {
263302
Text,

worker-kv/tests/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

worker-kv/tests/integration.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)