Skip to content

Commit 060a628

Browse files
authored
chore: adding NameTransformer interface and default implementation (#30)
fixes: #29
1 parent 68d489a commit 060a628

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.cloudquery.transformers;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.cloudquery.caser.Caser;
5+
6+
import javax.xml.transform.TransformerException;
7+
import java.lang.reflect.Field;
8+
9+
public interface NameTransformer {
10+
class DefaultNameTransformer implements NameTransformer {
11+
private final Caser caser = Caser.builder().build();
12+
13+
/**
14+
* Transforms the field name to the name of the property in the JSON
15+
* or use the {@link Caser#toSnake(String)} if no annotation.
16+
*
17+
* @param field Field to transform
18+
* @return Transformed field name
19+
* @throws TransformerException If the field name cannot be transformed
20+
*/
21+
@Override
22+
public String transform(Field field) throws TransformerException {
23+
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
24+
if (annotation != null) {
25+
return annotation.value();
26+
}
27+
return caser.toSnake(field.getName());
28+
}
29+
}
30+
31+
String transform(Field field) throws TransformerException;
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.cloudquery.transformers;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.cloudquery.transformers.NameTransformer.DefaultNameTransformer;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import javax.xml.transform.TransformerException;
9+
import java.lang.reflect.Field;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class DefaultNameTransformerTest {
14+
15+
private DefaultNameTransformer transformer;
16+
17+
@SuppressWarnings("unused")
18+
private static class SimpleClass {
19+
// Simple fields with no custom property mapping
20+
private String simpleField;
21+
private String aLongerFieldName;
22+
23+
// Fields with custom property mapping
24+
@JsonProperty("id")
25+
private String userID;
26+
}
27+
28+
@BeforeEach
29+
void setUp() {
30+
transformer = new DefaultNameTransformer();
31+
}
32+
33+
@Test
34+
public void shouldReturnSnakeCaseFieldNamesByDefault() throws TransformerException {
35+
Field[] declaredFields = SimpleClass.class.getDeclaredFields();
36+
37+
// Simple fields with no custom property mapping
38+
assertEquals("simple_field", transformer.transform(declaredFields[0]));
39+
assertEquals("a_longer_field_name", transformer.transform(declaredFields[1]));
40+
41+
// Fields with custom property mapping
42+
assertEquals("id", transformer.transform(declaredFields[2]));
43+
}
44+
}

0 commit comments

Comments
 (0)