tmpltool 1.5.0

A fast and simple command-line template rendering tool using MiniJinja templates with environment variables
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
use minijinja::Value;
use minijinja::value::Kwargs;
use tmpltool::functions::Function;
use tmpltool::functions::array::{ArrayGroupBy, ArraySortBy};

// ============================================================================
// Array Sort By Tests
// ============================================================================

#[test]
fn test_array_sort_by_numeric() {
    let users = serde_json::json!([
        {"name": "Alice", "age": 30},
        {"name": "Bob", "age": 25},
        {"name": "Charlie", "age": 35}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&users)),
        ("key", Value::from("age")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result[0]["age"], 25);
    assert_eq!(json_result[1]["age"], 30);
    assert_eq!(json_result[2]["age"], 35);
}

#[test]
fn test_array_sort_by_string() {
    let users = serde_json::json!([
        {"name": "Charlie", "age": 30},
        {"name": "Alice", "age": 25},
        {"name": "Bob", "age": 35}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&users)),
        ("key", Value::from("name")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result[0]["name"], "Alice");
    assert_eq!(json_result[1]["name"], "Bob");
    assert_eq!(json_result[2]["name"], "Charlie");
}

#[test]
fn test_array_sort_by_missing_key() {
    let users = serde_json::json!([
        {"name": "Alice", "age": 30},
        {"name": "Bob"},
        {"name": "Charlie", "age": 25}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&users)),
        ("key", Value::from("age")),
    ]))
    .unwrap();

    // Items with missing keys should be sorted to the end
    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result[0]["age"], 25);
    assert_eq!(json_result[1]["age"], 30);
    assert_eq!(json_result[2]["name"], "Bob");
}

#[test]
fn test_array_sort_by_empty_array() {
    let empty: Vec<serde_json::Value> = vec![];

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&empty)),
        ("key", Value::from("age")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_array().unwrap().len(), 0);
}

#[test]
fn test_array_sort_by_error_not_array() {
    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from("test")),
        ("key", Value::from("age")),
    ]));

    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("requires an array")
    );
}

#[test]
fn test_array_sort_by_missing_key_param() {
    let users = serde_json::json!([{"name": "Alice"}]);
    let result = ArraySortBy::call(Kwargs::from_iter(vec![(
        "array",
        Value::from_serialize(&users),
    )]));

    assert!(result.is_err());
}

// ============================================================================
// Array Group By Tests
// ============================================================================

#[test]
fn test_array_group_by_basic() {
    let users = serde_json::json!([
        {"name": "Alice", "dept": "Engineering"},
        {"name": "Bob", "dept": "Sales"},
        {"name": "Charlie", "dept": "Engineering"}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&users)),
        ("key", Value::from("dept")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    let engineering = json_result["Engineering"].as_array().unwrap();
    let sales = json_result["Sales"].as_array().unwrap();

    assert_eq!(engineering.len(), 2);
    assert_eq!(sales.len(), 1);
}

#[test]
fn test_array_group_by_numeric_key() {
    let items = serde_json::json!([
        {"name": "Item1", "priority": 1},
        {"name": "Item2", "priority": 2},
        {"name": "Item3", "priority": 1}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("priority")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    let group1 = json_result["1"].as_array().unwrap();
    let group2 = json_result["2"].as_array().unwrap();

    assert_eq!(group1.len(), 2);
    assert_eq!(group2.len(), 1);
}

#[test]
fn test_array_group_by_boolean_key() {
    let items = serde_json::json!([
        {"name": "Item1", "active": true},
        {"name": "Item2", "active": false},
        {"name": "Item3", "active": true}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("active")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    let active = json_result["true"].as_array().unwrap();
    let inactive = json_result["false"].as_array().unwrap();

    assert_eq!(active.len(), 2);
    assert_eq!(inactive.len(), 1);
}

#[test]
fn test_array_group_by_empty_array() {
    let empty: Vec<serde_json::Value> = vec![];

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&empty)),
        ("key", Value::from("dept")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_object().unwrap().len(), 0);
}

#[test]
fn test_array_group_by_error_not_array() {
    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from(42)),
        ("key", Value::from("dept")),
    ]));

    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("requires an array")
    );
}

