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
use super::path::AbsolutePath;
use serde_json::Value;
use std::collections::HashMap;
pub(crate) fn resolve_values(json: &Value, context: &HashMap<AbsolutePath, Value>) -> Value {
match json {
Value::Object(map) => {
let mut resolved_map = serde_json::Map::new();
for (key, value) in map {
let resolved_value = resolve_values(value, context);
resolved_map.insert(key.clone(), resolved_value);
}
Value::Object(resolved_map)
}
Value::Array(arr) => Value::Array(arr.iter().map(|v| resolve_values(v, context)).collect()),
Value::String(text) => {
// Check if the string is a single dependency like "{/absolute_path}"
if let Some(dependency_path) = extract_dependency(&text) {
// If it's a dependency, directly replace the value and type
if let Some(resolved_value) = context.get(&dependency_path) {
return resolved_value.clone(); // Resolve to the dependency value and type
}
}
// Handle embedded dependencies (e.g., "Hello {path}")
resolve_embedded_refs(text, context)
}
_ => json.clone(), // Leave other types of JSON values untouched
}
}
/// Extracts a dependency path from a string in the format "{path}".
/// Returns an AbsolutePath if the string is a valid single dependency, otherwise None.
fn extract_dependency(text: &str) -> Option<AbsolutePath> {
// Check if the entire string is in the format "{dependency_path}"
if text.starts_with('{') && text.ends_with('}') && text.len() > 2 {
let path = &text[1..text.len() - 1]; // Extract the path between '{' and '}'
return Some(AbsolutePath::new(path));
}
None
}
/// Resolves embedded references in a string, such as "Hello {path}".
/// This keeps the input as a string and replaces any "{dependency_path}" references within it.
fn resolve_embedded_refs(text: &str, source_map: &HashMap<AbsolutePath, Value>) -> Value {
let mut resolved_text = text.to_string();
let mut start_pos = 0;
while let Some(start) = resolved_text[start_pos..].find('{') {
let absolute_start = start_pos + start;
if let Some(end) = resolved_text[absolute_start..].find('}') {
let absolute_end = absolute_start + end;
let reference = &resolved_text[absolute_start + 1..absolute_end];
// Check if it's an absolute path
if reference.starts_with('/') {
let absolute_path = AbsolutePath::new(reference);
if let Some(resolved_value) = source_map.get(&absolute_path) {
if let Value::String(resolved_string) = resolved_value {
// Replace the dependency with the resolved string slice
resolved_text
.replace_range(absolute_start..=absolute_end, resolved_string.as_str());
start_pos = absolute_start + resolved_string.len();
continue;
}
}
}
start_pos = absolute_end + 1;
} else {
break; // No closing brace found
}
}
Value::String(resolved_text)
}
// Recursive function for resolving the template
pub(crate) fn resolve_recursive(
template: &Value,
source_map: &HashMap<AbsolutePath, Value>,
) -> Value {
match template {
Value::Object(template_map) => {
// Traverse the template map
let mut resolved_map = serde_json::Map::new();
for (key, value) in template_map {
resolved_map.insert(key.clone(), resolve_recursive(value, source_map));
}
Value::Object(resolved_map)
}
Value::Array(template_array) => {
// Traverse the template array
Value::Array(
template_array
.iter()
.map(|value| resolve_recursive(value, source_map))
.collect(),
)
}
Value::String(text) => {
// Handle standalone dependency (e.g., "{/absolute/path}")
if let Some(absolute_path) = extract_dependency(text) {
if let Some(resolved_value) = source_map.get(&absolute_path) {
return resolved_value.clone(); // Fully resolve the standalone dependency
}
}
// Handle embedded dependencies
resolve_embedded_refs(text, source_map)
}
_ => template.clone(), // Return all other values unchanged
}
}
#[cfg(test)]
mod tests {
use super::AbsolutePath;
use super::*;
use std::collections::HashMap;
#[test]
fn test_resolve_embedded_refs() {
// Sample source_map
let source_map: HashMap<AbsolutePath, Value> = HashMap::from([
(
AbsolutePath::new("/data/key1"),
Value::String("Value 1".to_string()),
),
(
AbsolutePath::new("/data/key2"),
Value::String("Value 2".to_string()),
),
(
AbsolutePath::new("/nested/object"),
Value::String("Nested Value".to_string()),
),
]);
// Input containing embedded dependencies
let input = "This is {/data/key1}, and here is {/data/key2}. Lastly: {/nested/object}";
// Resolve the embedded dependencies
let result = resolve_embedded_refs(input, &source_map);
// Expected resolved output
let expected =
Value::String("This is Value 1, and here is Value 2. Lastly: Nested Value".to_string());
// Validate the result matches the expected output
assert_eq!(result, expected);
}
#[test]
fn test_resolve_embedded_refs_with_invalid_paths() {
// Sample source_map with correct absolute paths
let source_map: HashMap<AbsolutePath, Value> = HashMap::from([
(
AbsolutePath::new("/data/key1"),
Value::String("Resolved Key1".to_string()),
),
(
AbsolutePath::new("/nested/object"),
Value::String("Nested Object Resolved".to_string()),
),
]);
// Input contains a mix of valid and invalid placeholders
let input = "Valid: {/data/key1}, Invalid: {relative/key}, Another: {/nested/object}, Broken: {missing_brace";
// Resolve the embedded placeholders
let result = resolve_embedded_refs(input, &source_map);
// Expected output
let expected = Value::String(
"Valid: Resolved Key1, Invalid: {relative/key}, Another: Nested Object Resolved, Broken: {missing_brace".to_string(),
);
// Assert that the result matches the expected output
assert_eq!(result, expected);
}
#[test]
fn test_resolve_values_with_absolute_paths() {
let json = serde_json::json!({
"config": {
"level1": {
"key1": "{/config/level1/key1_resolved}", // Simple reference to a string
"key2": "{/config/level1/key2_resolved}", // Reference to a number
"key3": "{/config/level2/key3_resolved}", // Reference to an array
"key4": "Embedded {/config/level2/key4_resolved} Example", // Embedded reference
"key5": "{/config/level2/key5_resolved}" // Reference to an object
},
"level2": {
"key3_resolved": [10, 20, 30], // Array
"key4_resolved": "value_here", // String to embed
"key5_resolved": { "nested_key": "nested_value" } // Object
}
}
});
// Context with predefined resolved values for absolute paths
let context = HashMap::from([
(
AbsolutePath::new("/config/level1/key1_resolved"),
Value::String("Resolved Value for Key1".to_string()),
),
(
AbsolutePath::new("/config/level1/key2_resolved"),
Value::Number(123.into()), // Number
),
(
AbsolutePath::new("/config/level2/key3_resolved"),
Value::Array(vec![
Value::Number(10.into()),
Value::Number(20.into()),
Value::Number(30.into()),
]),
),
(
AbsolutePath::new("/config/level2/key4_resolved"),
Value::String("value_here".to_string()), // For embedded reference
),
(
AbsolutePath::new("/config/level2/key5_resolved"),
Value::Object(serde_json::Map::from_iter(vec![(
"nested_key".to_string(),
Value::String("nested_value".to_string()),
)])),
),
]);
let resolved_json = resolve_values(&json, &context);
// Expected JSON after resolving all dependencies
let expected_resolved = serde_json::json!({
"config": {
"level1": {
"key1": "Resolved Value for Key1",
"key2": 123,
"key3": [10, 20, 30],
"key4": "Embedded value_here Example",
"key5": { "nested_key": "nested_value" }
},
"level2": {
"key3_resolved": [10, 20, 30],
"key4_resolved": "value_here",
"key5_resolved": { "nested_key": "nested_value" }
}
}
});
assert_eq!(resolved_json, expected_resolved);
}
#[test]
fn test_resolve_values_with_missing_path() {
let json = serde_json::json!({
"config": {
"key1": "value1", // Regular value
"key2": "{/config/key3}", // Reference to a missing path
"key3": 42, // Existing value, but not used in the context
"key4": "Hello {/missing/path}" // Embedded reference to a missing path
}
});
// Context that omits some paths intentionally
let context = HashMap::from([
// Only define `/config/key1` in the context
(
AbsolutePath::new("/config/key1"),
Value::String("value1".to_string()),
),
]);
// Resolve the values in the JSON based on the context
let resolved_json = resolve_values(&json, &context);
// Expected JSON after resolving what is resolvable
let expected_resolved = serde_json::json!({
"config": {
"key1": "value1", // Resolved correctly
"key2": "{/config/key3}", // Missing path remains unresolved
"key3": 42, // Key untouched as it is not referenced
"key4": "Hello {/missing/path}" // Embedded missing path remains unresolved
}
});
// Assert that the resolved JSON matches the expected result
assert_eq!(resolved_json, expected_resolved);
}
#[test]
fn test_resolve_values_simple_case() {
let json = serde_json::json!({
"posting_config": {
"published_message_caption": "Link {/posting_config/invite_group_link} and {/posting_config/other_key}",
"invite_group_link": "This should be replaced",
"other_key": "This too"
}
});
// Context containing paths to resolve and their corresponding values
let context = HashMap::from([
(
AbsolutePath::new("/posting_config/invite_group_link"),
Value::String("Invite Link".to_string()),
),
(
AbsolutePath::new("/posting_config/other_key"),
Value::String("Other Value".to_string()),
),
]);
// Perform resolution
let resolved_json = resolve_values(&json, &context);
// Expected JSON after resolving the references
let expected_resolved = serde_json::json!({
"posting_config": {
"published_message_caption": "Link Invite Link and Other Value",
"invite_group_link": "This should be replaced",
"other_key": "This too"
}
});
// Assert that the resolved JSON matches the expected result
assert_eq!(resolved_json, expected_resolved);
}
#[test]
fn test_resolve_values_with_nested_dependencies() {
let json = serde_json::json!({
"config": {
"level1": {
"key1": "{/config/level2/key4}", // Ссылка на числовое значение
"key2": "{/config/level2/key5}", // Ссылка на массив
"key3": "{/config/level2/key6}", // Ссылка на объект
"key4": "Hello {/config/level2/key7}" // Вложенная ссылка в строке
},
"level2": {
"key4": 100,
"key5": [1, 2, 3],
"key6": {"nested_key": "nested_value"},
"key7": "World"
}
}
});
// Контекст с разрешёнными значениями (разных типов)
let context = HashMap::from([
(
AbsolutePath::new("/config/level2/key4"),
Value::Number(100.into()),
),
(
AbsolutePath::new("/config/level2/key5"),
Value::Array(vec![
Value::Number(1.into()),
Value::Number(2.into()),
Value::Number(3.into()),
]),
),
(
AbsolutePath::new("/config/level2/key6"),
Value::Object(serde_json::Map::from_iter(vec![(
"nested_key".to_string(),
Value::String("nested_value".to_string()),
)])),
),
(
AbsolutePath::new("/config/level2/key7"),
Value::String("World".to_string()),
),
]);
let resolved_json = resolve_values(&json, &context);
let expected_resolved = serde_json::json!({
"config": {
"level1": {
"key1": 100,
"key2": [1, 2, 3],
"key3": {"nested_key": "nested_value"},
"key4": "Hello World"
},
"level2": {
"key4": 100,
"key5": [1, 2, 3],
"key6": {"nested_key": "nested_value"},
"key7": "World"
}
}
});
assert_eq!(resolved_json, expected_resolved);
}
}