Permalink
Browse files

Introduction of MetaKey as the type for keys on metadata.

  • Loading branch information...
neilellis committed Sep 13, 2017
1 parent 7aff637 commit 4c23062beb83264a805cd737997cdaa7ed931a61
@@ -4,4 +4,4 @@ then
source ~/.bash_profile
fi
export MAVEN_OPTS="$MAVEN_OPTS -XX:+TieredCompilation -XX:TieredStopAtLevel=1"
mvn -DfailIfNoTests=false -Dtest=DocTest,ParserQuickTest,ParserSlowTest -pl dollar-examples -am --offline test
mvn -DfailIfNoTests=false -Dtest=DocTest,ParserQuickTest -pl dollar-examples -am --offline clean test
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2014-2017 Neil Ellis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dollar.api;

import com.google.common.base.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MetaKey {
@NotNull
private final String value;

public MetaKey(@NotNull String value) {this.value = value;}

public static MetaKey of(@NotNull String value) {
return new MetaKey(value);
}

public static MetaKey of(@Nullable Object o) {
return of(o.toString());
}

public String asString() {
return value;
}

@Override
public int hashCode() {
return Objects.hashCode(value);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MetaKey metaKey = (MetaKey) o;
return Objects.equal(value, metaKey.value);
}

public String toString() {
return value;
}
}
@@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableList;
import dollar.api.DollarException;
import dollar.api.DollarStatic;
import dollar.api.MetaKey;
import dollar.api.Pipeable;
import dollar.api.Signal;
import dollar.api.SubType;
@@ -53,7 +54,7 @@


@NotNull
private final ConcurrentHashMap<String, Object> meta = new ConcurrentHashMap<>();
private final ConcurrentHashMap<MetaKey, Object> meta = new ConcurrentHashMap<>();

