Skip to content

Commit

Permalink
Revert pull request entur#133
Browse files Browse the repository at this point in the history
Due to issue entur#232

This reverts commits 4d0b790, d28509b, 09da3b2, 3d4ea39, b7c87d7, 3b8f0b3
  • Loading branch information
ro0sterjam committed Oct 26, 2022
1 parent 007fde0 commit 5f099d5
Show file tree
Hide file tree
Showing 10 changed files with 6 additions and 277 deletions.
27 changes: 0 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,6 @@ which require manual mapping.
Implements ```EnumMappingStrategy``` and provides complete enum constant mappings if you follow Googles style guide for
enums https://developers.google.com/protocol-buffers/docs/style#enums

If needed you can specify a different postfix for the 0 value enum by passing in `mapstructSpi.enumPostfixOverrides` as
a compilerArg in the format of:

`-AmapstructSpi.enumPostfixOverrides=com.package.root.a=POSTFIX_1,com.package.root.b=POSTFIX_2`

Otherwise, this will default to `UNSPECIFIED` as per the Google style guide.

```xml

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>no.entur.mapstruct.spi</groupId>
<artifactId>protobuf-spi-impl</artifactId>
<version>LATEST.VERSION</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AmapstructSpi.enumPostfixOverrides=com.company.name=INVALID</arg>
</compilerArgs>
</configuration>
</plugin>

```

## Support - Mapping funcions:

Standard mapping functions between often used proto types and java types:
Expand Down
22 changes: 0 additions & 22 deletions spi-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>no/entur/mapstruct/spi/protobuf/ProcessingEnvOptionsHolder.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-project</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
* #L%
*/

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
Expand All @@ -37,7 +33,6 @@
import org.mapstruct.ap.spi.DefaultEnumMappingStrategy;

import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableMap;

public class ProtobufEnumMappingStrategy extends DefaultEnumMappingStrategy {

Expand All @@ -47,34 +42,13 @@ public class ProtobufEnumMappingStrategy extends DefaultEnumMappingStrategy {
private static final String PROTOBUF_LITE_ENUM_INTERFACE = "com.google.protobuf.Internal.EnumLite";
private static final HashMap<TypeElement, Boolean> KNOWN_ENUMS = new HashMap<>();

private Map<String, String> enumPostfixOverrides = null;

/**
* The enum constant postfix used as default value in protobuf, ie for enum "Cake" the default constant should be CAKE_UNSPECIFIED = 0; This is the
* recommended style according to Googles style guide https://developers.google.com/protocol-buffers/docs/style#enums. If you use some other pattern in your
* protobuf files you can pass in "mapstructSpi.enumPostfixOverrides" as a compilerArg.
* recommended style according to Googles style guide https://developers.google.com/protocol-buffers/docs/style#enums . If you use some other pattern in
* your protobuf files you can simply subclass this class and override this method.
*/
private String getEnumPostfix(TypeElement enumType) {
if (enumPostfixOverrides == null) {
initEnumPostfixOverrides();
}

String enumTypeName = enumType.getQualifiedName().toString();
Optional<String> override = enumPostfixOverrides.keySet().stream().filter(enumTypeName::startsWith).map(enumPostfixOverrides::get).findAny();

return override.orElse(DEFAULT_ENUM_POSTFIX);
}

private void initEnumPostfixOverrides() {
if (ProcessingEnvOptionsHolder.containsKey(ProcessingEnvOptionsHolder.ENUM_POSTFIX_OVERRIDES)) {
String[] postfixOverrides = ProcessingEnvOptionsHolder.getOption(ProcessingEnvOptionsHolder.ENUM_POSTFIX_OVERRIDES).split(",");

enumPostfixOverrides = Arrays.stream(postfixOverrides)
.map(override -> override.split("=", 2))
.collect(Collectors.toMap(args -> args[0].trim(), args -> args[1].trim()));
} else {
enumPostfixOverrides = ImmutableMap.of();
}
protected String getDefaultEnumConstant() {
return DEFAULT_ENUM_POSTFIX;
}

// @Override
Expand All @@ -85,7 +59,7 @@ public boolean isMapEnumConstantToNull(TypeElement enumType, String sourceEnumVa
}

String trimmedEnumValue = removeEnumNamePrefixFromConstant(enumType, sourceEnumValue);
if (getEnumPostfix(enumType).equals(trimmedEnumValue)) {
if (getDefaultEnumConstant().equals(trimmedEnumValue)) {
return true;
}

Expand All @@ -98,7 +72,7 @@ public String getDefaultNullEnumConstant(TypeElement enumType) {
boolean isProtobufEnum = isProtobufEnum(enumType);

if (isProtobufEnum) {
return addEnumNamePrefixToConstant(enumType, getEnumPostfix(enumType));
return addEnumNamePrefixToConstant(enumType, getDefaultEnumConstant());
} else {
return null;
}
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions usage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@
(in this case from the local M2-repo cache). -->
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AmapstructSpi.enumPostfixOverrides=no.entur.mapstruct.example.EnumPostfixOverrideProtos=INVALID</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions usage/src/main/proto/EnumPostfixOverride.proto

This file was deleted.

11 changes: 0 additions & 11 deletions usage/src/test/java/no/entur/mapstruct/example/MappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import no.entur.mapstruct.example.EnumPostfixOverrideProtos.EnumPostfixOverrideValuesDTO;
import no.entur.mapstruct.example.UserProtos.UserDTO;
import no.entur.mapstruct.spi.protobuf.Department;
import no.entur.mapstruct.spi.protobuf.EnumPostfixOverrideValues;
import no.entur.mapstruct.spi.protobuf.MultiNumber;
import no.entur.mapstruct.spi.protobuf.Status;
import no.entur.mapstruct.spi.protobuf.User;
Expand Down Expand Up @@ -247,13 +245,4 @@ private void assertUser(User orig, User back) {
// WILL FAIL: Mapstruct cannot handle presence checker on oneOfs : assertNull(back.getFireDepartment());

}

@Test
public void testEnumPostfixOverride() {
EnumPostfixOverrideValues enumValue = EnumPostfixOverrideValues.TWO;
EnumPostfixOverrideValuesDTO dto = EnumPostfixOverrideMapper.INSTANCE.map(enumValue);
EnumPostfixOverrideValues back = EnumPostfixOverrideMapper.INSTANCE.map(dto);

assertEquals(enumValue, back);
}
}

0 comments on commit 5f099d5

Please sign in to comment.