ossify 0.4.0

A modern, easy-to-use, and reqwest-powered Rust SDK for Alibaba Cloud Object Storage Service (OSS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! SelectObject: run a restricted SQL query over a CSV or JSON object.
//!
//! The request body is the same `<SelectRequest>` element for both formats;
//! pick the format by setting the matching input- and output-serialization
//! variants. The response is a stream of binary frames decoded by
//! [`super::frame::SelectFrame`].
//!
//! The SQL expression and the single-byte delimiters / quote characters
//! in the XML must all be base64-encoded — the public `new_csv(...)` and
//! `new_json(...)` constructors handle this for you.
//!
//! Official document: <https://www.alibabacloud.com/help/en/oss/developer-reference/selectobject>

use std::future::Future;

use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use http::Method;
use serde::Serialize;
use serde_with::skip_serializing_none;

use super::frame::SelectFrameStream;
use crate::body::XMLBody;
use crate::error::Result;
use crate::response::StreamResponseProcessor;
use crate::{Client, Ops, Prepared, Request};

/// Query parameters for [`SelectObject`].
#[derive(Debug, Clone, Serialize)]
pub struct SelectObjectParams {
    #[serde(rename = "x-oss-process")]
    x_oss_process: String,
}

impl SelectObjectParams {
    fn csv() -> Self {
        Self {
            x_oss_process: "csv/select".into(),
        }
    }

    fn json() -> Self {
        Self {
            x_oss_process: "json/select".into(),
        }
    }
}

// --------------------------------------------------------------------------
// Request XML types
// --------------------------------------------------------------------------

/// How to interpret the first line of a CSV object.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FileHeaderInfo {
    /// No header line.
    None,
    /// Treat the first line as a header but don't reference it in the query.
    Ignore,
    /// Treat the first line as a header and allow column-name references.
    Use,
}

/// JSON object layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum JsonType {
    /// Entire object is one JSON document.
    #[serde(rename = "DOCUMENT")]
    Document,
    /// One JSON object per line (newline-delimited).
    #[serde(rename = "LINES")]
    Lines,
}

/// Compression format of the source object.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum SelectCompressionType {
    None,
    #[serde(rename = "GZIP")]
    Gzip,
}

/// CSV-specific input serialization.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CsvInputSerialization {
    pub file_header_info: Option<FileHeaderInfo>,
    /// Base64-encoded record delimiter (≤ 2 bytes). Default `\n`.
    pub record_delimiter: Option<String>,
    /// Base64-encoded field delimiter (1 byte). Default `,`.
    pub field_delimiter: Option<String>,
    /// Base64-encoded quote character (1 byte). Default `"`.
    pub quote_character: Option<String>,
    /// Base64-encoded comment character (1 byte). Default empty.
    pub comment_character: Option<String>,
    /// `line-range=<start>-<end>` or `split-range=<start>-<end>`. Requires
    /// that `CreateSelectObjectMeta` has been called first.
    pub range: Option<String>,
    pub allow_quoted_record_delimiter: Option<bool>,
}

/// JSON-specific input serialization.
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct JsonInputSerialization {
    #[serde(rename = "Type")]
    pub json_type: JsonType,
    /// `line-range=<start>-<end>` (LINES only).
    pub range: Option<String>,
    pub parse_json_number_as_string: Option<bool>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SelectInputSerialization {
    pub compression_type: Option<SelectCompressionType>,
    #[serde(rename = "CSV")]
    pub csv: Option<CsvInputSerialization>,
    #[serde(rename = "JSON")]
    pub json: Option<JsonInputSerialization>,
}

/// CSV-specific output serialization.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CsvOutputSerialization {
    /// Base64-encoded record delimiter (≤ 2 bytes). Default `\n`.
    pub record_delimiter: Option<String>,
    /// Base64-encoded field delimiter (1 byte). Default `,`.
    pub field_delimiter: Option<String>,
}

