Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IMMUTABLE to handle Map and return correct type #949

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions warp10/src/main/java/io/warp10/script/functions/IMMUTABLE.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2018 SenX S.A.S.
// Copyright 2018-2021 SenX S.A.S.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,14 @@
package io.warp10.script.functions;

import io.warp10.script.NamedWarpScriptFunction;
import io.warp10.script.WarpScriptStackFunction;
import io.warp10.script.WarpScriptException;
import io.warp10.script.WarpScriptStack;
import io.warp10.script.WarpScriptStackFunction;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Makes a map/list/set immutable
Expand All @@ -31,18 +33,22 @@ public class IMMUTABLE extends NamedWarpScriptFunction implements WarpScriptStac
public IMMUTABLE(String name) {
super(name);
}

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

Object top = stack.pop();

if (!(top instanceof Collection)) {
throw new WarpScriptException(getName() + " operates on a list, map or set.");

if (top instanceof List) {
stack.push(Collections.unmodifiableList((List) top));
} else if (top instanceof Set) {
stack.push(Collections.unmodifiableSet((Set) top));
} else if (top instanceof Map) {
stack.push(Collections.unmodifiableMap((Map) top));
} else {
throw new WarpScriptException(getName() + " operates on a " + TYPEOF.TYPE_LIST + ", " + TYPEOF.TYPE_SET + " or " + TYPEOF.TYPE_MAP + ".");
}

stack.push(Collections.unmodifiableCollection((Collection) top));


return stack;
}

Expand Down