org.msgpack.value.MapValue Java Examples

The following examples show how to use org.msgpack.value.MapValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitMap_AddColumns()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "add_columns:",
            "  - {name: $.json1.k3.k3, type: string, default: v}",
            "  - {name: $.json1.k4, src: $.json1.k2}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{\"k1\":\"v\"},\"k2\":{\"k2\":\"v\"},\"k3\":{\"k3\":\"v\"},\"k4\":{\"k2\":\"v\"}}", visited.toString());
}
 
Example #2
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visit_withColumnNameIncludingSingleQuotes()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            " - {name: \"$['\\\\'json1']['k1']\"}");
    Schema inputSchema = Schema.builder()
            .add("'json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":"v"}
    Value k1 = ValueFactory.newString("k1");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(k1, v);

    MapValue visited = subject.visit("$['\\'json1']", map).asMapValue();
    assertEquals("{\"k1\":\"v\"}", visited.toString());
}
 
Example #3
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visit_withComplexRename()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            " - {name: \"$.json1['k____1']\", src: \"$.json1['k.-=+1']\"}",
            " - {name: \"$.json1['k____1'][0]['k____1']\", src: \"$.json1['k____1'][0]['k.-=+1']\"}",
            " - {name: \"$['json1']['k_2']\", src: \"$['json1']['k.2']\"}",
            " - {name: \"$['json1']['k_2']['k_2']\", src: \"$['json1']['k_2']['k.2']\"}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k.1":[{"k.1":"v"}], "k.2":{"k.2":"v"}}
    Value k1 = ValueFactory.newString("k.-=+1");
    Value k2 = ValueFactory.newString("k.2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k____1\":[{\"k____1\":\"v\"}],\"k_2\":{\"k_2\":\"v\"}}", visited.toString());
}
 
Example #4
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visit_withSingleQuotesAndDoubleQuotes()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            " - {name: \"$['json1']['k_1']\", src: \"$['json1']['k.1']\"}",
            " - {name: '$[\"json1\"][\"k_1\"][0][\"k_1\"]', src: '$[\"json1\"][\"k_1\"][0][\"k.1\"]'}",
            " - {name: '$[\"json1\"][\"k_2\"]', src: '$[\"json1\"][\"k.2\"]'}",
            " - {name: '$[\"json1\"][\"k_2\"][\"k_2\"]', src: '$[\"json1\"][\"k_2\"][\"k.2\"]'}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k.1":[{"k.1":"v"}], "k.2":{"k.2":"v"}}
    Value k1 = ValueFactory.newString("k.1");
    Value k2 = ValueFactory.newString("k.2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k_1\":[{\"k_1\":\"v\"}],\"k_2\":{\"k_2\":\"v\"}}", visited.toString());
}
 
Example #5
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visit_withDotAndBracket()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            " - {name: \"$.json1['k_1']\"}",
            " - {name: \"$.json1['k_1'][0]['k_1']\"}",
            " - {name: \"$['json1']['k_2']\"}",
            " - {name: \"$['json1']['k_2']['k_2']\"}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k.1":[{"k.1":"v"}], "k.2":{"k.2":"v"}}
    Value k1 = ValueFactory.newString("k_1");
    Value k2 = ValueFactory.newString("k_2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k_1\":[{\"k_1\":\"v\"}],\"k_2\":{\"k_2\":\"v\"}}", visited.toString());
}
 
Example #6
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitArray_columnsUsingBracketNotation()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            "  - {name: \"$['json1']['k1'][1]\", src: \"$['json1']['k1'][0]\"}",
            "  - {name: \"$['json1']['k2'][0]\"}",
            "  - {name: \"$['json1']['k3'][0]['k3']\", type: string, default: v}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":[{"k1":"v"},"v"],"k2":["v","v"]}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v), v),
            k2, ValueFactory.newArray(v, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":[{\"k1\":\"v\"}],\"k2\":[\"v\"],\"k3\":[{\"k3\":\"v\"}]}", visited.toString());
}
 
