use of net.morimekta.providence.descriptor.PList in project providence by morimekta.
the class BinaryFormatUtils method readFieldValue.
/**
* Read a field value from stream.
*
* @param in The stream to consume.
* @param fieldInfo The field info about the content.
* @param fieldType The type to generate content for.
* @param strict If the field should be read strictly.
* @return The field value, or null if no type.
* @throws IOException If unable to read from stream or invalid field type.
*/
public static Object readFieldValue(BigEndianBinaryReader in, FieldInfo fieldInfo, PDescriptor fieldType, boolean strict) throws IOException {
if (fieldType != null && forType(fieldType.getType()) != fieldInfo.type) {
throw new SerializerException("Wrong field type for id=%d: expected %s, got %s", fieldInfo.id, asString(forType(fieldType.getType())), asString(fieldInfo.getType()));
}
switch(fieldInfo.type) {
case BinaryType.VOID:
return Boolean.TRUE;
case BinaryType.BOOL:
return in.expectByte() != 0;
case BinaryType.BYTE:
return in.expectByte();
case BinaryType.I16:
return in.expectShort();
case BinaryType.I32:
int val = in.expectInt();
if (fieldType != null && fieldType instanceof PEnumDescriptor) {
@SuppressWarnings("unchecked") PEnumBuilder builder = ((PEnumDescriptor<?>) fieldType).builder();
builder.setById(val);
return builder.build();
} else {
return val;
}
case BinaryType.I64:
return in.expectLong();
case BinaryType.DOUBLE:
return in.expectDouble();
case BinaryType.STRING:
int len = in.expectUInt32();
byte[] data = in.expectBytes(len);
if (fieldType != null && fieldType.getType() == PType.STRING) {
return new String(data, StandardCharsets.UTF_8);
} else {
return Binary.wrap(data);
}
case BinaryType.STRUCT:
{
if (fieldType == null) {
consumeMessage(in);
return null;
}
return readMessage(in, (PMessageDescriptor<?, ?>) fieldType, strict);
}
case BinaryType.MAP:
{
final byte keyT = in.expectByte();
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor keyType = null;
PDescriptor valueType = null;
PMap.Builder<Object, Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PMap<Object, Object> mapType = (PMap<Object, Object>) fieldType;
keyType = mapType.keyDescriptor();
valueType = mapType.itemDescriptor();
out = mapType.builder();
} else {
out = new PMap.DefaultBuilder<>();
}
FieldInfo keyInfo = new FieldInfo(1, keyT);
FieldInfo itemInfo = new FieldInfo(2, itemT);
for (int i = 0; i < size; ++i) {
Object key = readFieldValue(in, keyInfo, keyType, strict);
Object value = readFieldValue(in, itemInfo, valueType, strict);
if (key != null && value != null) {
out.put(key, value);
} else if (strict) {
if (key == null) {
throw new SerializerException("Null key in map");
} else {
throw new SerializerException("Null value in map");
}
}
}
return out.build();
}
case BinaryType.SET:
{
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor entryType = null;
PSet.Builder<Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PSet<Object> setType = (PSet<Object>) fieldType;
entryType = setType.itemDescriptor();
out = setType.builder();
} else {
out = new PSet.DefaultBuilder<>();
}
FieldInfo itemInfo = new FieldInfo(0, itemT);
for (int i = 0; i < size; ++i) {
Object value = readFieldValue(in, itemInfo, entryType, strict);
if (value != null) {
out.add(value);
} else if (strict) {
throw new SerializerException("Null value in set");
}
}
return out.build();
}
case BinaryType.LIST:
{
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor entryType = null;
PList.Builder<Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PList<Object> listType = (PList<Object>) fieldType;
entryType = listType.itemDescriptor();
out = listType.builder();
} else {
out = new PList.DefaultBuilder<>();
}
FieldInfo itemInfo = new FieldInfo(0, itemT);
for (int i = 0; i < size; ++i) {
Object value = readFieldValue(in, itemInfo, entryType, strict);
if (value != null) {
out.add(value);
} else if (strict) {
throw new SerializerException("Null value in list");
}
}
return out.build();
}
default:
throw new SerializerException("unknown data type: " + fieldInfo.getType());
}
}
use of net.morimekta.providence.descriptor.PList in project providence by morimekta.
the class BaseTypeRegistryTest method testGetProvider_list.
@Test
public void testGetProvider_list() {
PDescriptorProvider p1 = registry.getProvider("list<I>", "number", null);
assertThat(p1.descriptor().getType(), is(PType.LIST));
PList list = (PList) p1.descriptor();
assertThat(list.itemDescriptor(), is(Imaginary.kDescriptor));
}
use of net.morimekta.providence.descriptor.PList in project providence by morimekta.
the class JField method getProvider.
public String getProvider() throws GeneratorException {
String container = field.getAnnotationValue("container");
String containerProvider = "provider";
if (container != null) {
ThriftContainer containerType = ThriftContainer.forName(container);
switch(containerType) {
case DEFAULT:
containerProvider = "provider";
break;
case SORTED:
containerProvider = "sortedProvider";
break;
case ORDERED:
containerProvider = "orderedProvider";
break;
default:
break;
}
}
switch(field.getType()) {
case ENUM:
case MESSAGE:
return String.format(Locale.US, "%s.provider()", helper.getFieldType(field.getDescriptor()));
case LIST:
PList<?> lType = (PList<?>) field.getDescriptor();
return String.format(Locale.US, "%s.provider(%s)", PList.class.getName(), helper.getProviderName(lType.itemDescriptor()));
case SET:
PSet<?> sType = (PSet<?>) field.getDescriptor();
return String.format(Locale.US, "%s.%s(%s)", PSet.class.getName(), containerProvider, helper.getProviderName(sType.itemDescriptor()));
case MAP:
PMap<?, ?> mType = (PMap<?, ?>) field.getDescriptor();
return String.format(Locale.US, "%s.%s(%s,%s)", PMap.class.getName(), containerProvider, helper.getProviderName(mType.keyDescriptor()), helper.getProviderName(mType.itemDescriptor()));
default:
if (!(field.getDescriptor() instanceof PPrimitive)) {
throw new IllegalArgumentException("Unhandled type group " + field.getType());
}
return String.format(Locale.US, "%s.%s.provider()", PPrimitive.class.getName(), field.getDescriptor().getName().toUpperCase(Locale.US));
}
}
use of net.morimekta.providence.descriptor.PList in project providence by morimekta.
the class ProgramRegistryTest method testGetProvider_list.
@Test
public void testGetProvider_list() {
PDescriptorProvider p1 = registry.getProvider("list<I>", "number", null);
assertThat(p1.descriptor().getType(), is(PType.LIST));
PList list = (PList) p1.descriptor();
assertThat(list.itemDescriptor().getQualifiedName(), is(Imaginary.kDescriptor.getQualifiedName()));
}
Aggregations