Skip to content

Commit

Permalink
Merge pull request #115 from HubSpot/jh/rm-naming-strategy-base
Browse files Browse the repository at this point in the history
Revert to previous logic
  • Loading branch information
jhaber authored Dec 30, 2023
2 parents ac64b70 + 7cbe700 commit 513bc54
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 674 deletions.
Original file line number Diff line number Diff line change
@@ -1,81 +1,39 @@
package com.hubspot.jackson.datatype.protobuf;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.NamingBase;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotationMap;
import com.fasterxml.jackson.databind.introspect.TypeResolutionContext;
import com.google.common.base.CaseFormat;
import com.google.protobuf.Message;
import java.lang.reflect.Field;

@SuppressWarnings("serial")
public class PropertyNamingStrategyWrapper {

private static final NamingBase SNAKE_TO_CAMEL = new SnakeToCamelNamingStrategy();
private static final NamingBase NO_OP = new NoOpNamingStrategy();

private final Class<?> messageType;
private final MapperConfig<?> mapperConfig;
private final PropertyNamingStrategy delegate;
private final NamingBase delegate;

public PropertyNamingStrategyWrapper(
Class<? extends Message> messageType,
MapperConfig<?> mapperConfig
) {
this.messageType = messageType;
this.mapperConfig = mapperConfig;

if (mapperConfig.getPropertyNamingStrategy() == null) {
this.delegate = SNAKE_TO_CAMEL;
} else if (
mapperConfig.getPropertyNamingStrategy() ==
PropertyNamingStrategies.LOWER_CAMEL_CASE
) {
this.delegate = NO_OP;
if (mapperConfig.getPropertyNamingStrategy() instanceof NamingBase) {
this.delegate = (NamingBase) mapperConfig.getPropertyNamingStrategy();
} else {
this.delegate = mapperConfig.getPropertyNamingStrategy();
this.delegate = SNAKE_TO_CAMEL;
}
}

public String translate(String fieldName) {
AnnotatedField annotatedField = null;
try {
Field field = messageType.getDeclaredField(javaFieldName(fieldName));
annotatedField =
new AnnotatedField(
new TypeResolutionContext.Empty(mapperConfig.getTypeFactory()),
field,
new AnnotationMap()
);
} catch (ReflectiveOperationException e) {
// ignored
}

return delegate.nameForField(mapperConfig, annotatedField, fieldName);
}

private static String javaFieldName(String fieldName) {
return (
(fieldName.contains("_") ? SNAKE_TO_CAMEL.translate(fieldName) : fieldName) + "_"
);
return delegate.translate(fieldName);
}

private static class SnakeToCamelNamingStrategy extends NamingBase {

@Override
public String translate(String fieldName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName);
}
}

private static class NoOpNamingStrategy extends NamingBase {

@Override
public String translate(String fieldName) {
return fieldName;
return fieldName.contains("_")
? CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName)
: fieldName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.hubspot.jackson.datatype.protobuf;

import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.camelCase;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.newUnderscore;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.oldUnderscore;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.underscore;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.writeAndReadBack;
import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ExtensionRegistry;
import com.hubspot.jackson.datatype.protobuf.util.ProtobufCreator;
Expand Down Expand Up @@ -77,23 +74,7 @@ public void testMultipleBuildersCamelCase() {
public void testSingleMessageUnderscore() {
AllFields message = ProtobufCreator.create(AllFields.class, EXTENSION_REGISTRY);

AllFields parsed = writeAndReadBack(oldUnderscore(EXTENSION_REGISTRY), message);

assertThat(parsed).isEqualTo(message);
}

@Test
public void testSingleMessageMixedUnderscoreNamingStrategies() throws IOException {
AllFields message = ProtobufCreator.create(AllFields.class, EXTENSION_REGISTRY);

JsonNode json = newUnderscore(EXTENSION_REGISTRY).valueToTree(message);
AllFields parsed = oldUnderscore(EXTENSION_REGISTRY)
.treeToValue(json, AllFields.class);

assertThat(parsed).isEqualTo(message);

json = oldUnderscore(EXTENSION_REGISTRY).valueToTree(message);
parsed = newUnderscore(EXTENSION_REGISTRY).treeToValue(json, AllFields.class);
AllFields parsed = writeAndReadBack(underscore(EXTENSION_REGISTRY), message);

assertThat(parsed).isEqualTo(message);
}
Expand All @@ -106,33 +87,7 @@ public void testMultipleMessagesUnderscore() {
10
);

List<AllFields> parsed = writeAndReadBack(
oldUnderscore(EXTENSION_REGISTRY),
messages
);

assertThat(parsed).isEqualTo(messages);
}

@Test
public void testMultipleMessagesMixedUnderscoreNamingStrategies() {
List<AllFields> messages = ProtobufCreator.create(
AllFields.class,
EXTENSION_REGISTRY,
10
);

JsonNode json = newUnderscore(EXTENSION_REGISTRY).valueToTree(messages);
List<AllFields> parsed = parseList(
oldUnderscore(EXTENSION_REGISTRY),
AllFields.class,
json
);

assertThat(parsed).isEqualTo(messages);

json = oldUnderscore(EXTENSION_REGISTRY).valueToTree(messages);
parsed = parseList(newUnderscore(EXTENSION_REGISTRY), AllFields.class, json);
List<AllFields> parsed = writeAndReadBack(underscore(EXTENSION_REGISTRY), messages);

assertThat(parsed).isEqualTo(messages);
}
Expand All @@ -144,29 +99,7 @@ public void testSingleBuilderUnderscore() {
EXTENSION_REGISTRY
);

AllFields.Builder parsed = writeAndReadBack(
oldUnderscore(EXTENSION_REGISTRY),
builder
);

assertThat(parsed.build()).isEqualTo(builder.build());
}

@Test
public void testSingleBuilderMixedUnderscoreNamingStrategies() throws IOException {
AllFields.Builder builder = ProtobufCreator.createBuilder(
AllFields.Builder.class,
EXTENSION_REGISTRY
);

JsonNode json = newUnderscore(EXTENSION_REGISTRY).valueToTree(builder);
AllFields.Builder parsed = oldUnderscore(EXTENSION_REGISTRY)
.treeToValue(json, AllFields.Builder.class);

assertThat(parsed.build()).isEqualTo(builder.build());

json = oldUnderscore(EXTENSION_REGISTRY).valueToTree(builder);
parsed = newUnderscore(EXTENSION_REGISTRY).treeToValue(json, AllFields.Builder.class);
AllFields.Builder parsed = writeAndReadBack(underscore(EXTENSION_REGISTRY), builder);

assertThat(parsed.build()).isEqualTo(builder.build());
}
Expand All @@ -180,36 +113,13 @@ public void testMultipleBuildersUnderscore() {
);

List<AllFields.Builder> parsed = writeAndReadBack(
oldUnderscore(EXTENSION_REGISTRY),
underscore(EXTENSION_REGISTRY),
builders
);

assertThat(build(parsed)).isEqualTo(build(builders));
}

