Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement processMap with recursion on ChangeCase #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -56,6 +57,27 @@ public void configure(Map<String, ?> map) {

Map<Schema, Schema> schemaState = new HashMap<>();

@Override
protected SchemaAndValue processMap(R record, Map<String, Object> input) {
return new SchemaAndValue(null, convertMap(input));
}

private Object convertMap(Map<String, Object> input) {
final Map<String, Object> outputMap = new LinkedHashMap<>(input.size());

for (final String inputFieldName : input.keySet()) {
log.trace("processMap() - Processing field '{}'", inputFieldName);
final String outputFieldName = this.config.from.to(this.config.to, inputFieldName);
Object value = input.get(inputFieldName);
if (value instanceof Map) {
value = convertMap((Map<String, Object>) value);
}
outputMap.put(outputFieldName, value);
}

return outputMap;
}

@Override
protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct input) {
final Schema outputSchema = this.schemaState.computeIfAbsent(inputSchema, schema -> convertSchema(schema));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

import static com.github.jcustenborder.kafka.connect.transform.common.GenericAssertions.assertMap;
import static com.github.jcustenborder.kafka.connect.utils.AssertSchema.assertSchema;
import static com.github.jcustenborder.kafka.connect.utils.AssertStruct.assertStruct;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -58,6 +60,47 @@ public void test() {
}
}

@Test
public void testSchemaLess() {
this.transformation.configure(
ImmutableMap.of(ChangeCaseConfig.FROM_CONFIG, CaseFormat.UPPER_CAMEL.toString(),
ChangeCaseConfig.TO_CONFIG, CaseFormat.LOWER_UNDERSCORE.toString()));

final Map<String, Object> input = ImmutableMap.of(
"Field", "first value",
"NestingField", ImmutableMap.of(
"secondDepthField", "second depth",
"secondDepthNestingField", ImmutableMap.of(
"thirdDepthField", "third depth"
)
)
);
final Map<String, Object> expected = ImmutableMap.of(
"field", "first value",
"nesting_field", ImmutableMap.of(
"second_depth_field", "second depth",
"second_depth_nesting_field", ImmutableMap.of(
"third_depth_field", "third depth"
)
)
);
final SinkRecord inputRecord = new SinkRecord(
TOPIC,
1,
null,
null,
null,
input,
1L
);

final SinkRecord transformedRecord = this.transformation.apply(inputRecord);
assertNotNull(transformedRecord, "transformedRecord should not be null.");
@SuppressWarnings("unchecked")
final Map<String, Object> actual = (Map<String, Object>) transformedRecord.value();
assertMap(expected, actual, "ChangeCase is not working on schema-less input, as expected");
}

private Schema makeSchema(CaseFormat caseFormat) {
final Function<String, String> convert = s -> CaseFormat.LOWER_UNDERSCORE.to(caseFormat, s);
return SchemaBuilder.struct().field(convert.apply("contacts"),
Expand Down