/// JSON-specific output serialization.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct JsonOutputSerialization {
    /// Base64-encoded record delimiter (default `\n`).
    pub record_delimiter: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SelectOutputSerialization {
    #[serde(rename = "CSV")]
    pub csv: Option<CsvOutputSerialization>,
    #[serde(rename = "JSON")]
    pub json: Option<JsonOutputSerialization>,
    /// Return all columns in the CSV output even if some were not selected.
    pub keep_all_columns: Option<bool>,
    /// Return the response body as raw bytes without the frame envelope.
    /// **Incompatible with `enable_payload_crc = true`.**
    pub output_raw_data: Option<bool>,
    /// Attach a CRC-32 to each frame payload.
    pub enable_payload_crc: Option<bool>,
    /// Output the CSV header row as the first line of the response.
    pub output_header: Option<bool>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SelectOptions {
    pub skip_partial_data_record: Option<bool>,
    pub max_skipped_records_allowed: Option<u64>,
}

/// Root `<SelectRequest>` element.
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize)]
#[serde(rename = "SelectRequest", rename_all = "PascalCase")]
pub struct SelectRequest {
    /// Base64-encoded SQL expression.
    pub expression: String,
    pub input_serialization: SelectInputSerialization,
    pub output_serialization: SelectOutputSerialization,
    pub options: Option<SelectOptions>,
}

impl SelectRequest {
    /// Build a CSV Select request. The SQL `expression` is base64-encoded
    /// automatically.
    pub fn new_csv(
        expression: impl AsRef<str>,
        input: CsvInputSerialization,
        output: CsvOutputSerialization,
    ) -> Self {
        Self {
            expression: BASE64.encode(expression.as_ref().as_bytes()),
            input_serialization: SelectInputSerialization {
                compression_type: None,
                csv: Some(input),
                json: None,
            },
            output_serialization: SelectOutputSerialization {
                csv: Some(output),
                json: None,
                ..Default::default()
            },
            options: None,
        }
    }

    /// Build a JSON Select request. The SQL `expression` is base64-encoded
    /// automatically.
    pub fn new_json(
        expression: impl AsRef<str>,
        input: JsonInputSerialization,
        output: JsonOutputSerialization,
    ) -> Self {
        Self {
            expression: BASE64.encode(expression.as_ref().as_bytes()),
            input_serialization: SelectInputSerialization {
                compression_type: None,
                csv: None,
                json: Some(input),
            },
            output_serialization: SelectOutputSerialization {
                csv: None,
                json: Some(output),
                ..Default::default()
            },
            options: None,
        }
    }

    pub fn with_compression(mut self, compression: SelectCompressionType) -> Self {
        self.input_serialization.compression_type = Some(compression);
        self
    }

    pub fn with_options(mut self, options: SelectOptions) -> Self {
        self.options = Some(options);
        self
    }

    /// Convenience: enable frame-level payload CRC. The corresponding stream
    /// consumer should be constructed with the same flag.
    pub fn with_payload_crc(mut self, enable: bool) -> Self {
        self.output_serialization.enable_payload_crc = Some(enable);
        self
    }
}

/// Helper to base64-encode a single-byte delimiter such as `,`, `\n`, or `"`.
pub fn b64_delimiter(ch: &[u8]) -> String {
    BASE64.encode(ch)
}

// --------------------------------------------------------------------------
// Ops
// --------------------------------------------------------------------------

/// The `SelectObject` operation. `verify_payload_crc` is stored on the
/// operation struct so the returned `SelectFrameStream` can match the
/// request's `enable_payload_crc` setting without the caller repeating it.
pub struct SelectObject {
    pub key: String,
    pub request: SelectRequest,
    pub is_json: bool,
}

impl Ops for SelectObject {
    type Response = StreamResponseProcessor;
    type Body = XMLBody<SelectRequest>;
    type Query = SelectObjectParams;

    fn prepare(self) -> Result<Prepared<SelectObjectParams, SelectRequest>> {
        let query = if self.is_json {
            SelectObjectParams::json()
        } else {
            SelectObjectParams::csv()
        };
        Ok(Prepared {
            method: Method::POST,
            key: Some(self.key),
            query: Some(query),
            body: Some(self.request),
            ..Default::default()
        })
    }
}

pub trait SelectObjectOps {
    /// Run a CSV Select query and return a [`SelectFrameStream`] over the
    /// decoded frames. The caller must inspect the final `End` /
    /// `MetaEnd*` frame and treat any non-2xx `status` as a failure
    /// (the outer HTTP response may be 206 even when the select itself
    /// ultimately errored).
    ///
    /// Official document: <https://www.alibabacloud.com/help/en/oss/developer-reference/selectobject>
    fn select_object_csv(
        &self,
        key: impl Into<String>,
        request: SelectRequest,
    ) -> impl Future<Output = Result<SelectFrameStream<reqwest::Response>>>;

