Search in sources :

Example 1 with FieldType

use of com.ociweb.gl.api.FieldType in project GreenLightning by oci-pronghorn.

the class GreenParserMessageTest method complexStringTest.

@Test
public void complexStringTest() {
    NumberFormat formatter = new DecimalFormat("#0.0000");
    final GreenReader reader = buildParser().newReader();
    ChannelReader testToRead = BlobReaderFactory.generateExtractionDataToTest(new MyConsumer<DataOutputBlobWriter<?>>() {

        @Override
        public void accept(DataOutputBlobWriter<?> dataOutputBlobWriter) {
            complexStreamAppend(dataOutputBlobWriter);
        }
    });
    reader.beginRead(testToRead);
    StringBuilder rebuild = new StringBuilder();
    while (reader.hasMore()) {
        int parsedId = (int) reader.readToken();
        if (parsedId == -1) {
            reader.skipByte();
        } else {
            final MsgField msgField = messages[parsedId];
            final FieldType fieldType = msgField.type;
            final String key = msgField.key;
            rebuild.append(key);
            switch(fieldType) {
                case integer:
                    {
                        int value = (int) reader.extractedLong(0);
                        rebuild.append(value);
                        break;
                    }
                case int64:
                    {
                        long value = reader.extractedLong(0);
                        rebuild.append(value);
                        break;
                    }
                case string:
                    {
                        StringBuilder value = new StringBuilder();
                        reader.copyExtractedUTF8ToAppendable(0, value);
                        rebuild.append("\"");
                        rebuild.append(value);
                        rebuild.append("\"");
                        break;
                    }
                case floatingPoint:
                    {
                        double value = reader.extractedDouble(0);
                        rebuild.append(formatter.format(value));
                        break;
                    }
            }
        }
    }
    assertEquals(complexData, rebuild.toString());
}
Also used : DecimalFormat(java.text.DecimalFormat) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) FieldType(com.ociweb.gl.api.FieldType) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Aggregations

FieldType (com.ociweb.gl.api.FieldType)1 ChannelReader (com.ociweb.pronghorn.pipe.ChannelReader)1 DataOutputBlobWriter (com.ociweb.pronghorn.pipe.DataOutputBlobWriter)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 Test (org.junit.Test)1