-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support simple generic arguments for List and Set mappings
- Loading branch information
1 parent
01ca574
commit 2f21ca6
Showing
15 changed files
with
549 additions
and
283 deletions.
There are no files selected for viewing
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
4 changes: 1 addition & 3 deletions
4
...o/jonasg/xjx/serdes/deserialize/Path.java → .../main/java/io/jonasg/xjx/serdes/Path.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
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,35 +1,4 @@ | ||
package io.jonasg.xjx.serdes; | ||
|
||
import java.util.StringJoiner; | ||
|
||
public class Section { | ||
|
||
private final String name; | ||
private final boolean isLeaf; | ||
|
||
public Section(String name) { | ||
this.name = name; | ||
isLeaf = false; | ||
} | ||
|
||
public Section(String name, boolean isLeaf) { | ||
this.name = name; | ||
this.isLeaf = isLeaf; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public boolean isLeaf() { | ||
return isLeaf; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", Section.class.getSimpleName() + "[", "]") | ||
.add("name='" + name + "'") | ||
.add("isLeaf=" + isLeaf) | ||
.toString(); | ||
} | ||
public record Section(String name, boolean isLeaf) { | ||
} |
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
89 changes: 89 additions & 0 deletions
89
xjx-serdes/src/main/java/io/jonasg/xjx/serdes/TypeMappers.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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.jonasg.xjx.serdes; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
public final class TypeMappers { | ||
|
||
static List<Class<Double>> DOUBLE_TYPES = List.of(double.class, Double.class); | ||
static List<Class<Long>> LONG_TYPES = List.of(long.class, Long.class); | ||
static List<Class<Character>> CHAR_TYPES = List.of(char.class, Character.class); | ||
static List<Class<Boolean>> BOOLEAN_TYPES = List.of(boolean.class, Boolean.class); | ||
|
||
public static Set<Class<?>> TYPES; | ||
|
||
static { | ||
TYPES = new HashSet<>(); | ||
TYPES.addAll(DOUBLE_TYPES); | ||
TYPES.addAll(LONG_TYPES); | ||
TYPES.addAll(CHAR_TYPES); | ||
TYPES.addAll(BOOLEAN_TYPES); | ||
TYPES.add(String.class); | ||
TYPES.add(LocalDate.class); | ||
} | ||
|
||
public static Function<Object, Object> forType(Class<?> type) { | ||
Function<Object, Object> mapper = Function.identity(); | ||
if (type.equals(String.class)) { | ||
mapper = String::valueOf; | ||
} | ||
if (type.equals(Integer.class)) { | ||
mapper = value -> Integer.parseInt(String.valueOf(value)); | ||
} | ||
if (LONG_TYPES.contains(type)) { | ||
mapper = value -> Long.parseLong(String.valueOf(value)); | ||
} | ||
if (type.equals(BigDecimal.class)) { | ||
mapper = value -> new BigDecimal(String.valueOf(value)); | ||
} | ||
if (DOUBLE_TYPES.contains(type)) { | ||
mapper = value -> Double.valueOf(String.valueOf(value)); | ||
} | ||
if (CHAR_TYPES.contains(type)) { | ||
mapper = value -> String.valueOf(value).charAt(0); | ||
} | ||
if (BOOLEAN_TYPES.contains(type)) { | ||
mapper = value -> { | ||
String lowered = String.valueOf(value).toLowerCase(); | ||
if (lowered.equals("true") || lowered.equals("yes") || lowered.equals("1")) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
} | ||
if (type.equals(LocalDate.class)) { | ||
mapper = value -> LocalDate.parse(String.valueOf(value)); | ||
} | ||
if (type.equals(LocalDateTime.class)) { | ||
mapper = value -> LocalDateTime.parse(String.valueOf(value)); | ||
} | ||
if (type.equals(ZonedDateTime.class)) { | ||
mapper = value -> ZonedDateTime.parse(String.valueOf(value)); | ||
} | ||
if (type.isEnum()) { | ||
mapper = value -> toEnum(type, String.valueOf(value)); | ||
} | ||
return mapper; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T extends Enum<T>> T toEnum(Class<?> type, String value) { | ||
try { | ||
T[] enumConstants = (T[]) type.getEnumConstants(); | ||
for (T constant : enumConstants) { | ||
if (value.equals(constant.name())) { | ||
return constant; | ||
} | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return null; | ||
} | ||
} |
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.