Do null safety for objects properly
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful
This commit is contained in:
parent
7fbec30c92
commit
486f241eca
|
@ -33,9 +33,13 @@ public class Get extends DynamicExpr {
|
||||||
Dynamic<?> left = this.left.get(dataRoot);
|
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 (Dynamic.isNull(left)) throw new LocationalException(location, "Could not get \"" + name.asStringExpr().get(dataRoot) + "\" because left is null");
|
||||||
if (left instanceof DObject o) {
|
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));
|
return o.get(name.asStringExpr().get(dataRoot));
|
||||||
} else if (left instanceof DList l) {
|
} 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);
|
throw new DynamicTypeConversionException("object or list", left).locational(location);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,14 @@ package io.gitlab.jfronny.muscript.data.dynamic;
|
||||||
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
||||||
import io.gitlab.jfronny.muscript.ast.dynamic.ObjectLiteral;
|
import io.gitlab.jfronny.muscript.ast.dynamic.ObjectLiteral;
|
||||||
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
|
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public non-sealed interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
|
public non-sealed interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
|
||||||
default @NotNull Dynamic<?> get(String key) {
|
default @Nullable Dynamic<?> get(String key) {
|
||||||
return Dynamic.fromNullable(getValue().get(key));
|
return getValue().get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
default boolean has(String key) {
|
default boolean has(String key) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user