Example #7
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitArray_addColumnsUsingBracketNotation()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "add_columns:",
            "  - {name: \"$['json1']['k1'][1]\", src: \"$['json1']['k1'][0]\"}",
            "  - {name: \"$['json1']['k3'][0]['k3']\", 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"]}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)),
            k2, ValueFactory.newArray(v, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":[{\"k1\":\"v\"},{\"k1\":\"v\"}],\"k2\":[\"v\",\"v\"],\"k3\":[{\"k3\":\"v\"}]}", visited.toString());
}
 
Example #8
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitArray_dropColumnsUsingBracketNotation()
{
    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
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":[{"k1":"v"}[,"k2":["v","v"]}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newArray(ValueFactory.newMap(k1, v)),
            k2, ValueFactory.newArray(v, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":[{}],\"k2\":[]}", visited.toString());
}
 
Example #9
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitMap_columnsUsingBracketNotation()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            "  - {name: \"$['json1']['k1']\"}",
            "  - {name: \"$['json1']['k2']['k2']\"}",
            "  - {name: \"$['json1']['k3']['k3']\", type: string, default: v}",
            "  - {name: \"$['json1']['k4']\", src: \"$['json1']['k2']\"}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k1":"v","k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{\"k1\":\"v\"},\"k2\":{\"k2\":\"v\"},\"k3\":{\"k3\":\"v\"},\"k4\":{\"k2\":\"v\"}}", visited.toString());
}
 
Example #10
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitMap_addColumnsUsingBracketNotation()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "add_columns:",
            "  - {name: \"$['json1']['k3']['k3']\", type: string, default: v}",
            "  - {name: \"$['json1']['k4']\", src: \"$['json1']['k2']\"}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{\"k1\":\"v\"},\"k2\":{\"k2\":\"v\"},\"k3\":{\"k3\":\"v\"},\"k4\":{\"k2\":\"v\"}}", visited.toString());
}
 
Example #11
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitMap_dropColumnsUsingBracketNotation()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "drop_columns:",
            "  - {name: \"$['json1']['k1']['k1']\"}",
            "  - {name: \"$['json1']['k2']\"}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{}}", visited.toString());
}
 
Example #12
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 6 votes vote down vote up
@Test
public void visitMap_DropColumns()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "drop_columns:",
            "  - {name: $.json1.k1.k1}",
            "  - {name: $.json1.k2}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{}}", visited.toString());
}
 
Example #13
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 5 votes vote down vote up
@Test
public void visitArray_Columns()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            "  - {name: \"$.json1.k1[1]\", src: \"$.json1.k1[0]\"}",
            "  - {name: \"$.json1.k2[0]\"}",
            "  - {name: \"$.json1.k3[*].k1\"}",
            "  - {name: \"$.json1.k3[*].k3\", src: \"$.json1.k3[*].k1\"}",
            "  - {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"},"v"],"k2":["v","v"],"k3":[{"k1":"v","k2":"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), v),
            k2, ValueFactory.newArray(v, v),
            k3, ValueFactory.newArray(ValueFactory.newMap(k1, v, k2, v)));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":[{\"k1\":\"v\"}],\"k2\":[\"v\"],\"k3\":[{\"k1\":\"v\",\"k3\":\"v\"}],\"k4\":[],\"k5\":[{\"k1\":\"v\"}]}", visited.toString());
}
 
Example #14
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 5 votes vote down vote up
@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());
}
 
Example #15
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 5 votes vote down vote up
@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());
}
 
Example #16
Source File: TestJsonVisitor.java    From embulk-filter-column with MIT License 5 votes vote down vote up
@Test
public void visitMap_Columns()
{
    PluginTask task = taskFromYamlString(
            "type: column",
            "columns:",
            "  - {name: $.json1.k1}",
            "  - {name: $.json1.k2.k2}",
            "  - {name: $.json1.k3.k3, type: string, default: v}",
            "  - {name: $.json1.k4, src: $.json1.k2}");
    Schema inputSchema = Schema.builder()
            .add("json1", JSON)
            .add("json2", JSON)
            .build();
    JsonVisitor subject = jsonVisitor(task, inputSchema);

    // {"k1":{"k1":"v"},"k2":{"k1":"v","k2":"v"}}
    Value k1 = ValueFactory.newString("k1");
    Value k2 = ValueFactory.newString("k2");
    Value v = ValueFactory.newString("v");
    Value map = ValueFactory.newMap(
            k1, ValueFactory.newMap(k1, v),
            k2, ValueFactory.newMap(k2, v));

    MapValue visited = subject.visit("$['json1']", map).asMapValue();
    assertEquals("{\"k1\":{\"k1\":\"v\"},\"k2\":{\"k2\":\"v\"},\"k3\":{\"k3\":\"v\"},\"k4\":{\"k2\":\"v\"}}", visited.toString());
}
 
