Skip to content
Merged
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
@@ -0,0 +1,32 @@
package io.cloudquery.transformers;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.cloudquery.caser.Caser;

import javax.xml.transform.TransformerException;
import java.lang.reflect.Field;

public interface NameTransformer {
class DefaultNameTransformer implements NameTransformer {
private final Caser caser = Caser.builder().build();

/**
* Transforms the field name to the name of the property in the JSON
* or use the {@link Caser#toSnake(String)} if no annotation.
*
* @param field Field to transform
* @return Transformed field name
* @throws TransformerException If the field name cannot be transformed
*/
@Override
public String transform(Field field) throws TransformerException {
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
if (annotation != null) {
return annotation.value();
}
return caser.toSnake(field.getName());
}
}

String transform(Field field) throws TransformerException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.cloudquery.transformers;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.cloudquery.transformers.NameTransformer.DefaultNameTransformer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.xml.transform.TransformerException;
import java.lang.reflect.Field;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultNameTransformerTest {

private DefaultNameTransformer transformer;

@SuppressWarnings("unused")
private static class SimpleClass {
// Simple fields with no custom property mapping
private String simpleField;
private String aLongerFieldName;

// Fields with custom property mapping
@JsonProperty("id")
private String userID;
}

@BeforeEach
void setUp() {
transformer = new DefaultNameTransformer();
}

@Test
public void shouldReturnSnakeCaseFieldNamesByDefault() throws TransformerException {
Field[] declaredFields = SimpleClass.class.getDeclaredFields();

// Simple fields with no custom property mapping
assertEquals("simple_field", transformer.transform(declaredFields[0]));
assertEquals("a_longer_field_name", transformer.transform(declaredFields[1]));

// Fields with custom property mapping
assertEquals("id", transformer.transform(declaredFields[2]));
}
}