@Test
public void testMultipleBuildersMixedUnderscoreNamingStrategies() {
List<AllFields.Builder> builders = ProtobufCreator.createBuilder(
AllFields.Builder.class,
EXTENSION_REGISTRY,
10
);

JsonNode json = newUnderscore(EXTENSION_REGISTRY).valueToTree(builders);
List<AllFields.Builder> parsed = parseList(
oldUnderscore(EXTENSION_REGISTRY),
AllFields.Builder.class,
json
);

assertThat(build(parsed)).isEqualTo(build(builders));

json = oldUnderscore(EXTENSION_REGISTRY).valueToTree(builders);
parsed = parseList(newUnderscore(EXTENSION_REGISTRY), AllFields.Builder.class, json);

assertThat(build(parsed)).isEqualTo(build(builders));
}

@Test
public void testEmptyNestedObject() throws IOException {
String json = "{\"nested\":{}}";
Expand All @@ -219,17 +129,6 @@ public void testEmptyNestedObject() throws IOException {
assertThat(parsed.getNested()).isEqualTo(Nested.getDefaultInstance());
}

private static <T> List<T> parseList(
ObjectMapper mapper,
Class<T> type,
JsonNode json
) {
return mapper.convertValue(
json,
mapper.getTypeFactory().constructCollectionType(List.class, type)
);
}

private static List<AllFields> build(List<AllFields.Builder> builders) {
return builders.stream().map(Builder::build).collect(ImmutableList.toImmutableList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.hubspot.jackson.datatype.protobuf;

import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.camelCase;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.newUnderscore;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.oldUnderscore;
import static com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper.writeAndReadBack;
import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper;
import com.hubspot.jackson.datatype.protobuf.util.ProtobufCreator;
Expand Down Expand Up @@ -63,22 +59,7 @@ public void testMultipleBuildersCamelCase() {
public void testSingleMessageUnderscore() {
AllFields message = ProtobufCreator.create(AllFields.class);

AllFields parsed = writeAndReadBack(ObjectMapperHelper.oldUnderscore(), message);

assertThat(parsed).isEqualTo(message);
}

@Test
public void testSingleMessageMixedUnderscoreNamingStrategies() throws IOException {
AllFields message = ProtobufCreator.create(AllFields.class);

JsonNode json = newUnderscore().valueToTree(message);
AllFields parsed = oldUnderscore().treeToValue(json, AllFields.class);

assertThat(parsed).isEqualTo(message);

json = oldUnderscore().valueToTree(message);
parsed = newUnderscore().treeToValue(json, AllFields.class);
AllFields parsed = writeAndReadBack(ObjectMapperHelper.underscore(), message);

assertThat(parsed).isEqualTo(message);
}
Expand All @@ -87,25 +68,7 @@ public void testSingleMessageMixedUnderscoreNamingStrategies() throws IOExceptio
public void testMultipleMessagesUnderscore() {
List<AllFields> messages = ProtobufCreator.create(AllFields.class, 10);

List<AllFields> parsed = writeAndReadBack(
ObjectMapperHelper.oldUnderscore(),
messages
);

assertThat(parsed).isEqualTo(messages);
}

@Test
public void testMultipleMessagesMixedUnderscoreNamingStrategies() {
List<AllFields> messages = ProtobufCreator.create(AllFields.class, 10);

JsonNode json = newUnderscore().valueToTree(messages);
List<AllFields> parsed = parseList(oldUnderscore(), AllFields.class, json);

assertThat(parsed).isEqualTo(messages);

json = oldUnderscore().valueToTree(messages);
parsed = parseList(newUnderscore(), AllFields.class, json);
List<AllFields> parsed = writeAndReadBack(ObjectMapperHelper.underscore(), messages);

assertThat(parsed).isEqualTo(messages);
}
Expand All @@ -114,25 +77,7 @@ public void testMultipleMessagesMixedUnderscoreNamingStrategies() {
public void testSingleBuilderUnderscore() {
AllFields.Builder builder = ProtobufCreator.createBuilder(AllFields.Builder.class);

AllFields.Builder parsed = writeAndReadBack(
ObjectMapperHelper.oldUnderscore(),
builder
);

assertThat(parsed.build()).isEqualTo(builder.build());
}

@Test
public void testSingleBuilderMixedUnderscoreNamingStrategies() throws IOException {
AllFields.Builder builder = ProtobufCreator.createBuilder(AllFields.Builder.class);

JsonNode json = newUnderscore().valueToTree(builder);
AllFields.Builder parsed = oldUnderscore().treeToValue(json, AllFields.Builder.class);

assertThat(parsed.build()).isEqualTo(builder.build());

json = oldUnderscore().valueToTree(builder);
parsed = newUnderscore().treeToValue(json, AllFields.Builder.class);
AllFields.Builder parsed = writeAndReadBack(ObjectMapperHelper.underscore(), builder);

assertThat(parsed.build()).isEqualTo(builder.build());
}
Expand All @@ -145,35 +90,13 @@ public void testMultipleBuildersUnderscore() {
);

List<AllFields.Builder> parsed = writeAndReadBack(
ObjectMapperHelper.oldUnderscore(),
ObjectMapperHelper.underscore(),
builders
);

assertThat(build(parsed)).isEqualTo(build(builders));
}

@Test
public void testMultipleBuildersMixedUnderscoreNamingStrategies() {
List<AllFields.Builder> builders = ProtobufCreator.createBuilder(
AllFields.Builder.class,
10
);

JsonNode json = newUnderscore().valueToTree(builders);
List<AllFields.Builder> parsed = parseList(
oldUnderscore(),
AllFields.Builder.class,
json
);

assertThat(build(parsed)).isEqualTo(build(builders));

json = oldUnderscore().valueToTree(builders);
parsed = parseList(newUnderscore(), AllFields.Builder.class, json);

assertThat(build(parsed)).isEqualTo(build(builders));
}

@Test
public void testEmptyNestedObject() throws IOException {
String json = "{\"nested\":{}}";
Expand All @@ -183,17 +106,6 @@ public void testEmptyNestedObject() throws IOException {
assertThat(parsed.getNested()).isEqualTo(Nested.getDefaultInstance());
}

private static <T> List<T> parseList(
ObjectMapper mapper,
Class<T> type,
JsonNode json
) {
return mapper.convertValue(
json,
mapper.getTypeFactory().constructCollectionType(List.class, type)
);
}

private static List<AllFields> build(List<AllFields.Builder> builders) {
return builders.stream().map(Builder::build).collect(ImmutableList.toImmutableList());
}
Expand Down
Loading

0 comments on commit 513bc54

Please sign in to comment.