Example #17
Source File: TdOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example #18
Source File: MessagePackDeserializer.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private Map<Object, Object> deserializeMap( ModuleDescriptor module, MapType mapType, MapValue value )
    throws IOException
{
    Map<Object, Object> map = new LinkedHashMap<>( value.size() );
    for( Map.Entry<Value, Value> entry : value.entrySet() )
    {
        Object key = doDeserialize( module, mapType.keyType(), entry.getKey() );
        Object val = doDeserialize( module, mapType.valueType(), entry.getValue() );
        map.put( key, val );
    }
    return map;
}
 
Example #19
Source File: MessagePackDeserializer.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private Object deserializeStatefulAssociationValue( ModuleDescriptor module,
                                                    StatefulAssociationValueType<?> valueType,
                                                    MapValue value ) throws IOException
{
    Map<String, Value> namedValues = value.map().entrySet().stream().map(
        entry ->
        {
            String key = doDeserialize( module, ValueType.STRING, entry.getKey() );
            return new AbstractMap.SimpleImmutableEntry<>( key, entry.getValue() );
        }
    ).collect( toMap( HashMap::new ) );

    String typeInfo = null;
    if( namedValues.containsKey( "_type" ) )
    {
        typeInfo = doDeserialize( module, ValueType.STRING, namedValues.get( "_type" ) );
    }
    if( typeInfo != null )
    {
        // TODO What to do with typeInfo? Value or Entity ?
        StatefulAssociationCompositeDescriptor descriptor = statefulCompositeDescriptorFor( module, typeInfo );
        if( descriptor == null )
        {
            throw new SerializationException(
                "_type: " + typeInfo + " could not be resolved while deserializing " + value );
        }
        valueType = descriptor.valueType();
    }

    ValueBuilder builder = module.instance().newValueBuilderWithState(
        valueType.primaryType(),
        propertyFunction( valueType.module(), namedValues ),
        associationFunction( valueType.module(), namedValues ),
        manyAssociationFunction( valueType.module(), namedValues ),
        namedAssociationFunction( valueType.module(), namedValues ) );
    return builder.newInstance();
}
 
Example #20
Source File: MessagePackDeserializer.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private Object doGuessDeserialize( ModuleDescriptor module, ValueType valueType, Value value )
    throws IOException, ClassNotFoundException
{
    switch( value.getValueType() )
    {
        case MAP:
            MapValue mapValue = value.asMapValue();
            Optional<String> typeInfo = mapValue
                .entrySet().stream()
                .filter( entry -> entry.getKey().isStringValue() )
                .map( entry ->
                      {
                          String key = doDeserialize( module, ValueType.STRING, entry.getKey() );
                          return new AbstractMap.SimpleImmutableEntry<>( key, entry.getValue() );
                      } )
                .filter( entry -> "_type".equals( entry.getKey() ) )
                .findFirst()
                .map( entry -> doDeserialize( module, ValueType.STRING, entry.getValue() ) );
            if( typeInfo.isPresent() )
            {
                StatefulAssociationCompositeDescriptor descriptor = statefulCompositeDescriptorFor(
                    module, typeInfo.get() );
                if( descriptor != null )
                {
                    return deserializeStatefulAssociationValue( ( (CompositeDescriptor) descriptor ).module(),
                                                                descriptor.valueType(),
                                                                mapValue );
                }
            }
        default:
            throw new SerializationException( "Don't know how to deserialize " + valueType + " from " + value
                                              + " (" + value.getValueType() + ")" );
    }
}
 
