Do null safety for objects properly
ci/woodpecker/manual/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-06-29 16:33:15 +02:00
parent 7fbec30c92
commit 486f241eca
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 7 additions and 4 deletions

View File

@ -33,9 +33,13 @@ public class Get extends DynamicExpr {
Dynamic<?> left = this.left.get(dataRoot);
if (Dynamic.isNull(left)) throw new LocationalException(location, "Could not get \"" + name.asStringExpr().get(dataRoot) + "\" because left is null");
if (left instanceof DObject o) {
var n = name.asStringExpr().get(dataRoot);
if (!o.has(n)) throw new LocationalException(location, "Object does not contain \"" + n + "\"");
return o.get(name.asStringExpr().get(dataRoot));
} else if (left instanceof DList l) {
return l.get(name.asNumberExpr().get(dataRoot).intValue());
int idx = name.asNumberExpr().get(dataRoot).intValue();
if (idx < 0 || idx >= l.size()) throw new LocationalException(location, "Index " + idx + " is out of range for list with size " + l.size());
return l.get(idx);
}
throw new DynamicTypeConversionException("object or list", left).locational(location);
}

View File

@ -3,15 +3,14 @@ package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.ObjectLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.stream.Collectors;
public non-sealed interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
default @NotNull Dynamic<?> get(String key) {
return Dynamic.fromNullable(getValue().get(key));
default @Nullable Dynamic<?> get(String key) {
return getValue().get(key);
}
default boolean has(String key) {