use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class ToLongTestCase method requireThatValueIsConverted.
@Test
public void requireThatValueIsConverted() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setValue(new StringFieldValue("69")).execute(new ToLongExpression());
FieldValue val = ctx.getValue();
assertTrue(val instanceof LongFieldValue);
assertEquals(69L, ((LongFieldValue) val).getLong());
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class AddRemoveCreator method createAddsOrRemoves.
// yes, this suppresswarnings ugliness is by intention, the code relies on
// the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) {
FieldValue container = field.getDataType().createFieldValue();
FieldUpdate singleUpdate;
int initNesting = buffer.nesting();
Preconditions.checkState(buffer.currentToken().isStructStart(), "Expected start of composite, got %s", buffer.currentToken());
if (container instanceof CollectionFieldValue) {
buffer.next();
DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType();
if (container instanceof WeightedSet) {
// these are objects with string keys (which are the nested
// types) and values which are the weight
WeightedSet weightedSet = (WeightedSet) container;
fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet);
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet);
} else {
singleUpdate = FieldUpdate.createAddAll(field, weightedSet);
}
} else {
List<FieldValue> arrayContents = new ArrayList<>();
ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents);
if (buffer.currentToken() != JsonToken.END_ARRAY) {
throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'.");
}
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents);
} else {
singleUpdate = FieldUpdate.createAddAll(field, arrayContents);
}
}
} else {
throw new UnsupportedOperationException("Trying to add or remove from a field of a type the reader does not know how to handle: " + container.getClass().getName());
}
expectCompositeEnd(buffer.currentToken());
update.addAll(singleUpdate);
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class MapReader method createMapUpdate.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ValueUpdate createMapUpdate(TokenBuffer buffer, DataType currentLevel, FieldValue keyParent, FieldValue topLevelKey) {
TokenBuffer.Token element = buffer.prefetchScalar(UPDATE_ELEMENT);
if (UPDATE_ELEMENT.equals(buffer.currentName())) {
buffer.next();
}
FieldValue key = keyTypeForMapUpdate(element, currentLevel);
if (keyParent != null) {
((CollectionFieldValue) keyParent).add(key);
}
// match will always have element, and either match or action
if (!UPDATE_MATCH.equals(buffer.currentName())) {
// we have reached an action...
if (topLevelKey == null) {
return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
} else {
return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
}
} else {
// next level of matching
if (topLevelKey == null) {
return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key);
} else {
return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey);
}
}
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class StructReader method fillStruct.
public static void fillStruct(TokenBuffer buffer, StructuredFieldValue parent) {
// do note the order of initializing initNesting and token is relevant for empty docs
int initNesting = buffer.nesting();
buffer.next();
while (buffer.nesting() >= initNesting) {
Field f = getField(buffer, parent);
try {
FieldValue v = readSingleValue(buffer, f.getDataType());
parent.setFieldValue(f, v);
buffer.next();
} catch (IllegalArgumentException e) {
throw new JsonReaderException(f, e);
}
}
}
use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.
the class WeightedSetReader method iterateThroughWeightedSet.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void iterateThroughWeightedSet(TokenBuffer buffer, int initNesting, DataType valueType, WeightedSet weightedSet) {
while (buffer.nesting() >= initNesting) {
// XXX the keys are defined in the spec to always be represented as strings
FieldValue v = valueType.createFieldValue(buffer.currentName());
weightedSet.put(v, Integer.valueOf(buffer.currentText()));
buffer.next();
}
}
Aggregations