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
//! Row-path parsing and extraction.
//!
//! A source-pack table declares a fixed *row path* — the JSON location of
//! the row array inside an action's response envelope (`$.issues`,
//! `$.data.items`). Paths are deliberately limited to object-key segments:
//! they are relational contracts maintained by Skardi, not a user query
//! language, so arrays/wildcards are out of scope.
use serde_json::Value;
use super::error::OpenConnectorError;
/// A parsed row path (`$.key[.key…]`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RowPath {
raw: String,
segments: Vec<String>,
}
impl RowPath {
/// Parse a row path. Segments must be non-empty object keys; the path
/// must have at least one segment (`$` alone is not a row location).
///
/// # Example
/// ```
/// use skardi::sources::providers::open_connector::row_path::RowPath;
///
/// let path = RowPath::parse("$.data.items").unwrap();
/// assert_eq!(path.as_str(), "$.data.items");
/// assert!(RowPath::parse("data.items").is_err()); // missing `$.` prefix
/// assert!(RowPath::parse("$").is_err()); // no segments
/// ```
pub fn parse(path: &str) -> Result<Self, OpenConnectorError> {
let body = path
.strip_prefix("$.")
.ok_or_else(|| OpenConnectorError::InvalidRowPath {
path: path.to_string(),
reason: "must start with '$.'".to_string(),
})?;
let segments: Vec<String> = body.split('.').map(str::to_string).collect();
if segments.iter().any(|s| s.is_empty()) {
return Err(OpenConnectorError::InvalidRowPath {
path: path.to_string(),
reason: "segments must be non-empty object keys".to_string(),
});
}
Ok(Self {
raw: path.to_string(),
segments,
})
}
/// The path as written, e.g. `$.data.items`.
pub fn as_str(&self) -> &str {
&self.raw
}
/// The object-key segments, in traversal order.
pub fn segments(&self) -> impl Iterator<Item = &str> {
self.segments.iter().map(String::as_str)
}
/// Walk the path inside `value`. A key absent from an object fails with
/// [`OpenConnectorError::RowPathNotFound`]; a present non-object where
/// the path must descend fails with
/// [`OpenConnectorError::RowPathNotObject`]. `page` is the 1-based page
/// number, carried into the error for scan diagnostics.
pub fn extract<'a>(
&self,
value: &'a Value,
page: usize,
) -> Result<&'a Value, OpenConnectorError> {
let mut current = value;
for segment in &self.segments {
let map = current
.as_object()
.ok_or_else(|| OpenConnectorError::RowPathNotObject {
path: self.raw.clone(),
segment: segment.clone(),
page,
found: json_kind(current),
})?;
current = map
.get(segment)
.ok_or_else(|| OpenConnectorError::RowPathNotFound {
path: self.raw.clone(),
segment: segment.clone(),
page,
})?;
}
Ok(current)
}
/// Extract the row array at the path. An absent path fails with
/// [`OpenConnectorError::RowPathNotFound`]; a present non-array target
/// fails with [`OpenConnectorError::RowPathNotArray`].
pub fn rows<'a>(
&self,
value: &'a Value,
page: usize,
) -> Result<&'a [Value], OpenConnectorError> {
let target = self.extract(value, page)?;
target
.as_array()
.map(Vec::as_slice)
.ok_or_else(|| OpenConnectorError::RowPathNotArray {
path: self.raw.clone(),
page,
found: json_kind(target),
})
}
}
/// Short human-readable kind of a JSON value, for error messages.
pub(crate) fn json_kind(value: &Value) -> String {
match value {
Value::Null => "null".to_string(),
Value::Bool(_) => "a boolean".to_string(),
Value::Number(_) => "a number".to_string(),
Value::String(_) => "a string".to_string(),
Value::Array(_) => "an array".to_string(),
Value::Object(_) => "an object".to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn parses_dotted_paths() {
let path = RowPath::parse("$.data.items").unwrap();
assert_eq!(path.as_str(), "$.data.items");
}
#[test]
fn rejects_paths_without_prefix_or_empty_segments() {
for bad in ["data.items", "$", "$.", "$.a..b", "$.a."] {
assert!(
matches!(
RowPath::parse(bad),
Err(OpenConnectorError::InvalidRowPath { .. })
),
"{bad} should be rejected"
);
}
}
#[test]
fn rows_extracts_nested_array() {
let page = json!({"data": {"items": [{"id": 1}, {"id": 2}]}});
let rows = RowPath::parse("$.data.items")
.unwrap()
.rows(&page, 1)
.unwrap();
assert_eq!(rows.len(), 2);
}
#[test]
fn missing_key_fails_with_segment_and_page() {
let page = json!({"data": {}});
let err = RowPath::parse("$.data.items")
.unwrap()
.rows(&page, 3)
.unwrap_err();
assert!(matches!(
err,
OpenConnectorError::RowPathNotFound { ref segment, page: 3, .. }
if segment == "items"
));
}
#[test]
fn traversing_a_non_object_fails() {
let page = json!({"data": [1, 2, 3]});
let err = RowPath::parse("$.data.items")
.unwrap()
.rows(&page, 1)
.unwrap_err();
assert!(matches!(
err,
OpenConnectorError::RowPathNotObject { ref segment, ref found, .. }
if segment == "items" && found == "an array"
));
}
#[test]
fn non_array_target_fails_with_kind() {
let page = json!({"items": {"count": 2}});
let err = RowPath::parse("$.items")
.unwrap()
.rows(&page, 2)
.unwrap_err();
assert!(matches!(
err,
OpenConnectorError::RowPathNotArray { page: 2, ref found, .. }
if found == "an object"
));
}
}