protected AbstractDollar() {

@@ -404,17 +405,17 @@ public boolean map() {

@Nullable
@Override
public <T> T meta(@NotNull String key) {
public <T> T meta(@NotNull MetaKey key) {
return (T) meta.get(key);
}

@Override
public void meta(@NotNull String key, @NotNull Object value) {
public void meta(@NotNull MetaKey key, @NotNull Object value) {
meta.put(key, value);
}

@Override
public void metaAttribute(@NotNull String key, @NotNull String value) {
public void metaAttribute(@NotNull MetaKey key, @NotNull String value) {
if (meta.containsKey(key)) {
@NotNull var result;
throw new DollarFailureException(ErrorType.METADATA_IMMUTABLE);
@@ -424,7 +425,7 @@ public void metaAttribute(@NotNull String key, @NotNull String value) {

@Nullable
@Override
public String metaAttribute(@NotNull String key) {
public String metaAttribute(@NotNull MetaKey key) {
return (String) meta.get(key);
}

@@ -19,6 +19,7 @@
import com.github.oxo42.stateless4j.StateMachine;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import dollar.api.MetaKey;
import dollar.api.Pipeable;
import dollar.api.Signal;
import dollar.api.SubType;
@@ -621,21 +622,21 @@ public var infof(@NotNull String message,

@NotNull
@Override
public Object meta(@NotNull String key) {
public Object meta(@NotNull MetaKey key) {
return getValue().meta(key);
}

@Override
public void meta(@NotNull String key, @NotNull Object value) {
public void meta(@NotNull MetaKey key, @NotNull Object value) {
getValue().meta(key, value);
}

@Override
public void metaAttribute(@NotNull String key, @NotNull String value) {getValue().metaAttribute(key, value);}
public void metaAttribute(@NotNull MetaKey key, @NotNull String value) {getValue().metaAttribute(key, value);}

@NotNull
@Override
public String metaAttribute(@NotNull String key) {return getValue().metaAttribute(key);}
public String metaAttribute(@NotNull MetaKey key) {return getValue().metaAttribute(key);}

@Override
public boolean neitherTrueNorFalse() {return getValue().neitherTrueNorFalse();}
@@ -17,6 +17,7 @@
package dollar.api.types;

import dollar.api.DollarStatic;
import dollar.api.MetaKey;
import dollar.api.Pipeable;
import dollar.api.SubType;
import dollar.api.exceptions.LambdaRecursionException;
@@ -47,7 +48,7 @@
@NotNull
protected final Pipeable lambda;
@NotNull
protected final ConcurrentHashMap<Object, Object> meta = new ConcurrentHashMap<>();
protected final ConcurrentHashMap<MetaKey, Object> meta = new ConcurrentHashMap<>();
private final boolean fixable;
@NotNull
private final var in;
@@ -145,9 +146,9 @@ public Object invoke(@Nullable Object proxy, @NotNull Method method, @Nullable O
} else if ("constraintLabel".equals(method.getName())) {
return meta.get(MetaConstants.CONSTRAINT_FINGERPRINT);
} else if ("metaAttribute".equals(method.getName()) && (args.length == 1)) {
return meta.get(args[0].toString());
return meta.get(MetaKey.of(args[0]));
} else if ("metaAttribute".equals(method.getName()) && (args.length == 2)) {
meta.put(String.valueOf(args[0]), String.valueOf(args[1]));
meta.put(MetaKey.of(args[0]), String.valueOf(args[1]));
return null;
} else if ("_scope".equals(method.getName())) {
throw new Exception("Deprectated (_scope)");
@@ -161,9 +162,9 @@ public Object invoke(@Nullable Object proxy, @NotNull Method method, @Nullable O
// meta.put(String.of(args[0]), String.of(args[1]));
// return null;
} else if ("meta".equals(method.getName()) && (args.length == 1)) {
return meta.get(args[0]);
return meta.get(MetaKey.of(args[0]));
} else if ("meta".equals(method.getName()) && (args.length == 2)) {
meta.put(args[0], args[1]);
meta.put(MetaKey.of(args[0]), args[1]);
return null;
} else if ("$listen".equals(method.getName())) {
String listenerId = UUID.randomUUID().toString();
@@ -185,7 +186,7 @@ public Object invoke(@Nullable Object proxy, @NotNull Method method, @Nullable O
notifyStack.get().remove(this);
return proxy;
} else if ("$remove".equals(method.getName())) {
listeners.remove(args[0]);
listeners.remove(args[0].toString());
return args[0];
} else if ("hasErrors".equals(method.getName())) {
return false;
@@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import dollar.api.DollarStatic;
import dollar.api.MetaKey;
import dollar.api.Pipeable;
import dollar.api.Signal;
import dollar.api.StateTracer;
@@ -560,24 +561,24 @@ public boolean map() {

@NotNull
@Override
public Object meta(@NotNull String key) {
public Object meta(@NotNull MetaKey key) {
return getValue().meta(key);
}

@Override
public void meta(@NotNull String key, @NotNull Object value) {
public void meta(@NotNull MetaKey key, @NotNull Object value) {
getValue().meta(key, value);

}

@Override
public void metaAttribute(@NotNull String key, @NotNull String value) {
public void metaAttribute(@NotNull MetaKey key, @NotNull String value) {
getValue().metaAttribute(key, value);
}

@NotNull
@Override
public String metaAttribute(@NotNull String key) {
public String metaAttribute(@NotNull MetaKey key) {
return getValue().metaAttribute(key);
}

@@ -16,38 +16,30 @@

package dollar.api.types.meta;

import dollar.api.MetaKey;
import org.jetbrains.annotations.NotNull;

public class MetaConstants {
@NotNull
public static final String CONSTRAINT_FINGERPRINT = "dollar.internal.constraint.fingerprint";
public static final MetaKey ASSIGNMENT_TYPE = MetaKey.of("dollar.internal.assignment.type");
@NotNull
public static final String IS_BUILTIN = "dollar.internal.builtin";

public static final MetaKey CONSTRAINT_FINGERPRINT = MetaKey.of("dollar.internal.constraint.fingerprint");
@NotNull
public static final String VARIABLE = "dollar.internal.variable";

public static final MetaKey CONSTRAINT_SOURCE = MetaKey.of("dollar.internal.constraint.source");
@NotNull
public static final String OPERATION_NAME = "dollar.internal.operation.name";

public static final MetaKey ID = MetaKey.of("dollar.internal.id");
@NotNull
public static final String OPERATION = "dollar.internal.operation";


public static final MetaKey IMPURE = MetaKey.of("dollar.internal.pure");
@NotNull
public static final String IMPURE = "dollar.internal.pure";
public static final MetaKey IS_BUILTIN = MetaKey.of("dollar.internal.builtin");
@NotNull
public static final String CONSTRAINT_SOURCE = "dollar.internal.constraint.source";

public static final MetaKey OPERATION = MetaKey.of("dollar.internal.operation");
@NotNull
public static final String SCOPES = "dollar.internal.scopes";

public static final MetaKey OPERATION_NAME = MetaKey.of("dollar.internal.operation.name");
@NotNull
public static final String ID = "dollar.internal.id";

public static final MetaKey SCOPES = MetaKey.of("dollar.internal.scopes");
@NotNull
public static final String ASSIGNMENT_TYPE = "dollar.internal.assignment.type";

public static final MetaKey TYPE_HINT = MetaKey.of("dollar.internal.type.hint");
@NotNull
public static final String TYPE_HINT = "dollar.internal.type.hint";
public static final MetaKey VARIABLE = MetaKey.of("dollar.internal.variable");
}
@@ -909,23 +909,23 @@ default boolean map() {
* @param key the key
* @return the meta object
*/
@Nullable <T> T meta(@NotNull String key);
@Nullable <T> T meta(@NotNull MetaKey key);

/**
* Sets meta attribute.
*
* @param key the key
* @param value the value
*/
<T> void meta(@NotNull String key, @NotNull T value);
<T> void meta(@NotNull MetaKey key, @NotNull T value);

/**
* Sets meta attribute.
*
* @param key the key
* @param value the value
*/
void metaAttribute(@NotNull String key, @NotNull String value);
void metaAttribute(@NotNull MetaKey key, @NotNull String value);

/**
* Gets meta attribute.
@@ -934,7 +934,7 @@ default boolean map() {
* @return the meta attribute
*/
@Nullable
String metaAttribute(@NotNull String key);
String metaAttribute(@NotNull MetaKey key);

/**
* Is negative.
@@ -61,6 +61,7 @@
import static dollar.api.types.meta.MetaConstants.VARIABLE;
import static dollar.internal.runtime.script.parser.DollarParserImpl.NAMED_PARAMETER_META_ATTR;
import static dollar.internal.runtime.script.parser.Symbols.VAR_USAGE_OP;
import static java.lang.String.format;

public final class DollarUtilFactory implements DollarUtil {

@@ -122,7 +123,7 @@ public void checkLearntType(@NotNull Token token, @Nullable Type type, @NotNull
new SourceCode(scope(), token).getSourceMessage()
);
if (getConfig().failFast()) {
throw new DollarScriptException(String.format(
throw new DollarScriptException(format(
"Type prediction failed, was expecting %s but most likely type is %s if this prediction is wrong please add an explicit cast (using 'as %s')",
type, prediction.probableType(), type.name()));
}
@@ -136,12 +137,12 @@ public var constrain(@NotNull Scope scope,
@NotNull var value,
@Nullable var constraint,
@Nullable SubType label) {
// System.err.println("(" + label + ") " + rhs.$type().constraint());
SubType valueLabel = value.constraintLabel();
if (!Objects.equals(valueLabel, label)) {
if ((label != null) && (valueLabel != null) && !valueLabel.isEmpty()) {
scope.handleError(
new DollarScriptException("Trying to assign an invalid constrained variable " + valueLabel + " vs " + label,
new DollarScriptException(format("Trying to assign an invalid constrained variable %s vs %s",
valueLabel, label),
value));
}
}
@@ -18,6 +18,7 @@

import com.google.common.io.ByteStreams;
import dollar.api.DollarStatic;
import dollar.api.MetaKey;
import dollar.api.Pipeable;
import dollar.api.Scope;
import dollar.api.VarKey;
@@ -90,7 +91,7 @@

public class DollarParserImpl implements DollarParser {
@NotNull
public static final String NAMED_PARAMETER_META_ATTR = "__named_parameter";
public static final MetaKey NAMED_PARAMETER_META_ATTR = MetaKey.of("__named_parameter");
@NotNull
private static final Logger log = LoggerFactory.getLogger("DollarParser");
@NotNull
Oops, something went wrong.

0 comments on commit 4c23062

Please sign in to comment.