#[test]
fn test_array_group_by_missing_key_param() {
    let users = serde_json::json!([{"name": "Alice"}]);
    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![(
        "array",
        Value::from_serialize(&users),
    )]));

    assert!(result.is_err());
}

// Note: Array Unique and Array Flatten tests removed - functions now in filter_functions/array.rs
// See tests/test_array_filter_functions.rs for tests of these functions

// ============================================================================
// Additional Array Sort By Edge Cases
// ============================================================================

#[test]
fn test_array_sort_by_all_missing_key() {
    let users = serde_json::json!([
        {"name": "Alice"},
        {"name": "Bob"},
        {"name": "Charlie"}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&users)),
        ("key", Value::from("age")),
    ]))
    .unwrap();

    // All have missing key, should maintain relative order
    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_array().unwrap().len(), 3);
}

#[test]
fn test_array_sort_by_some_missing_key() {
    let items = serde_json::json!([
        {"name": "First"},
        {"name": "Second", "order": 1},
        {"name": "Third"},
        {"name": "Fourth", "order": 2}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("order")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    // Items with key should come first (sorted), items without key should come at end
    assert_eq!(json_result[0]["order"], 1);
    assert_eq!(json_result[1]["order"], 2);
}

#[test]
fn test_array_sort_by_null_values() {
    let items = serde_json::json!([
        {"name": "First", "order": null},
        {"name": "Second", "order": 1},
        {"name": "Third", "order": 2}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("order")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_array().unwrap().len(), 3);
}

#[test]
fn test_array_sort_by_boolean_values() {
    let items = serde_json::json!([
        {"name": "First", "active": true},
        {"name": "Second", "active": false},
        {"name": "Third", "active": true}
    ]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("active")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_array().unwrap().len(), 3);
}

#[test]
fn test_array_sort_by_single_item() {
    let items = serde_json::json!([{"name": "Only", "age": 25}]);

    let result = ArraySortBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("age")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_array().unwrap().len(), 1);
    assert_eq!(json_result[0]["age"], 25);
}

#[test]
fn test_array_sort_by_missing_array_param() {
    let result = ArraySortBy::call(Kwargs::from_iter(vec![("key", Value::from("name"))]));

    assert!(result.is_err());
}

// ============================================================================
// Additional Array Group By Edge Cases
// ============================================================================

#[test]
fn test_array_group_by_null_key_value() {
    let items = serde_json::json!([
        {"name": "First", "category": null},
        {"name": "Second", "category": "A"},
        {"name": "Third", "category": null}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("category")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    // Nulls should be grouped under "null" key
    assert!(json_result.get("null").is_some() || json_result.get("A").is_some());
}

#[test]
fn test_array_group_by_missing_key_in_some_items() {
    let items = serde_json::json!([
        {"name": "First", "dept": "Engineering"},
        {"name": "Second"},
        {"name": "Third", "dept": "Sales"}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("dept")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    // Only items with the key should be included in groups
    let total_items: usize = json_result
        .as_object()
        .unwrap()
        .values()
        .map(|v| v.as_array().unwrap().len())
        .sum();
    assert_eq!(total_items, 2);
}

#[test]
fn test_array_group_by_single_group() {
    let items = serde_json::json!([
        {"name": "First", "type": "A"},
        {"name": "Second", "type": "A"},
        {"name": "Third", "type": "A"}
    ]);

    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![
        ("array", Value::from_serialize(&items)),
        ("key", Value::from("type")),
    ]))
    .unwrap();

    let json_result: serde_json::Value = serde_json::to_value(&result).unwrap();
    assert_eq!(json_result.as_object().unwrap().len(), 1);
    assert_eq!(json_result["A"].as_array().unwrap().len(), 3);
}

#[test]
fn test_array_group_by_missing_array_param() {
    let result = ArrayGroupBy::call(Kwargs::from_iter(vec![("key", Value::from("dept"))]));

    assert!(result.is_err());
}

// Note: array_unique and array_flatten tests removed - these functions are now
// in filter_functions/array.rs with dual function+filter syntax support.
// See tests/test_filters_integration.rs for integration tests of these filters.