-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
47 additions
and
674 deletions.
There are no files selected for viewing
58 changes: 8 additions & 50 deletions
58
src/main/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingStrategyWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.