switch (token) { case VALUE_NULL: return ValueFactory.newNil(); case VALUE_TRUE: return ValueFactory.newBoolean(true); case VALUE_FALSE: return ValueFactory.newBoolean(false); case VALUE_NUMBER_FLOAT: return ValueFactory.newFloat(parser.getDoubleValue()); case VALUE_NUMBER_INT: try { return ValueFactory.newInteger(parser.getLongValue()); } catch (com.fasterxml.jackson.core.JsonParseException ex) { return ValueFactory.newInteger(parser.getBigIntegerValue()); return ValueFactory.newString(parser.getText()); case START_ARRAY: { List<Value> list = new ArrayList<>(); token = parser.nextToken(); if (token == JsonToken.END_ARRAY) { return ValueFactory.newArray(list); } else if (token == null) { throw new JsonParseException( token = parser.nextToken(); if (token == JsonToken.END_OBJECT) { return ValueFactory.newMap(map); } else if (token == null) {
@Test public void visitArray_AddColumns() { PluginTask task = taskFromYamlString( "type: column", "add_columns:", " - {name: \"$.json1.k1[1]\", src: \"$.json1.k1[0]\"}", " - {name: \"$.json1.k3[*].k2\", type: string, default: v}", " - {name: \"$.json1.k4[*].k1\", type: string, default: v}", " - {name: \"$.json1.k5[0].k1\", type: string, default: v}"); Schema inputSchema = Schema.builder() .add("json1", JSON) .add("json2", JSON) .build(); JsonVisitor subject = jsonVisitor(task, inputSchema); // {"k1":[{"k1":"v"}],"k2":["v","v"],"k3":[{"k1":"v"}]} Value k1 = ValueFactory.newString("k1"); Value k2 = ValueFactory.newString("k2"); Value k3 = ValueFactory.newString("k3"); Value v = ValueFactory.newString("v"); Value map = ValueFactory.newMap( k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)), k2, ValueFactory.newArray(v, v), k3, ValueFactory.newArray(ValueFactory.newMap(k1, v))); MapValue visited = subject.visit("$['json1']", map).asMapValue(); assertEquals("{\"k1\":[{\"k1\":\"v\"},{\"k1\":\"v\"}],\"k2\":[\"v\",\"v\"],\"k3\":[{\"k1\":\"v\",\"k2\":\"v\"}],\"k4\":[],\"k5\":[{\"k1\":\"v\"}]}", visited.toString()); }
static Config buildStoreParams(ConfigFactory cf, TDJobOperator j, boolean storeLastResults, TaskState state, DurationInterval retryInterval) { if (storeLastResults) { Config td = cf.create(); List<ArrayValue> results = downloadFirstResults(j, 1, state, RESULT, retryInterval); Map<RawValue, Value> map = new LinkedHashMap<>(); if (!results.isEmpty()) { ArrayValue row = results.get(0); List<String> columnNames = j.getResultColumnNames(); for (int i = 0; i < Math.min(row.size(), columnNames.size()); i++) { map.put(ValueFactory.newString(columnNames.get(i)), row.get(i)); } } MapValue lastResults = ValueFactory.newMap(map); try { td.set("last_results", new ObjectMapper().readTree(lastResults.toJson())); } catch (IOException ex) { throw Throwables.propagate(ex); } return cf.create().set("td", td); } else { return cf.create(); } }
case NIL: readByte(); return ValueFactory.newNil(); case BOOLEAN: return ValueFactory.newBoolean(unpackBoolean()); case INTEGER: switch (mf) { case UINT64: return ValueFactory.newInteger(unpackBigInteger()); default: return ValueFactory.newInteger(unpackLong()); return ValueFactory.newFloat(unpackDouble()); case STRING: { int length = unpackRawStringHeader(); return ValueFactory.newString(readPayload(length), true); return ValueFactory.newBinary(readPayload(length), true); array[i] = unpackValue(); return ValueFactory.newArray(array, true); i++; return ValueFactory.newMap(kvs, true); return ValueFactory.newExtension(extHeader.getType(), readPayload(extHeader.getLength()));
@Override public void set(String v) { pageBuilder.setJson(column, ValueFactory.newString(v)); }
static Value getDefault(PluginTask task, String name, Type type, ColumnConfig columnConfig) { Object defaultValue = ColumnVisitorImpl.getDefault(task, name, type, columnConfig); if (defaultValue == null) { return ValueFactory.newNil(); } if (type instanceof BooleanType) { return ValueFactory.newBoolean((Boolean) defaultValue); } else if (type instanceof LongType) { return ValueFactory.newInteger((Long) defaultValue); } else if (type instanceof DoubleType) { return ValueFactory.newFloat((Double) defaultValue); } else if (type instanceof StringType) { return ValueFactory.newString((String) defaultValue.toString()); } else if (type instanceof JsonType) { return (Value) defaultValue; } else if (type instanceof TimestampType) { throw new ConfigException("type: timestamp is not available in json path"); } else { throw new ConfigException(String.format("type: '%s' is not supported", type)); } }
public JsonColumn(String path, Type type, Value defaultValue, String src) { Path compiledPath = PathCompiler.compile(path); Path compiledSrc = src == null ? compiledPath : PathCompiler.compile(src); RootPathToken compiledRoot = (RootPathToken) compiledPath.getRoot(); RootPathToken compiledSrcRoot = (RootPathToken) compiledSrc.getRoot(); this.path = compiledPath.toString(); this.type = type; this.defaultValue = (defaultValue == null ? ValueFactory.newNil() : defaultValue); this.src = compiledSrc.toString(); this.pathValue = ValueFactory.newString(path); this.parentPath = compiledPath.getParentPath(); this.tailIndex = getTailIndex(compiledRoot); this.parentPathValue = ValueFactory.newString(parentPath); String tailName = getTailName(compiledRoot); this.tailNameValue = tailName == null ? ValueFactory.newNil() : ValueFactory.newString(tailName); this.srcValue = ValueFactory.newString(this.src); this.srcParentPath = compiledSrc.getParentPath(); this.srcTailIndex = getTailIndex(compiledSrcRoot); this.srcParentPathValue = ValueFactory.newString(this.srcParentPath); String srcTailName = getTailName(compiledSrcRoot); this.srcTailNameValue = srcTailName == null ? ValueFactory.newNil() : ValueFactory.newString(srcTailName); if (!srcParentPath.equals(parentPath)) { throw new ConfigException(String.format("The branch (parent path) of src \"%s\" must be same with of name \"%s\" yet", src, path)); } }
@Override public void set(long v) { pageBuilder.setJson(column, ValueFactory.newInteger(v)); }
public static ArrayList<JsonColumn> getAncestorJsonColumnList(String path) { ArrayList<JsonColumn> ancestorJsonColumnList = new ArrayList<>(); Path compiledPath; try { compiledPath = PathCompiler.compile(path); } catch (InvalidPathException e) { throw new ConfigException(String.format("jsonpath %s, %s", path, e.getMessage())); } StringBuilder partialPath = new StringBuilder("$"); PathToken parts = compiledPath.getRoot(); parts = parts.next(); // skip "$" while (! parts.isLeaf()) { partialPath.append(parts.getPathFragment()); PathToken next = parts.next(); JsonColumn jsonColumn; if (next instanceof ArrayPathToken || next instanceof WildcardPathToken) { jsonColumn = new JsonColumn(partialPath.toString(), Types.JSON, ValueFactory.newArray(new Value[0], false)); } else { jsonColumn = new JsonColumn(partialPath.toString(), Types.JSON, ValueFactory.newMap(new Value[0])); } ancestorJsonColumnList.add(jsonColumn); parts = next; } return ancestorJsonColumnList; }
@Override public void set(boolean v) { pageBuilder.setJson(column, ValueFactory.newBoolean(v)); }
public MapValue build() { return newMap(map); }
Value visited = visit(newPath, v); newValue.add(i++, jsonColumn.getTailNameValue()); newValue.add(i++, visited == null ? ValueFactory.newNil() : visited); Value visited = visit(newPath, v); newValue.add(i++, jsonColumn.getTailNameValue()); newValue.add(i++, visited == null ? ValueFactory.newNil() : visited); return ValueFactory.newMap(newValue.toArray(new Value[0]), true);
@Test public void initialize() { try { JsonColumn column = new JsonColumn("$.foo.bar", Types.BOOLEAN); assertEquals("$['foo']['bar']", column.getSrc()); assertEquals(ValueFactory.newNil(), column.getDefaultValue()); } catch (Exception e) { fail(); } try { Value defaultValue = ValueFactory.newBoolean(true); JsonColumn column = new JsonColumn("$['foo']['bar']", Types.BOOLEAN, defaultValue); assertEquals("$['foo']['bar']", column.getSrc()); assertEquals(defaultValue, column.getDefaultValue()); } catch (Exception e) { fail(); } }
newValue.add(j++, visited == null ? ValueFactory.newNil() : visited); newValue.add(j++, visited == null ? ValueFactory.newNil() : visited); Value visited = visit(newPath, v); newValue.add(j++, visited == null ? ValueFactory.newNil() : visited); return ValueFactory.newArray(newValue.toArray(new Value[0]), true);
StatefulAssociationValueType<?> valueType = descriptor.valueType(); ValueFactory.MapBuilder builder = ValueFactory.newMapBuilder(); valueType.properties().forEach( property -> ValueFactory.newString( property.qualifiedName().name() ), doSerialize( options, value, false ) ); } ); valueType.associations().forEach( association -> builder.put( ValueFactory.newString( association.qualifiedName().name() ), doSerialize( options, state.associationFor( association.accessor() ).reference(), false ) ) ); valueType.manyAssociations().forEach( association -> builder.put( ValueFactory.newString( association.qualifiedName().name() ), doSerialize( options, state.manyAssociationFor( association.accessor() ).references().collect( toList() ), valueType.namedAssociations().forEach( association -> builder.put( ValueFactory.newString( association.qualifiedName().name() ), doSerialize( options, state.namedAssociationFor( association.accessor() ).references().collect( toMap() ), builder.put( ValueFactory.newString( "_type" ), ValueFactory.newString( valueType.primaryType().getName() ) );
private ArrayValue serializeStream( Options options, Stream<?> stream ) { return ValueFactory.newArray( stream.map( element -> doSerialize( options, element, false ) ) .collect( toList() ) ); } }
@Override public void set(double v) { pageBuilder.setJson(column, ValueFactory.newFloat(v)); }
@Override public ImmutableNilValue immutableValue() { return ValueFactory.newNil(); }
@Test public void visitArray_DropColumns() { PluginTask task = taskFromYamlString( "type: column", "drop_columns:", " - {name: \"$.json1.k1[0].k1\"}", " - {name: \"$.json1.k2[*]\"}", // ending with [*] is allowed for drop_columns, but not for others " - {name: \"$.json1.k3[*].k1\"}"); Schema inputSchema = Schema.builder() .add("json1", JSON) .add("json2", JSON) .build(); JsonVisitor subject = jsonVisitor(task, inputSchema); // {"k1":[{"k1":"v"}],"k2":["v","v"],"k3":[{"k3":"v"}]} Value k1 = ValueFactory.newString("k1"); Value k2 = ValueFactory.newString("k2"); Value k3 = ValueFactory.newString("k3"); Value v = ValueFactory.newString("v"); Value map = ValueFactory.newMap( k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)), k2, ValueFactory.newArray(v, v), k3, ValueFactory.newArray(ValueFactory.newMap(k1, v))); MapValue visited = subject.visit("$['json1']", map).asMapValue(); assertEquals("{\"k1\":[{}],\"k2\":[],\"k3\":[{}]}", visited.toString()); }