Skip to content

Commit

Permalink
Add Property class
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Nov 21, 2023
1 parent d6269bf commit 2c548fc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.Unmodifiable;

import java.util.Map;
import java.util.Objects;

public interface ObjectMapper<T> {

Expand All @@ -33,5 +34,41 @@ public interface ObjectMapper<T> {

@NotNull Class<T> getObjectClass();

@Unmodifiable @NotNull Map<@NotNull String, @NotNull Type<?>> getProperties();
@NotNull Property<?> @NotNull [] getProperties();

interface Property<T> {

@NotNull String getKey();

@NotNull Type<T> getType();

boolean isOptional();

static <T> Property<T> of(@NotNull String key, @NotNull Type<T> type, boolean optional) {
Objects.requireNonNull(key);
Objects.requireNonNull(type);
if (key.isEmpty())
throw new IllegalArgumentException("Empty key");
return new Property<T>() {
@Override
public @NotNull String getKey() {
return key;
}

@Override
public @NotNull Type<T> getType() {
return type;
}

@Override
public boolean isOptional() {
return optional;
}
};
}

static <T> Property<T> of(@NotNull String key, @NotNull Type<T> type) {
return of(key, type, false);
}
}
}
25 changes: 11 additions & 14 deletions core/src/main/java/io/github/almightysatan/jaskl/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ public interface Type<T> {
@SuppressWarnings("unchecked")
static <T> @NotNull Type<T> custom(@NotNull ObjectMapper<T> objectMapper) {
Objects.requireNonNull(objectMapper);
@NotNull Map<@NotNull String, @NotNull Type<?>> properties = objectMapper.getProperties();
Objects.requireNonNull(properties);
Map<String, ObjectMapper.Property<?>> properties = new HashMap<>();
for (ObjectMapper.Property<?> property : objectMapper.getProperties())
if (properties.put(property.getKey(), ObjectMapper.Property.of(property.getKey(), property.getType(), property.isOptional())) != null)
throw new IllegalArgumentException("Duplicate key: " + property.getKey());
if (properties.isEmpty())
throw new IllegalArgumentException("properties should not be empty");
throw new IllegalArgumentException("Properties should not be empty");
return new Type<T>() {
@Override
public @NotNull T toEntryType(@NotNull Object value) throws InvalidTypeException, ValidationException {
Expand All @@ -343,8 +345,8 @@ public interface Type<T> {
Map<String, Object> newMap = new HashMap<>();
for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
String key = STRING.toEntryType(entry.getKey());
Type<?> type = this.getType(key);
newMap.put(key, type.toEntryType(entry.getValue()));
ObjectMapper.Property<?> property = properties.get(key);
newMap.put(key, property.getType().toEntryType(entry.getValue()));
}

return objectMapper.createInstance(Collections.unmodifiableMap(newMap));
Expand All @@ -353,18 +355,13 @@ public interface Type<T> {
@Override
public @NotNull Object toWritable(@NotNull T value, @NotNull Function<@NotNull Object, @NotNull Object> keyPreprocessor) throws InvalidTypeException {
Map<Object, Object> newMap = new HashMap<>();
for (Map.Entry<String, Object> entry : objectMapper.readValues(value).entrySet())
newMap.put(keyPreprocessor.apply(STRING.toWritable(entry.getKey(), keyPreprocessor)), this.getType(entry.getKey()).toWritable(entry.getValue(), keyPreprocessor));
for (Map.Entry<String, Object> entry : objectMapper.readValues(value).entrySet()) {
ObjectMapper.Property<Object> property = (ObjectMapper.Property<Object>) properties.get(entry.getKey());
newMap.put(keyPreprocessor.apply(STRING.toWritable(entry.getKey(), keyPreprocessor)), property.getType().toWritable(entry.getValue(), keyPreprocessor));
}

return Collections.unmodifiableMap(newMap);
}

private <U> Type<U> getType(String key) {
Type<U> type = (Type<U>) properties.get(key);
if (type == null)
throw new InvalidTypeException(key, properties.keySet());
return type;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,10 @@ public static void testCustom(Supplier<Config> configSupplier) throws IOExceptio
}

@Override
public @NotNull Map<@NotNull String, @NotNull Type<?>> getProperties() {
Map<String, Type<?>> properties = new HashMap<>();
properties.put("exampleString", Type.STRING);
properties.put("exampleInt", Type.INTEGER);
properties.put("exampleEnum", Type.enumType(ExampleEnum.class));
return Collections.unmodifiableMap(properties);
public @NotNull Property<?> @NotNull [] getProperties() {
return new Property[]{Property.of("exampleString", Type.STRING),
Property.of("exampleInt", Type.INTEGER),
Property.of("exampleEnum", Type.enumType(ExampleEnum.class))};
}
};
Config config0 = configSupplier.get();
Expand Down

0 comments on commit 2c548fc

Please sign in to comment.