Adding b64 and json decoder and encoder functions (#829)

This commit is contained in:
Ghoulboy authored and GitHub committed 2021-04-26 21:21:35 -03:00
1 parent 28101e716b
commit 1bc2dd94da
4 files changed
+36

No files matched your search

@@ -27,6 +27,7 @@ public class Throwables {
public static final Throwables IO_EXCEPTION = register("io_exception", THROWN_EXCEPTION_TYPE);
public static final Throwables NBT_ERROR = register("nbt_error", IO_EXCEPTION);
public static final Throwables JSON_ERROR = register("json_error", IO_EXCEPTION);
public static final Throwables B64_ERROR = register("b64_error", IO_EXCEPTION);
public static final Throwables USER_DEFINED = register("user_exception", THROWN_EXCEPTION_TYPE);
/**
@@ -4,6 +4,9 @@ import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.ThrowStatement;
import carpet.script.exception.Throwables;
import carpet.script.utils.ScarpetJsonDeserializer;
import carpet.script.value.ContainerValueInterface;
import carpet.script.value.LContainerValue;
import carpet.script.value.LazyListValue;
@@ -12,8 +15,12 @@ import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
import carpet.script.value.StringValue;
import carpet.script.value.Value;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -320,5 +327,23 @@ public class DataStructures {
Value ret = new NumericValue(((ContainerValueInterface) container).delete(lv.get(lv.size()-1).evalValue(c)));
return (cc, tt) -> ret;
});
expression.addUnaryFunction("encode_b64", v -> StringValue.of(Base64.getEncoder().encodeToString(v.getString().getBytes())));
expression.addUnaryFunction("decode_b64", v -> {
try {
return StringValue.of(new String(Base64.getDecoder().decode(v.getString()), StandardCharsets.ISO_8859_1));//using this charset cos it's the one used in decoding function
} catch (IllegalArgumentException iae){
throw new ThrowStatement("Invalid b64 string: " + v.getString(), Throwables.B64_ERROR);
}
});
expression.addUnaryFunction("encode_json", v -> StringValue.of(v.toJson().toString()));
expression.addUnaryFunction("decode_json", v -> {
try {
return new ScarpetJsonDeserializer().deserialize(new JsonParser().parse(v.getString()), null, null);
} catch (JsonParseException jpe){
throw new ThrowStatement("Invalid json string: " + v.getString(), Throwables.JSON_ERROR);
}
});
}
}