36 | * Protobuf lite builders are not detected as such by the DefaultBuilderProvider because the return type is not an explicit match for the desired TypeElement.
37 | * Relying instead on known name of builder method and superclass of return type.
38 | */
39 | public class ProtobufLiteBuilderProvider extends DefaultBuilderProvider {
40 |
41 | public static final String PROTOBUF_LITE_GENERATED_MESSAGE = "com.google.protobuf.GeneratedMessageLite";
42 | private static final String PROTOBUF_BUILD_METHOD_NAME = "build";
43 |
44 | @Override
45 | protected boolean isBuildMethod(ExecutableElement buildMethod, TypeElement typeElement) {
46 |
47 | if (PROTOBUF_BUILD_METHOD_NAME.equals(buildMethod.getSimpleName().toString())) {
48 | if (buildMethod.getReturnType() instanceof TypeVariable) {
49 | TypeVariable tv = (TypeVariable) buildMethod.getReturnType();
50 |
51 | TypeMirror upperBound = tv.getUpperBound() == null ? tv : tv.getUpperBound();
52 |
53 | if (upperBound.toString().startsWith(PROTOBUF_LITE_GENERATED_MESSAGE)) {
54 | return true;
55 | }
56 |
57 | }
58 | }
59 |
60 | return super.isBuildMethod(buildMethod, typeElement);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/spi-impl/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | no.entur.mapstruct.spi.protobuf.ProcessingEnvOptionsHolder
2 |
--------------------------------------------------------------------------------
/spi-impl/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy:
--------------------------------------------------------------------------------
1 | no.entur.mapstruct.spi.protobuf.ProtobufAccessorNamingStrategy
2 |
--------------------------------------------------------------------------------
/spi-impl/src/main/resources/META-INF/services/org.mapstruct.ap.spi.BuilderProvider:
--------------------------------------------------------------------------------
1 | no.entur.mapstruct.spi.protobuf.ProtobufLiteBuilderProvider
2 |
--------------------------------------------------------------------------------
/spi-impl/src/main/resources/META-INF/services/org.mapstruct.ap.spi.EnumMappingStrategy:
--------------------------------------------------------------------------------
1 | no.entur.mapstruct.spi.protobuf.ProtobufEnumMappingStrategy
2 |
--------------------------------------------------------------------------------
/support-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | no.entur.mapstruct.spi
6 | spi-protobuf-parent
7 | 0.1-SNAPSHOT
8 |
9 | protobuf-support-core
10 | 0.1-SNAPSHOT
11 |
12 |
13 | org.mapstruct
14 | mapstruct
15 |
16 |
17 | org.slf4j
18 | slf4j-api
19 |
20 |
21 | com.google.api.grpc
22 | proto-google-common-protos
23 | provided
24 |
25 |
26 | org.junit.jupiter
27 | junit-jupiter-api
28 | test
29 |
30 |
31 |
32 |
33 |
34 | org.apache.maven.plugins
35 | maven-source-plugin
36 |
37 |
38 | com.diffplug.spotless
39 | spotless-maven-plugin
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-compiler-plugin
44 |
45 | ${maven.compiler.source}
46 | ${maven.compiler.target}
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/support-core/src/main/java/no/entur/abt/mapstruct/common/ProtobufStandardMappings.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct.common;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import com.google.protobuf.BoolValue;
27 | import com.google.protobuf.ByteString;
28 | import com.google.protobuf.BytesValue;
29 | import com.google.protobuf.DoubleValue;
30 | import com.google.protobuf.FloatValue;
31 | import com.google.protobuf.Int32Value;
32 | import com.google.protobuf.Int64Value;
33 | import com.google.protobuf.StringValue;
34 | import com.google.protobuf.Timestamp;
35 | import com.google.protobuf.UInt32Value;
36 | import com.google.protobuf.UInt64Value;
37 |
38 | import java.time.Instant;
39 | import java.time.LocalDate;
40 | import java.time.LocalDateTime;
41 | import java.time.LocalTime;
42 | import java.time.OffsetDateTime;
43 | import java.time.ZoneOffset;
44 | import java.util.GregorianCalendar;
45 | import java.util.TimeZone;
46 |
47 | public interface ProtobufStandardMappings {
48 |
49 | default ByteString mapByteString(byte[] array) {
50 | if (array == null) {
51 | return ByteString.EMPTY;
52 | }
53 | return ByteString.copyFrom(array);
54 | }
55 |
56 | default byte[] mapByteString(ByteString in) {
57 | if (in != null && !in.isEmpty()) {
58 | return in.toByteArray();
59 | }
60 |
61 | return null;
62 | }
63 |
64 | default ByteString mapByteStringToString(String string) {
65 | return ByteString.copyFromUtf8(string);
66 | }
67 |
68 | default String mapStringToByteString(ByteString in) {
69 | if (in != null && !in.isEmpty()) {
70 | return in.toStringUtf8();
71 | }
72 |
73 | return null;
74 | }
75 |
76 | default com.google.type.Date mapLocalDate(LocalDate t) {
77 | return com.google.type.Date.newBuilder().setYear(t.getYear()).setMonth(t.getMonthValue()).setDay(t.getDayOfMonth()).build();
78 | }
79 |
80 | default LocalDate mapDate(com.google.type.Date t) {
81 | return LocalDate.of(t.getYear(), t.getMonth(), t.getDay());
82 | }
83 |
84 | default com.google.type.TimeOfDay mapLocalTime(LocalTime t) {
85 | return com.google.type.TimeOfDay.newBuilder().setHours(t.getHour()).setMinutes(t.getMinute()).setSeconds(t.getSecond()).setNanos(t.getNano()).build();
86 | }
87 |
88 | default LocalTime mapTimeOfDay(com.google.type.TimeOfDay t) {
89 | return LocalTime.of(t.getHours(), t.getMinutes(), t.getSeconds(), t.getNanos());
90 | }
91 |
92 | default Timestamp map(LocalDateTime i) {
93 | if (i == null) {
94 | return null;
95 | }
96 |
97 | TimeZone systemDefault = TimeZone.getDefault();
98 |
99 | int offset = systemDefault.getOffset(GregorianCalendar.AD, i.getYear(), i.getMonthValue() - 1, i.getDayOfMonth(), i.getDayOfWeek().getValue(),
100 | i.getNano() / 1000);
101 |
102 | return Timestamp.newBuilder().setSeconds(i.toEpochSecond(ZoneOffset.ofTotalSeconds(offset / 1000))).setNanos(i.getNano()).build();
103 | }
104 |
105 | default Timestamp map(OffsetDateTime in) {
106 | return Timestamp.newBuilder().setSeconds(in.toEpochSecond()).setNanos(0).build();
107 | }
108 |
109 | default float map(FloatValue f) {
110 | return f.getValue();
111 | }
112 |
113 | default double map(DoubleValue f) {
114 | return f.getValue();
115 | }
116 |
117 | default int map(Int32Value f) {
118 | return f.getValue();
119 | }
120 |
121 | default long map(Int64Value f) {
122 | return f.getValue();
123 | }
124 |
125 | default int map(UInt32Value f) {
126 | return f.getValue();
127 | }
128 |
129 | default long map(UInt64Value f) {
130 | return f.getValue();
131 | }
132 |
133 | default String map(StringValue f) {
134 | return f.getValue();
135 | }
136 |
137 | default boolean map(BoolValue f) {
138 | return f.getValue();
139 | }
140 |
141 | default ByteString map(BytesValue f) {
142 | return f.getValue();
143 | }
144 |
145 | default Instant mapToInstant(Timestamp t) {
146 | if (t == null) {
147 | return null;
148 | }
149 |
150 | Timestamp sanitized = Timestamps.sanitize(t);
151 |
152 | if (sanitized != null) {
153 | return Instant.ofEpochSecond(sanitized.getSeconds(), sanitized.getNanos());
154 | } else {
155 | return null;
156 | }
157 | }
158 |
159 | default Timestamp mapToTimestamp(Instant i) {
160 | if (i == null) {
161 | return null;
162 | }
163 |
164 | Timestamp t = Timestamp.newBuilder().setSeconds(i.getEpochSecond()).setNanos(i.getNano()).build();
165 | return Timestamps.sanitize(t);
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/support-core/src/main/java/no/entur/abt/mapstruct/common/Timestamps.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct.common;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support-lite
6 | * %%
7 | * Copyright (C) 2019 - 2022 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import com.google.protobuf.Timestamp;
27 | import org.slf4j.Logger;
28 | import org.slf4j.LoggerFactory;
29 |
30 | public class Timestamps {
31 |
32 | private static final Logger LOGGER = LoggerFactory.getLogger(Timestamps.class);;
33 | static final long TIMESTAMP_SECONDS_MIN = -62135596800L;
34 | static final long TIMESTAMP_SECONDS_MAX = 253402300799L;
35 | static final int NANOS_PER_SECOND = 1000000000;
36 |
37 | public static final Timestamp MAX_VALUE = Timestamp.newBuilder().setSeconds(TIMESTAMP_SECONDS_MAX).setNanos(NANOS_PER_SECOND - 1).build();
38 | public static final Timestamp MIN_VALUE = Timestamp.newBuilder().setSeconds(TIMESTAMP_SECONDS_MIN).setNanos(0).build();
39 |
40 | /**
41 | * Sanitize Timestamps outside legal range where possible.
42 | */
43 | public static Timestamp sanitize(Timestamp t) {
44 | if (t.getSeconds() == 0 && t.getNanos() == 0) {
45 | return null; // Assuming null for epoch, cannot differentiate
46 | }
47 | if (t.getNanos() < 0 || t.getNanos() >= NANOS_PER_SECOND) {
48 | throw new IllegalArgumentException(String.format(
49 | "Timestamp is not valid. See proto definition for valid values. Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. Nanos (%s) must be in range [0, +999,999,999].",
50 | t.getSeconds(), t.getNanos()));
51 | }
52 | if (t.getSeconds() > TIMESTAMP_SECONDS_MAX) {
53 | LOGGER.warn(String.format("Cannot map to timestamp greater than allowed MAX: (%s). Using MAX_TIMESTAMP instead: (%s)", t, Timestamps.MAX_VALUE));
54 | return MAX_VALUE;
55 | }
56 | if (t.getSeconds() < TIMESTAMP_SECONDS_MIN) {
57 | LOGGER.warn(String.format("Cannot map to timestamp less than allowed MIN: (%s). Using MIN_TIMESTAMP instead: (%s)", t, Timestamps.MIN_VALUE));
58 | return MIN_VALUE;
59 | }
60 |
61 | return t;
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/support-core/src/test/java/no/entur/abt/mapstruct/common/ProtobufStandardMappingsTest.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct.common;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public class ProtobufStandardMappingsTest {
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/support-lite/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | no.entur.mapstruct.spi
6 | spi-protobuf-parent
7 | 0.1-SNAPSHOT
8 |
9 | protobuf-support-lite
10 | 0.1-SNAPSHOT
11 |
12 |
13 | no.entur.mapstruct.spi
14 | protobuf-support-core
15 |
16 |
17 | org.mapstruct
18 | mapstruct
19 |
20 |
21 | com.google.api.grpc
22 | proto-google-common-protos
23 | provided
24 |
25 |
26 | org.junit.jupiter
27 | junit-jupiter-api
28 | test
29 |
30 |
31 |
32 |
33 |
34 | org.apache.maven.plugins
35 | maven-source-plugin
36 |
37 |
38 | com.diffplug.spotless
39 | spotless-maven-plugin
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-compiler-plugin
44 |
45 | ${maven.compiler.source}
46 | ${maven.compiler.target}
47 |
48 |
49 | no.entur.mapstruct.spi
50 | protobuf-spi-impl
51 | ${project.parent.version}
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/support-lite/src/main/java/no/entur/abt/mapstruct/ProtobufStandardMappings.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import java.time.Duration;
27 | import java.time.Instant;
28 | import java.util.concurrent.TimeUnit;
29 |
30 | import org.mapstruct.Mapper;
31 | import org.mapstruct.factory.Mappers;
32 |
33 | import com.google.protobuf.Timestamp;
34 |
35 | /***
36 | *
37 | * Note: This mapper must be kept in sync with its corresponding 'standard' equivalent
38 | *
39 | */
40 |
41 | @Mapper
42 | public interface ProtobufStandardMappings extends no.entur.abt.mapstruct.common.ProtobufStandardMappings {
43 |
44 | ProtobufStandardMappings INSTANCE = Mappers.getMapper(ProtobufStandardMappings.class);
45 |
46 | default Long toEpochMilliseconds(Timestamp instance) {
47 | Instant instant = mapToInstant(instance);
48 | return instant == null ? null : instant.toEpochMilli();
49 | }
50 |
51 | default Timestamp fromEpochMilliseconds(Long instance) {
52 | if (instance == null) {
53 | return null;
54 | }
55 | Instant instant = Instant.ofEpochMilli(instance);
56 | return mapToTimestamp(instant);
57 | }
58 |
59 | default Duration mapDuration(com.google.protobuf.Duration t) {
60 | return Duration.ofSeconds(t.getSeconds(), t.getNanos());
61 | }
62 |
63 | default com.google.protobuf.Duration mapDuration(Duration t) {
64 | long seconds = t.getSeconds();
65 | int nanos = t.getNano();
66 |
67 | // Protobuf requires same sign for seconds & nanos parts. Java time treats nano part as relative adjustment. Adjust to proto expectations
68 | if (seconds < 0 && nanos > 0) {
69 | seconds = seconds + 1;
70 | nanos = (int) (nanos - TimeUnit.SECONDS.toNanos(1));
71 | }
72 |
73 | return com.google.protobuf.Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/support-lite/src/test/java/no/entur/abt/mapstruct/Durations.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support-lite
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public class Durations {
27 |
28 | static final long NANOS_PER_SECOND = 1000000000;
29 |
30 | static final long DURATION_SECONDS_MIN = -315576000000L;
31 | static final long DURATION_SECONDS_MAX = 315576000000L;
32 |
33 | public static com.google.protobuf.Duration checkValid(com.google.protobuf.Duration duration) {
34 | long seconds = duration.getSeconds();
35 | int nanos = duration.getNanos();
36 | if (!isValid(seconds, nanos)) {
37 | throw new IllegalArgumentException(String.format(
38 | "Duration is not valid. See proto definition for valid values. " + "Seconds (%s) must be in range [-315,576,000,000, +315,576,000,000]. "
39 | + "Nanos (%s) must be in range [-999,999,999, +999,999,999]. " + "Nanos must have the same sign as seconds",
40 | seconds, nanos));
41 | }
42 | return duration;
43 | }
44 |
45 | public static boolean isValid(long seconds, int nanos) {
46 | if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) {
47 | return false;
48 | }
49 | if (nanos < -999999999L || nanos >= NANOS_PER_SECOND) {
50 | return false;
51 | }
52 | if (seconds < 0 || nanos < 0) {
53 | if (seconds > 0 || nanos > 0) {
54 | return false;
55 | }
56 | }
57 | return true;
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/support-lite/src/test/java/no/entur/abt/mapstruct/ProtobufStandardMappingsTest.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import static org.junit.jupiter.api.Assertions.assertEquals;
27 | import static org.junit.jupiter.api.Assertions.assertNull;
28 |
29 | import java.time.Duration;
30 | import java.time.Instant;
31 | import java.time.LocalDateTime;
32 | import java.time.ZoneId;
33 | import java.time.temporal.ChronoUnit;
34 | import java.util.concurrent.TimeUnit;
35 |
36 | import org.junit.jupiter.api.Test;
37 |
38 | import com.google.protobuf.Timestamp;
39 |
40 | import no.entur.abt.mapstruct.common.Timestamps;
41 |
42 | public class ProtobufStandardMappingsTest {
43 |
44 | ProtobufStandardMappings MAPPER = ProtobufStandardMappings.INSTANCE;
45 |
46 | @Test
47 | public void testMapLocalDateToTimestampSummertime() {
48 | LocalDateTime l = LocalDateTime.of(2000, 6, 1, 12, 0);
49 |
50 | Timestamp timestamp = MAPPER.map(l);
51 | Instant instant = MAPPER.mapToInstant(timestamp);
52 |
53 | LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
54 |
55 | assertEquals(l, back);
56 | }
57 |
58 | @Test
59 | public void testMapLocalDateToTimestampWintertime() {
60 | LocalDateTime l = LocalDateTime.of(2000, 2, 1, 12, 0);
61 |
62 | Timestamp timestamp = MAPPER.map(l);
63 | Instant instant = MAPPER.mapToInstant(timestamp);
64 |
65 | LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
66 |
67 | assertEquals(l, back);
68 | }
69 |
70 | @Test
71 | public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() {
72 | assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build()));
73 | }
74 |
75 | @Test
76 | public void mapToInstant_whenNanosIsSet_thenMapToInstant() {
77 | assertEquals(3000, MAPPER.mapToInstant(Timestamp.newBuilder().setNanos(3000).build()).getNano());
78 | }
79 |
80 | @Test
81 | public void mapToInstant_whenValueIsTooLargeForRangeForTimestamp_thenMapFromMaxValidTimestamp() {
82 | assertEquals(MAPPER.mapToInstant(Timestamps.MAX_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(Long.MAX_VALUE).build()));
83 | }
84 |
85 | @Test
86 | public void mapToInstant_whenValueIsTooSmallForRangeForTimestamp_thenMapFromMinValidTimestamp() {
87 | assertEquals(MAPPER.mapToInstant(Timestamps.MIN_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(-Long.MAX_VALUE).build()));
88 | }
89 |
90 | @Test
91 | public void mapInstantToTimestamp_whenValueIsTooLargeForRangeForTimestamp_thenMapToMaxValidTimestamp() {
92 | assertEquals(Timestamps.MAX_VALUE, MAPPER.mapToTimestamp(Instant.now().plus(Integer.MAX_VALUE, ChronoUnit.DAYS)));
93 | }
94 |
95 | @Test
96 | public void mapInstantToTimestamp_whenValueIsTooSmallForRangeForTimestamp_thenMapToMinValidTimestamp() {
97 | assertEquals(Timestamps.MIN_VALUE, MAPPER.mapToTimestamp(Instant.now().minus(Integer.MAX_VALUE, ChronoUnit.DAYS)));
98 | }
99 |
100 | public void mapPositiveDuration() {
101 | Duration duration = Duration.of(3, ChronoUnit.NANOS);
102 |
103 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
104 | Durations.checkValid(pbDuration);
105 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
106 | }
107 |
108 | @Test
109 | public void mapNegativeDurationToProto_whenSecondsAreNegativeAndNanoPositive() {
110 | Duration duration = Duration.ofSeconds(-3, 2);
111 |
112 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
113 | Durations.checkValid(pbDuration);
114 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
115 | }
116 |
117 | @Test
118 | public void mapNegativeDurationToProto_whenSecondsArePositiveAndNanoNegative() {
119 | // Duration.ofSeconds accepts negative values. Will still be stored as positive values in Duration
120 | Duration duration = Duration.ofSeconds(3, -(TimeUnit.SECONDS.toNanos(1) - 2));
121 |
122 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
123 | Durations.checkValid(pbDuration);
124 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
125 | }
126 |
127 | @Test
128 | public void mapNegativeDuration_fromProto() {
129 | com.google.protobuf.Duration pbDuration = com.google.protobuf.Duration.newBuilder().setSeconds(-10).setNanos(-5).build();
130 |
131 | Duration duration = MAPPER.mapDuration(pbDuration);
132 | Durations.checkValid(pbDuration);
133 | assertEquals(pbDuration, MAPPER.mapDuration(duration));
134 | }
135 |
136 | }
137 |
--------------------------------------------------------------------------------
/support-standard/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | no.entur.mapstruct.spi
6 | spi-protobuf-parent
7 | 0.1-SNAPSHOT
8 |
9 | protobuf-support-standard
10 | 0.1-SNAPSHOT
11 |
12 |
13 | no.entur.mapstruct.spi
14 | protobuf-support-core
15 |
16 |
17 | org.mapstruct
18 | mapstruct
19 |
20 |
21 | com.google.protobuf
22 | protobuf-java-util
23 | ${proto.version}
24 |
25 |
26 | com.google.api.grpc
27 | proto-google-common-protos
28 | provided
29 |
30 |
31 | org.junit.jupiter
32 | junit-jupiter-api
33 | test
34 |
35 |
36 |
37 |
38 |
39 | org.apache.maven.plugins
40 | maven-source-plugin
41 |
42 |
43 | com.diffplug.spotless
44 | spotless-maven-plugin
45 |
46 |
47 | org.apache.maven.plugins
48 | maven-compiler-plugin
49 |
50 | ${maven.compiler.source}
51 | ${maven.compiler.target}
52 |
53 |
54 | no.entur.mapstruct.spi
55 | protobuf-spi-impl
56 | ${project.parent.version}
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/support-standard/src/main/java/no/entur/abt/mapstruct/ProtobufStandardMappings.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import java.time.Duration;
27 |
28 | import org.mapstruct.Mapper;
29 | import org.mapstruct.factory.Mappers;
30 |
31 | import com.google.protobuf.Timestamp;
32 | import com.google.protobuf.util.Durations;
33 | import com.google.protobuf.util.Timestamps;
34 |
35 | /***
36 | *
37 | * Note: This mapper must be kept in sync with its corresponding 'lite' equivalent
38 | *
39 | */
40 |
41 | @Mapper
42 | public interface ProtobufStandardMappings extends no.entur.abt.mapstruct.common.ProtobufStandardMappings {
43 |
44 | ProtobufStandardMappings INSTANCE = Mappers.getMapper(ProtobufStandardMappings.class);
45 |
46 | default Long toEpochMilliseconds(Timestamp instance) {
47 | if (instance != null) {
48 | return Timestamps.toMillis(instance);
49 | } else {
50 | return null;
51 | }
52 | }
53 |
54 | default Timestamp fromEpochMilliseconds(long millis) {
55 | return Timestamps.fromMillis(millis);
56 | }
57 |
58 | default Duration mapDuration(com.google.protobuf.Duration t) {
59 | if (t != null) {
60 | return Duration.ofSeconds(t.getSeconds(), t.getNanos());
61 | } else {
62 | return null;
63 | }
64 | }
65 |
66 | default com.google.protobuf.Duration mapDuration(Duration t) {
67 | if (t != null) {
68 | return Durations.fromNanos(t.toNanos());
69 | } else {
70 | return null;
71 | }
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/support-standard/src/test/java/no/entur/abt/mapstruct/ProtobufStandardMappingsTest.java:
--------------------------------------------------------------------------------
1 | package no.entur.abt.mapstruct;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-support
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import com.google.protobuf.Timestamp;
27 | import com.google.protobuf.util.Durations;
28 | import no.entur.abt.mapstruct.common.Timestamps;
29 | import org.junit.jupiter.api.Test;
30 |
31 | import java.time.Duration;
32 | import java.time.Instant;
33 | import java.time.LocalDateTime;
34 | import java.time.ZoneId;
35 | import java.time.temporal.ChronoUnit;
36 | import java.util.concurrent.TimeUnit;
37 |
38 | import static org.junit.jupiter.api.Assertions.assertEquals;
39 | import static org.junit.jupiter.api.Assertions.assertNull;
40 |
41 | public class ProtobufStandardMappingsTest {
42 |
43 | no.entur.abt.mapstruct.ProtobufStandardMappings MAPPER = no.entur.abt.mapstruct.ProtobufStandardMappings.INSTANCE;
44 |
45 | @Test
46 | public void testMapLocalDateToTimestampSummertime() {
47 | LocalDateTime l = LocalDateTime.of(2000, 6, 1, 12, 0);
48 |
49 | Timestamp timestamp = MAPPER.map(l);
50 | Instant instant = MAPPER.mapToInstant(timestamp);
51 |
52 | LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
53 |
54 | assertEquals(l, back);
55 | }
56 |
57 | @Test
58 | public void testMapLocalDateToTimestampWintertime() {
59 | LocalDateTime l = LocalDateTime.of(2000, 2, 1, 12, 0);
60 |
61 | Timestamp timestamp = MAPPER.map(l);
62 | Instant instant = MAPPER.mapToInstant(timestamp);
63 |
64 | LocalDateTime back = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
65 |
66 | assertEquals(l, back);
67 | }
68 |
69 | @Test
70 | public void mapToInstant_whenSecondsAndNanosIs0_thenMapToNull() {
71 | assertNull(MAPPER.mapToInstant(Timestamp.newBuilder().build()));
72 | }
73 |
74 | @Test
75 | public void mapToInstant_whenSecondsAndNanosIsNull_thenMapToNull() {
76 | assertNull(MAPPER.mapToInstant(null));
77 | }
78 |
79 | @Test
80 | public void mapToInstant_whenNanosIsSet_thenMapToInstant() {
81 | assertEquals(3000, MAPPER.mapToInstant(Timestamp.newBuilder().setNanos(3000).build()).getNano());
82 | }
83 |
84 | @Test
85 | public void mapToInstant_whenValueIsTooLargeForRangeForTimestamp_thenMapFromMaxValidTimestamp() {
86 | assertEquals(MAPPER.mapToInstant(Timestamps.MAX_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(Long.MAX_VALUE).build()));
87 | }
88 |
89 | @Test
90 | public void mapToInstant_whenValueIsTooSmallForRangeForTimestamp_thenMapFromMinValidTimestamp() {
91 | assertEquals(MAPPER.mapToInstant(Timestamps.MIN_VALUE), MAPPER.mapToInstant(Timestamp.newBuilder().setSeconds(-Long.MAX_VALUE).build()));
92 | }
93 |
94 | @Test
95 | public void mapInstantToTimestamp_whenValueIsTooLargeForRangeForTimestamp_thenMapToMaxValidTimestamp() {
96 | assertEquals(Timestamps.MAX_VALUE, MAPPER.mapToTimestamp(Instant.now().plus(Integer.MAX_VALUE, ChronoUnit.DAYS)));
97 | }
98 |
99 | @Test
100 | public void mapInstantToTimestamp_whenValueIsTooSmallForRangeForTimestamp_thenMapToMinValidTimestamp() {
101 | assertEquals(Timestamps.MIN_VALUE, MAPPER.mapToTimestamp(Instant.now().minus(Integer.MAX_VALUE, ChronoUnit.DAYS)));
102 | }
103 |
104 | @Test
105 | public void mapPositiveDuration() {
106 | Duration duration = Duration.of(3, ChronoUnit.NANOS);
107 |
108 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
109 | Durations.checkValid(pbDuration);
110 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
111 | }
112 |
113 | @Test
114 | public void mapNegativeDurationToProto_whenSecondsAreNegativeAndNanoPositive() {
115 | Duration duration = Duration.ofSeconds(-3, 2);
116 |
117 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
118 | Durations.checkValid(pbDuration);
119 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
120 | }
121 |
122 | @Test
123 | public void mapNegativeDurationToProto_whenSecondsArePositiveAndNanoNegative() {
124 | // Duration.ofSeconds accepts negative values. Will still be stored as positive values in Duration
125 | Duration duration = Duration.ofSeconds(3, -(TimeUnit.SECONDS.toNanos(1) - 2));
126 |
127 | com.google.protobuf.Duration pbDuration = MAPPER.mapDuration(duration);
128 | Durations.checkValid(pbDuration);
129 | assertEquals(duration, MAPPER.mapDuration(pbDuration));
130 | }
131 |
132 | @Test
133 | public void mapNegativeDuration_fromProto() {
134 | com.google.protobuf.Duration pbDuration = com.google.protobuf.Duration.newBuilder().setSeconds(-10).setNanos(-5).build();
135 |
136 | Duration duration = MAPPER.mapDuration(pbDuration);
137 | Durations.checkValid(pbDuration);
138 | assertEquals(pbDuration, MAPPER.mapDuration(duration));
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/usage/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | no.entur.mapstruct.spi
6 | spi-protobuf-parent
7 | 0.1-SNAPSHOT
8 |
9 | protobuf-usage
10 | 0.1-SNAPSHOT
11 |
12 |
13 | org.mapstruct
14 | mapstruct
15 |
16 |
17 | com.google.protobuf
18 | protobuf-java
19 |
20 |
21 | org.junit.jupiter
22 | junit-jupiter-engine
23 | test
24 |
25 |
26 |
27 |
28 |
29 | dev.cookiecode
30 | protobuf-maven-plugin
31 | ${protobuf.plugin.version}
32 |
33 |
34 |
35 | compile
36 |
37 | generate-sources
38 |
39 | com.google.protobuf:protoc:${proto.version}:exe:${os.detected.classifier}
40 |
41 |
42 |
43 |
44 |
45 | maven-compiler-plugin
46 |
47 |
48 |
49 |
50 | no.entur.mapstruct.spi
51 | protobuf-spi-impl
52 | ${project.version}
53 |
60 |
61 |
62 |
63 | -AmapstructSpi.enumPostfixOverrides=no.entur.mapstruct.example.EnumPostfixOverrideProtos=INVALID
64 |
65 |
66 |
67 |
68 | com.diffplug.spotless
69 | spotless-maven-plugin
70 |
71 |
72 |
73 |
74 | kr.motd.maven
75 | os-maven-plugin
76 | ${os.mavenplugin.version}
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/example/EnumPostfixOverrideMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3 | * and/or other contributors as indicated by the @authors tag. See the
4 | * copyright.txt file in the distribution for a full listing of all
5 | * contributors.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | package no.entur.mapstruct.example;
20 |
21 | /*-
22 | * #%L
23 | * protobuf-usage
24 | * %%
25 | * Copyright (C) 2019 - 2020 Entur
26 | * %%
27 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
28 | * approved by the European Commission - subsequent versions of the
29 | * EUPL (the "Licence");
30 | *
31 | * You may not use this work except in compliance with the Licence.
32 | * You may obtain a copy of the Licence at:
33 | *
34 | * http://ec.europa.eu/idabc/eupl5
35 | *
36 | * Unless required by applicable law or agreed to in writing, software
37 | * distributed under the Licence is distributed on an "AS IS" basis,
38 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 | * See the Licence for the specific language governing permissions and
40 | * limitations under the Licence.
41 | * #L%
42 | */
43 |
44 | import org.mapstruct.CollectionMappingStrategy;
45 | import org.mapstruct.Mapper;
46 | import org.mapstruct.NullValueCheckStrategy;
47 | import org.mapstruct.ReportingPolicy;
48 | import org.mapstruct.factory.Mappers;
49 |
50 | import no.entur.mapstruct.example.EnumPostfixOverrideProtos.EnumPostfixOverrideValuesDTO;
51 | import no.entur.mapstruct.spi.protobuf.EnumPostfixOverrideValues;
52 |
53 | @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedSourcePolicy = ReportingPolicy.ERROR, unmappedTargetPolicy = ReportingPolicy.ERROR)
54 | public interface EnumPostfixOverrideMapper {
55 |
56 | EnumPostfixOverrideMapper INSTANCE = Mappers.getMapper(EnumPostfixOverrideMapper.class);
57 |
58 | EnumPostfixOverrideValuesDTO map(EnumPostfixOverrideValues value);
59 |
60 | EnumPostfixOverrideValues map(EnumPostfixOverrideValuesDTO dto);
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/example/UserMapper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3 | * and/or other contributors as indicated by the @authors tag. See the
4 | * copyright.txt file in the distribution for a full listing of all
5 | * contributors.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | package no.entur.mapstruct.example;
20 |
21 | /*-
22 | * #%L
23 | * protobuf-usage
24 | * %%
25 | * Copyright (C) 2019 - 2020 Entur
26 | * %%
27 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
28 | * approved by the European Commission - subsequent versions of the
29 | * EUPL (the "Licence");
30 | *
31 | * You may not use this work except in compliance with the Licence.
32 | * You may obtain a copy of the Licence at:
33 | *
34 | * http://ec.europa.eu/idabc/eupl5
35 | *
36 | * Unless required by applicable law or agreed to in writing, software
37 | * distributed under the Licence is distributed on an "AS IS" basis,
38 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 | * See the Licence for the specific language governing permissions and
40 | * limitations under the Licence.
41 | * #L%
42 | */
43 |
44 | import org.mapstruct.CollectionMappingStrategy;
45 | import org.mapstruct.Mapper;
46 | import org.mapstruct.NullValueCheckStrategy;
47 | import org.mapstruct.ReportingPolicy;
48 | import org.mapstruct.factory.Mappers;
49 |
50 | import no.entur.mapstruct.example.UserProtos.DepartmentDTO;
51 | import no.entur.mapstruct.example.UserProtos.PermissionDTO;
52 | import no.entur.mapstruct.example.UserProtos.UserDTO;
53 | import no.entur.mapstruct.spi.protobuf.Department;
54 | import no.entur.mapstruct.spi.protobuf.MultiNumber;
55 | import no.entur.mapstruct.spi.protobuf.Permission;
56 | import no.entur.mapstruct.spi.protobuf.Status;
57 | import no.entur.mapstruct.spi.protobuf.User;
58 |
59 | @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedSourcePolicy = ReportingPolicy.ERROR, unmappedTargetPolicy = ReportingPolicy.ERROR)
60 | public interface UserMapper {
61 |
62 | UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
63 |
64 | default String mapString(String in) {
65 | if ((null == in) || in.isEmpty()) {
66 | return null;
67 | }
68 | return in;
69 | }
70 |
71 | default Double mapDouble(Double in) {
72 | return in;
73 | }
74 |
75 | default MultiNumber map(UserProtos.MultiNumberDTO number) {
76 | return new MultiNumber();
77 | }
78 |
79 | default UserProtos.MultiNumberDTO map(MultiNumber number) {
80 | return UserProtos.MultiNumberDTO.newBuilder().build();
81 | }
82 |
83 | UserDTO map(User user);
84 |
85 | User map(UserDTO userDTO);
86 |
87 | Permission map(PermissionDTO permissionDTO);
88 |
89 | PermissionDTO map(Permission perm);
90 |
91 | Status map(UserProtos.EnumStatus permissionDTO);
92 |
93 | UserProtos.EnumStatus map(Status perm);
94 |
95 | Department map(DepartmentDTO departmentDTO);
96 |
97 | DepartmentDTO map(Department department);
98 | }
99 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/Department.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2018 - 2020 Entur AS and original authors
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | /**
27 | * @author Thomas Kratz
28 | */
29 | public class Department {
30 | private String name;
31 |
32 | public Department() {
33 | }
34 |
35 | public Department(String name) {
36 | this.name = name;
37 | }
38 |
39 | public String getName() {
40 | return name;
41 | }
42 |
43 | public void setName(String name) {
44 | this.name = name;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/EnumPostfixOverrideValues.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2021 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public enum EnumPostfixOverrideValues {
27 | ONE,
28 | TWO
29 | }
30 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/House.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2020 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public class House {
27 | private String name;
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public void setName(String name) {
34 | this.name = name;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/MultiNumber.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2020 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public class MultiNumber {
27 | Number number;
28 |
29 | public Number getNumber() {
30 | return number;
31 | }
32 |
33 | public void setNumber(Number number) {
34 | this.number = number;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/Permission.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2020 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | /**
27 | * @author Thomas Kratz
28 | */
29 | public enum Permission {
30 |
31 | ADMIN,
32 | USER,
33 | NONE
34 | }
35 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/Status.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2020 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | public enum Status {
27 | STARTED,
28 | STOPPED
29 | }
30 |
--------------------------------------------------------------------------------
/usage/src/main/java/no/entur/mapstruct/spi/protobuf/User.java:
--------------------------------------------------------------------------------
1 | package no.entur.mapstruct.spi.protobuf;
2 |
3 | /*-
4 | * #%L
5 | * protobuf-usage
6 | * %%
7 | * Copyright (C) 2019 - 2020 Entur
8 | * %%
9 | * Licensed under the EUPL, Version 1.1 or – as soon they will be
10 | * approved by the European Commission - subsequent versions of the
11 | * EUPL (the "Licence");
12 | *
13 | * You may not use this work except in compliance with the Licence.
14 | * You may obtain a copy of the Licence at:
15 | *
16 | * http://ec.europa.eu/idabc/eupl5
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the Licence is distributed on an "AS IS" basis,
20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the Licence for the specific language governing permissions and
22 | * limitations under the Licence.
23 | * #L%
24 | */
25 |
26 | import java.util.ArrayList;
27 | import java.util.HashMap;
28 | import java.util.List;
29 | import java.util.Map;
30 |
31 | import com.google.protobuf.ByteString;
32 |
33 | /**
34 | * @author Thomas Kratz
35 | */
36 | public class User {
37 |
38 | double v1;
39 | float v2;
40 | int v3;
41 | long v4;
42 | int v5;
43 | long v6;
44 | int v7;
45 | long v8;
46 | int v9;
47 |
48 | // public List getPermissions() {
49 | // return permissions;
50 | // }
51 | //
52 | // public void setPermissions(List permissions) {
53 | // this.permissions = permissions;
54 | // }
55 | long v10;
56 | int v11;
57 | long v12;
58 | boolean v13;
59 | String v14;
60 | ByteString v15;
61 | Status v16;
62 | User user;
63 | List rv1;
64 | List rv2;
65 | List rv3;
66 | List rv4;
67 | List rv5;
68 | List rv6;
69 | List rv7;
70 | List rv8;
71 | List rv9;
72 | List rv10;
73 | List rv11;
74 | List rv12;
75 | List rv13;
76 | List rv14;
77 | List rv15;
78 | List rv16;
79 | MultiNumber multiNumber;
80 | List repMultiNumbers;
81 | private String id;
82 | private String email;
83 | // private List permissions = new ArrayList<>();
84 | private List mainDepartments = new ArrayList<>();
85 | private List departments = new ArrayList<>();
86 | private String nonRepeatableFieldWithSuffixList;
87 | private List users;
88 |
89 | private Department fireDepartment;
90 | private Department policeDepartment;
91 |
92 | private Map stringMap = new HashMap<>();
93 |
94 | public Map getStringMap() {
95 | return stringMap;
96 | }
97 |
98 | public Map getEntityMap() {
99 | return entityMap;
100 | }
101 |
102 | private Map entityMap = new HashMap<>();
103 |
104 | public Department getFireDepartment() {
105 | return fireDepartment;
106 | }
107 |
108 | public void setFireDepartment(Department fireDepartment) {
109 | this.fireDepartment = fireDepartment;
110 | }
111 |
112 | public Department getPoliceDepartment() {
113 | return policeDepartment;
114 | }
115 |
116 | public void setPoliceDepartment(Department policeDepartment) {
117 | this.policeDepartment = policeDepartment;
118 | }
119 |
120 | public String getId() {
121 | return id;
122 | }
123 |
124 | public void setId(String id) {
125 | this.id = id;
126 | }
127 |
128 | public String getEmail() {
129 | return email;
130 | }
131 |
132 | public void setEmail(String email) {
133 | this.email = email;
134 | }
135 |
136 | public List getDepartments() {
137 | return departments;
138 | }
139 |
140 | public void setDepartments(List departments) {
141 | this.departments = departments;
142 | }
143 |
144 | public List getMainDepartments() {
145 | return mainDepartments;
146 | }
147 |
148 | public void setMainDepartments(List mainDepartments) {
149 | this.mainDepartments = mainDepartments;
150 | }
151 |
152 | public double getV1() {
153 | return v1;
154 | }
155 |
156 | public void setV1(double v1) {
157 | this.v1 = v1;
158 | }
159 |
160 | public float getV2() {
161 | return v2;
162 | }
163 |
164 | public void setV2(float v2) {
165 | this.v2 = v2;
166 | }
167 |
168 | public int getV3() {
169 | return v3;
170 | }
171 |
172 | public void setV3(int v3) {
173 | this.v3 = v3;
174 | }
175 |
176 | public long getV4() {
177 | return v4;
178 | }
179 |
180 | public void setV4(long v4) {
181 | this.v4 = v4;
182 | }
183 |
184 | public int getV5() {
185 | return v5;
186 | }
187 |
188 | public void setV5(int v5) {
189 | this.v5 = v5;
190 | }
191 |
192 | public long getV6() {
193 | return v6;
194 | }
195 |
196 | public void setV6(long v6) {
197 | this.v6 = v6;
198 | }
199 |
200 | public int getV7() {
201 | return v7;
202 | }
203 |
204 | public void setV7(int v7) {
205 | this.v7 = v7;
206 | }
207 |
208 | public long getV8() {
209 | return v8;
210 | }
211 |
212 | public void setV8(long v8) {
213 | this.v8 = v8;
214 | }
215 |
216 | public int getV9() {
217 | return v9;
218 | }
219 |
220 | public void setV9(int v9) {
221 | this.v9 = v9;
222 | }
223 |
224 | public long getV10() {
225 | return v10;
226 | }
227 |
228 | public void setV10(long v10) {
229 | this.v10 = v10;
230 | }
231 |
232 | public int getV11() {
233 | return v11;
234 | }
235 |
236 | public void setV11(int v11) {
237 | this.v11 = v11;
238 | }
239 |
240 | public long getV12() {
241 | return v12;
242 | }
243 |
244 | public void setV12(long v12) {
245 | this.v12 = v12;
246 | }
247 |
248 | public boolean isV13() {
249 | return v13;
250 | }
251 |
252 | public void setV13(boolean v13) {
253 | this.v13 = v13;
254 | }
255 |
256 | public String getV14() {
257 | return v14;
258 | }
259 |
260 | public void setV14(String v14) {
261 | this.v14 = v14;
262 | }
263 |
264 | public ByteString getV15() {
265 | return v15;
266 | }
267 |
268 | public void setV15(ByteString v15) {
269 | this.v15 = v15;
270 | }
271 |
272 | public Status getV16() {
273 | return v16;
274 | }
275 |
276 | public void setV16(Status v16) {
277 | this.v16 = v16;
278 | }
279 |
280 | public List getRv1() {
281 | return rv1;
282 | }
283 |
284 | public void setRv1(List rv1) {
285 | this.rv1 = rv1;
286 | }
287 |
288 | public List getRv2() {
289 | return rv2;
290 | }
291 |
292 | public void setRv2(List rv2) {
293 | this.rv2 = rv2;
294 | }
295 |
296 | public List getRv3() {
297 | return rv3;
298 | }
299 |
300 | public void setRv3(List rv3) {
301 | this.rv3 = rv3;
302 | }
303 |
304 | public List getRv4() {
305 | return rv4;
306 | }
307 |
308 | public void setRv4(List rv4) {
309 | this.rv4 = rv4;
310 | }
311 |
312 | public List getRv5() {
313 | return rv5;
314 | }
315 |
316 | public void setRv5(List rv5) {
317 | this.rv5 = rv5;
318 | }
319 |
320 | public List getRv6() {
321 | return rv6;
322 | }
323 |
324 | public void setRv6(List rv6) {
325 | this.rv6 = rv6;
326 | }
327 |
328 | public List getRv7() {
329 | return rv7;
330 | }
331 |
332 | public void setRv7(List rv7) {
333 | this.rv7 = rv7;
334 | }
335 |
336 | public List getRv8() {
337 | return rv8;
338 | }
339 |
340 | public void setRv8(List rv8) {
341 | this.rv8 = rv8;
342 | }
343 |
344 | public List getRv9() {
345 | return rv9;
346 | }
347 |
348 | public void setRv9(List rv9) {
349 | this.rv9 = rv9;
350 | }
351 |
352 | public List getRv10() {
353 | return rv10;
354 | }
355 |
356 | public void setRv10(List rv10) {
357 | this.rv10 = rv10;
358 | }
359 |
360 | public List getRv11() {
361 | return rv11;
362 | }
363 |
364 | public void setRv11(List rv11) {
365 | this.rv11 = rv11;
366 | }
367 |
368 | public List getRv12() {
369 | return rv12;
370 | }
371 |
372 | public void setRv12(List rv12) {
373 | this.rv12 = rv12;
374 | }
375 |
376 | public List getRv13() {
377 | return rv13;
378 | }
379 |
380 | public void setRv13(List rv13) {
381 | this.rv13 = rv13;
382 | }
383 |
384 | public List getRv14() {
385 | return rv14;
386 | }
387 |
388 | public void setRv14(List rv14) {
389 | this.rv14 = rv14;
390 | }
391 |
392 | public List getRv15() {
393 | return rv15;
394 | }
395 |
396 | public void setRv15(List rv15) {
397 | this.rv15 = rv15;
398 | }
399 |
400 | public List getRv16() {
401 | return rv16;
402 | }
403 |
404 | public void setRv16(List rv16) {
405 | this.rv16 = rv16;
406 | }
407 |
408 | public MultiNumber getMultiNumber() {
409 | return multiNumber;
410 | }
411 |
412 | public void setMultiNumber(MultiNumber multiNumber) {
413 | this.multiNumber = multiNumber;
414 | }
415 |
416 | public List getRepMultiNumbers() {
417 | return repMultiNumbers;
418 | }
419 |
420 | public void setRepMultiNumbers(List rMultiNumbers) {
421 | repMultiNumbers = rMultiNumbers;
422 | }
423 |
424 | public String getNonRepeatableFieldWithSuffixList() {
425 | return nonRepeatableFieldWithSuffixList;
426 | }
427 |
428 | public void setNonRepeatableFieldWithSuffixList(String nonRepeatableFieldWithSuffixList) {
429 | this.nonRepeatableFieldWithSuffixList = nonRepeatableFieldWithSuffixList;
430 | }
431 |
432 | public User getUser() {
433 | return user;
434 | }
435 |
436 | public void setUser(User user) {
437 | this.user = user;
438 | }
439 |
440 | public List getUsers() {
441 | return users;
442 | }
443 |
444 | public void setUsers(List users) {
445 | this.users = users;
446 | }
447 | }
448 |
--------------------------------------------------------------------------------
/usage/src/main/proto/EnumPostfixOverride.proto:
--------------------------------------------------------------------------------
1 | syntax = "proto3";
2 |
3 | option java_package = "no.entur.mapstruct.example";
4 | option java_outer_classname = "EnumPostfixOverrideProtos";
5 |
6 | enum EnumPostfixOverrideValuesDTO {
7 | ENUM_POSTFIX_OVERRIDE_VALUES_D_T_O_INVALID = 0;
8 | ENUM_POSTFIX_OVERRIDE_VALUES_D_T_O_ONE = 1;
9 | ENUM_POSTFIX_OVERRIDE_VALUES_D_T_O_TWO = 2;
10 | }
11 |
--------------------------------------------------------------------------------
/usage/src/main/proto/User.proto:
--------------------------------------------------------------------------------
1 | syntax = "proto3";
2 |
3 | option java_package = "no.entur.mapstruct.example";
4 | option java_outer_classname = "UserProtos";
5 |
6 | enum EnumStatus {
7 | ENUM_STATUS_UNSPECIFIED = 0;
8 | ENUM_STATUS_STARTED = 1;
9 | ENUM_STATUS_STOPPED = 2;
10 | }
11 |
12 | message MultiNumberDTO {
13 | oneof number {
14 | int32 integer = 1;
15 | double floating = 2;
16 | }
17 | }
18 |
19 | message UserDTO {
20 | oneof OneOfDepartments {
21 | DepartmentDTO fire_department = 123;
22 | DepartmentDTO police_department = 124;
23 | }
24 |
25 | string id = 1;
26 | string email = 2;
27 | repeated DepartmentDTO main_departments = 4;
28 | repeated DepartmentDTO departments = 5;
29 |
30 | double v1 = 6;
31 | float v2 = 7;
32 | int32 v3 = 8;
33 | int64 v4 = 9;
34 | uint32 v5 = 10;
35 | uint64 v6 = 11;
36 | sint32 v7 = 12;
37 | sint64 v8 = 13;
38 | fixed32 v9 = 14;
39 | fixed64 v10 = 15;
40 | sfixed32 v11 = 16;
41 | sfixed64 v12 = 17;
42 | bool v13 = 18;
43 | string v14 = 19;
44 | bytes v15 = 20;
45 |
46 | EnumStatus v16 = 21;
47 | // google.protobuf.Any v17 = 22;
48 |
49 | // oneof v18 {
50 | // int32 oneofv1 = 23;
51 | // double oneofv2 = 24;
52 | // }
53 |
54 | map stringMap = 25;
55 | map entityMap = 26;
56 |
57 | MultiNumberDTO multi_number = 27;
58 |
59 | UserDTO user = 28;
60 |
61 | repeated double rv1 = 100;
62 | repeated float rv2 = 101;
63 | repeated int32 rv3 = 102;
64 | repeated int64 rv4 = 103;
65 | repeated uint32 rv5 = 104;
66 | repeated uint64 rv6 = 105;
67 | repeated sint32 rv7 = 106;
68 | repeated sint64 rv8 = 107;
69 | repeated fixed32 rv9 = 108;
70 | repeated fixed64 rv10 = 109;
71 | repeated sfixed32 rv11 = 110;
72 | repeated sfixed64 rv12 = 111;
73 | repeated bool rv13 = 112;
74 | repeated string rv14 = 113;
75 | repeated bytes rv15 = 114;
76 |
77 | repeated EnumStatus rv16 = 115;
78 | // repeated google.protobuf.Any rv17 = 116;
79 |
80 | repeated MultiNumberDTO rep_multi_numbers = 120;
81 |
82 | string non_repeatable_field_with_suffix_list = 121;
83 |
84 | repeated UserDTO users = 122;
85 |
86 | }
87 |
88 | enum PermissionDTO {
89 | PERMISSION_D_T_O_UNSPECIFIED = 0;
90 | PERMISSION_D_T_O_USER = 1;
91 | PERMISSION_D_T_O_ADMIN = 2;
92 | PERMISSION_D_T_O_NONE = 3;
93 | }
94 |
95 | message DepartmentDTO {
96 | string name = 1;
97 | }
98 |
99 |
--------------------------------------------------------------------------------
/usage/src/test/java/no/entur/mapstruct/example/MappingTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3 | * and/or other contributors as indicated by the @authors tag. See the
4 | * copyright.txt file in the distribution for a full listing of all
5 | * contributors.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *