Skip to content

Commit

Permalink
Allow any string-like type to be a map key
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Oct 20, 2024
1 parent e9b08ad commit 6f69b44
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import io.github.kale_ko.bjsl.processor.conditions.ExpectIsNull;
import io.github.kale_ko.bjsl.processor.conditions.ExpectLessThan;
import io.github.kale_ko.bjsl.processor.conditions.ExpectNotNull;
import io.github.kale_ko.bjsl.processor.exception.EnumExpectedException;
import io.github.kale_ko.bjsl.processor.exception.ExpectFailedException;
import io.github.kale_ko.bjsl.processor.exception.InitializationException;
import io.github.kale_ko.bjsl.processor.exception.ProcessorException;
import io.github.kale_ko.bjsl.processor.exception.*;
import io.github.kale_ko.bjsl.processor.reflection.InitializationUtil;
import java.io.File;
import java.io.PrintWriter;
Expand Down Expand Up @@ -1259,17 +1256,17 @@ public boolean getEnableDefaultTypeProcessors() {
} else if (!type.getRawClass().isAnonymousClass() && !type.getRawClass().isAnnotation()) {
if (element instanceof ParsedObject) {
if (type instanceof MapType) {
Map<String, Object> object;
Map<Object, Object> object;
if (!type.getRawClass().isInterface()) {
object = (Map<String, Object>) InitializationUtil.initialize(type.getRawClass());
object = (Map<Object, Object>) InitializationUtil.initialize(type.getRawClass());
} else {
object = InitializationUtil.initialize(LinkedHashMap.class);
}

for (Map.Entry<String, ParsedElement> entry : element.asObject().getEntries()) {
Object subObject = toObject(entry.getValue(), type.getContentType());
if (!((ignoreNulls && subObject == null) || (ignoreEmptyObjects && subObject instanceof Object[] && ((Object[]) subObject).length == 0) || (ignoreEmptyObjects && subObject instanceof Collection<?> && ((Collection<?>) subObject).isEmpty()) || (ignoreEmptyObjects && subObject instanceof Map<?, ?> && ((Map<?, ?>) subObject).isEmpty()))) {
object.put(entry.getKey(), subObject);
object.put(toObject(ParsedPrimitive.fromString(entry.getKey()), type.getBindings().getTypeParameters().get(0)), subObject);
}
}

Expand Down Expand Up @@ -1787,7 +1784,7 @@ public boolean getEnableDefaultTypeProcessors() {
for (Map.Entry<?, ?> entry : Map.copyOf((Map<?, ?>) object).entrySet()) {
ParsedElement subElement = toElement(entry.getValue());
if (!((ignoreNulls && subElement.isPrimitive() && subElement.asPrimitive().isNull()) || (ignoreEmptyObjects && subElement.isArray() && subElement.asArray().getSize() == 0) || (ignoreEmptyObjects && subElement.isObject() && subElement.asObject().getSize() == 0))) {
objectElement.set(entry.getKey().toString(), subElement);
objectElement.set(toString(entry.getKey()), subElement);
}
}

Expand Down Expand Up @@ -1856,6 +1853,51 @@ public boolean getEnableDefaultTypeProcessors() {
}
}

private @Nullable String toString(@Nullable Object object) {
try {
ParsedElement element = toElement(object);

if (element.isPrimitive()) {
ParsedPrimitive primitive = element.asPrimitive();

if (primitive.isString()) {
return primitive.asString();
} else if (primitive.isByte()) {
return Byte.toString(primitive.asByte());
} else if (primitive.isChar()) {
return Character.toString(primitive.asChar());
} else if (primitive.isShort()) {
return Short.toString(primitive.asShort());
} else if (primitive.isInteger()) {
return Integer.toString(primitive.asInteger());
} else if (primitive.isLong()) {
return Long.toString(primitive.asLong());
} else if (primitive.isBigInteger()) {
return primitive.asBigInteger().toString();
} else if (primitive.isFloat()) {
return Float.toString(primitive.asFloat());
} else if (primitive.isDouble()) {
return Double.toString(primitive.asDouble());
} else if (primitive.isBigDecimal()) {
return primitive.asBigDecimal().toString();
} else if (primitive.isBoolean()) {
return Boolean.toString(primitive.asBoolean());
} else if (primitive.isNull()) {
return "null";
} else {
throw new NotAPrimitiveException(element);
}
} else {
throw new NotAPrimitiveException(element);
}
} catch (ProcessorException e) {
throw e;
} catch (Exception e) {
throw new ProcessorException(e);
}
}


/**
* Get all the fields on a class and its superclasses
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.kale_ko.bjsl.processor.exception;

import io.github.kale_ko.bjsl.elements.ParsedElement;
import org.jetbrains.annotations.NotNull;

/**
* Thrown when a value is attempted to be used as a map key but is not a string or other string-able primitive.
*
* @version 2.0.0
* @since 2.0.0
*/
public class NotAPrimitiveException extends RuntimeException {
/**
* Create a new NotAPrimitiveException
*
* @param parsedObject The parsed object that was invalid
*/
public NotAPrimitiveException(@NotNull ParsedElement parsedObject) {
super("ParedElement is not a string or other string-able primitive, must be to be a map key!\n" + parsedObject);
}
}

0 comments on commit 6f69b44

Please sign in to comment.