Example #21
Source File: MessagePackSerializer.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private MapValue serializeMap( Options options, Map<?, ?> map )
{
    ValueFactory.MapBuilder builder = ValueFactory.newMapBuilder();
    map.forEach( ( key, value ) -> builder.put( doSerialize( options, key, false ),
                                                doSerialize( options, value, false ) ) );
    return builder.build();
}
 
Example #22
Source File: MessagePackSerializer.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private MapValue serializeStatefulAssociationValue( Options options, Object composite, boolean root )
{
    CompositeInstance instance = PolygeneAPI.FUNCTION_COMPOSITE_INSTANCE_OF.apply( (Composite) composite );
    StatefulAssociationCompositeDescriptor descriptor =
        (StatefulAssociationCompositeDescriptor) instance.descriptor();
    AssociationStateHolder state = (AssociationStateHolder) instance.state();
    StatefulAssociationValueType<?> valueType = descriptor.valueType();

    ValueFactory.MapBuilder builder = ValueFactory.newMapBuilder();
    valueType.properties().forEach(
        property ->
        {
            Object value = state.propertyFor( property.accessor() ).get();
            Converter<Object> converter = converters.converterFor( property );
            if( converter != null )
            {
                value = converter.toString( value );
            }
            builder.put(
                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() ),
                         false ) ) );
    valueType.namedAssociations().forEach(
        association -> builder.put(
            ValueFactory.newString( association.qualifiedName().name() ),
            doSerialize( options,
                         state.namedAssociationFor( association.accessor() ).references().collect( toMap() ),
                         false ) ) );

    if( ( root && options.rootTypeInfo() ) || ( !root && options.nestedTypeInfo() ) )
    {
        builder.put( ValueFactory.newString( "_type" ),
                     ValueFactory.newString( valueType.primaryType().getName() ) );
    }
    return builder.build();
}
 
Example #23
Source File: FluencyTestWithMockServer.java    From fluency with Apache License 2.0 4 votes vote down vote up
@Override
protected EventHandler getFluentdEventHandler()
{
    return new EventHandler()
    {
        @Override
        public void onConnect(Socket acceptSocket)
        {
            connectCounter.incrementAndGet();
        }

        @Override
        public void onReceive(String tag, long timestampMillis, MapValue data)
        {
            if (tag.equals("foodb0.bartbl0")) {
                tag0EventsCounter.incrementAndGet();
            }
            else if (tag.equals("foodb1.bartbl1")) {
                tag1EventsCounter.incrementAndGet();
            }
            else if (tag.equals("foodb2.bartbl2")) {
                tag2EventsCounter.incrementAndGet();
            }
            else if (tag.equals("foodb3.bartbl3")) {
                tag3EventsCounter.incrementAndGet();
            }
            else {
                throw new IllegalArgumentException("Unexpected tag: tag=" + tag);
            }
            assertTrue(startTimestampMillis / 1000 <= timestampMillis / 1000 && timestampMillis < startTimestampMillis + 60 * 1000);

            assertEquals(4, data.size());
            for (Map.Entry<Value, Value> kv : data.entrySet()) {
                String key = kv.getKey().asStringValue().toString();
                Value val = kv.getValue();
                if (key.equals("comment")) {
                    assertEquals("hello, world", val.toString());
                }
                else if (key.equals("rate")) {
                    assertEquals(1.23, val.asFloatValue().toFloat(), 0.000001);
                }
                else if (key.equals("name")) {
                    nameEventsCounter.incrementAndGet();
                    nameEventsLength.addAndGet(val.asRawValue().asString().length());
                }
                else if (key.equals("age")) {
                    ageEventsCounter.incrementAndGet();
                    ageEventsSum.addAndGet(val.asIntegerValue().asInt());
                }
            }
        }

        @Override
        public void onClose(Socket accpetSocket)
        {
            closeCounter.incrementAndGet();
        }
    };
}
 
Example #24
Source File: AbstractFluentdServer.java    From fluency with Apache License 2.0 votes vote down vote up
void onReceive(String tag, long timestampMillis, MapValue data);