    /// Run a JSON Select query; see [`select_object_csv`] for details.
    fn select_object_json(
        &self,
        key: impl Into<String>,
        request: SelectRequest,
    ) -> impl Future<Output = Result<SelectFrameStream<reqwest::Response>>>;
}

impl SelectObjectOps for Client {
    async fn select_object_csv(
        &self,
        key: impl Into<String>,
        request: SelectRequest,
    ) -> Result<SelectFrameStream<reqwest::Response>> {
        let verify_crc = request.output_serialization.enable_payload_crc.unwrap_or(false);
        let resp = self
            .request(SelectObject {
                key: key.into(),
                request,
                is_json: false,
            })
            .await?;
        Ok(SelectFrameStream::new(resp, verify_crc))
    }

    async fn select_object_json(
        &self,
        key: impl Into<String>,
        request: SelectRequest,
    ) -> Result<SelectFrameStream<reqwest::Response>> {
        let verify_crc = request.output_serialization.enable_payload_crc.unwrap_or(false);
        let resp = self
            .request(SelectObject {
                key: key.into(),
                request,
                is_json: true,
            })
            .await?;
        Ok(SelectFrameStream::new(resp, verify_crc))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn params_csv_serialize() {
        assert_eq!(
            crate::ser::to_string(&SelectObjectParams::csv()).unwrap(),
            "x-oss-process=csv%2Fselect"
        );
    }

    #[test]
    fn params_json_serialize() {
        assert_eq!(
            crate::ser::to_string(&SelectObjectParams::json()).unwrap(),
            "x-oss-process=json%2Fselect"
        );
    }

    #[test]
    fn sql_expression_is_base64_encoded() {
        let req = SelectRequest::new_csv(
            "select * from ossobject where _1 > 10",
            CsvInputSerialization::default(),
            CsvOutputSerialization::default(),
        );
        // Verify the raw SQL is NOT in the serialized XML.
        let xml = quick_xml::se::to_string(&req).unwrap();
        assert!(!xml.contains("select *"));
        // And the base64 is present.
        let expected = BASE64.encode(b"select * from ossobject where _1 > 10");
        assert!(xml.contains(&format!("<Expression>{expected}</Expression>")));
    }

    #[test]
    fn csv_request_xml_round_trip() {
        let input = CsvInputSerialization {
            file_header_info: Some(FileHeaderInfo::Ignore),
            record_delimiter: Some(b64_delimiter(b"\n")),
            field_delimiter: Some(b64_delimiter(b",")),
            ..Default::default()
        };
        let output = CsvOutputSerialization {
            record_delimiter: Some(b64_delimiter(b"\n")),
            field_delimiter: Some(b64_delimiter(b",")),
        };
        let req = SelectRequest::new_csv("select _1 from ossobject", input, output).with_payload_crc(true);
        let xml = quick_xml::se::to_string(&req).unwrap();
        assert!(xml.contains("<FileHeaderInfo>Ignore</FileHeaderInfo>"));
        assert!(xml.contains("<EnablePayloadCrc>true</EnablePayloadCrc>"));
        // The shape must contain both <CSV> blocks (input + output).
        assert_eq!(xml.matches("<CSV>").count(), 2);
    }

    #[test]
    fn json_request_xml_uses_json_block() {
        let input = JsonInputSerialization {
            json_type: JsonType::Document,
            range: None,
            parse_json_number_as_string: None,
        };
        let output = JsonOutputSerialization {
            record_delimiter: Some(b64_delimiter(b"\n")),
        };
        let req = SelectRequest::new_json("select * from ossobject.records[*]", input, output);
        let xml = quick_xml::se::to_string(&req).unwrap();
        assert!(xml.contains("<Type>DOCUMENT</Type>"));
        assert_eq!(xml.matches("<JSON>").count(), 2);
        assert!(!xml.contains("<CSV>"));
    }

    #[test]
    fn b64_delimiter_matches_alicloud_examples() {
        // From the official documentation: '\n' -> Cg==, ',' -> LA==, '"' -> Ig==
        assert_eq!(b64_delimiter(b"\n"), "Cg==");
        assert_eq!(b64_delimiter(b","), "LA==");
        assert_eq!(b64_delimiter(b"\""), "Ig==");
    }
}