Skip to content

Commit 5b8ce42

Browse files
committed
add pretty xml and pretty json processor generators
1 parent 3c56630 commit 5b8ce42

File tree

8 files changed

+85
-6
lines changed

8 files changed

+85
-6
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
source "$(dirname "$0")/util/params.sh" || exit 255
4+
5+
function_source="fn %%FUNCTION_NAME%%(input: &Value, message: &mut OutputMessage) -> Result<(), ProcessingError> {
6+
if let Some(json) = input.get_val(##JSONPATH(%%SOURCE_FIELD%%)##)?
7+
.as_str()
8+
.map(|v| kafka_json_processor_core::formatters::json::pretty_json(v.to_string())) {
9+
10+
message.insert_val(##JSONPATH(%%TARGET_FIELD%%)##, Value::String(json))?;
11+
}
12+
Ok(())
13+
}
14+
"
15+
16+
required_param_to_var source_field
17+
required_param_to_var target_field
18+
19+
function_source="${function_source//"%%SOURCE_FIELD%%"/$source_field}"
20+
function_source="${function_source//"%%TARGET_FIELD%%"/$target_field}"
21+
function_source="${function_source//"%%FUNCTION_NAME%%"/$kjp_function_name}"
22+
23+
echo "OK"
24+
echo "$function_source"
25+
exit 0
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
source "$(dirname "$0")/util/params.sh" || exit 255
4+
5+
function_source="fn %%FUNCTION_NAME%%(input: &Value, message: &mut OutputMessage) -> Result<(), ProcessingError> {
6+
if let Some(xml) = input.get_val(##JSONPATH(%%SOURCE_FIELD%%)##)?
7+
.as_str()
8+
.map(|v| kafka_json_processor_core::formatters::xml::pretty_xml(v.to_string())) {
9+
10+
message.insert_val(##JSONPATH(%%TARGET_FIELD%%)##, Value::String(xml))?;
11+
}
12+
Ok(())
13+
}
14+
"
15+
16+
required_param_to_var source_field
17+
required_param_to_var target_field
18+
19+
function_source="${function_source//"%%SOURCE_FIELD%%"/$source_field}"
20+
function_source="${function_source//"%%TARGET_FIELD%%"/$target_field}"
21+
function_source="${function_source//"%%FUNCTION_NAME%%"/$kjp_function_name}"
22+
23+
echo "OK"
24+
echo "$function_source"
25+
exit 0

kjp-generator/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mod test {
9292

9393
#[test]
9494
fn should_read() {
95-
let result = read_template("../template-examples/correct.yaml");
95+
let result = read_template("../template-examples/basic.yaml");
9696

9797
assert!(result.is_ok());
9898
assert_eq!(Template {

kjp-generator/src/project.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ use serde_json::Value;
7979
use kafka_json_processor_core::processor::{ObjectKey, ObjectTree, OutputMessage};
8080
use kafka_json_processor_core::error::{ProcessingError, ErrorKind};
8181
use kafka_json_processor_core::{run_processor, Stream};
82-
use kafka_json_processor_core::formatters::json::pretty_json;
83-
use kafka_json_processor_core::formatters::xml::pretty_xml;
8482
use kafka_json_processor_core::processor::ObjectKey::{Key, Index};
8583
8684
fn main() {
@@ -188,8 +186,6 @@ use serde_json::Value;
188186
use kafka_json_processor_core::processor::{ObjectKey, ObjectTree, OutputMessage};
189187
use kafka_json_processor_core::error::{ProcessingError, ErrorKind};
190188
use kafka_json_processor_core::{run_processor, Stream};
191-
use kafka_json_processor_core::formatters::json::pretty_json;
192-
use kafka_json_processor_core::formatters::xml::pretty_xml;
193189
use kafka_json_processor_core::processor::ObjectKey::{Key, Index};
194190
195191
fn main() {

kjp-generator/tests/integration_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn generate_and_build() {
1111
.init();
1212
let root = root_project_dir();
1313

14-
let input_template = root.join("template-examples/correct.yaml");
14+
let input_template = root.join("template-examples/all_processors.yaml");
1515
let output_dir = root.join("test-output");
1616
let generator_dir = root.join("kjp-generator-generators");
1717

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Input]
2+
{
3+
"xml_message": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><breakfast_menu></breakfast_menu>",
4+
"json_message": "{\"test\":\"field\"}"
5+
}
6+
7+
[Expected]
8+
{
9+
"hello": "world",
10+
"pretty_xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<breakfast_menu>\n</breakfast_menu>",
11+
"pretty_json": "{\n \"test\": \"field\"\n}"
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: "Example processor"
2+
streams:
3+
- input_topic: in
4+
output_topic: out
5+
6+
processors:
7+
- generator: static_field
8+
field: $.hello
9+
value: world
10+
11+
- generator: copy_field
12+
source_field: $.abc[1]
13+
target_field: $.def
14+
15+
- generator: pretty_xml
16+
source_field: $.xml_message
17+
target_field: $.pretty_xml
18+
19+
- generator: pretty_json
20+
source_field: $.json_message
21+
target_field: $.pretty_json

0 commit comments

Comments
 (0)