use of net.morimekta.providence.descriptor.PContainer in project providence by morimekta.
the class FastBinarySerializer method writeContainerEntry.
@SuppressWarnings("unchecked")
private int writeContainerEntry(LittleEndianBinaryWriter out, int typeid, PDescriptor descriptor, Object value) throws IOException {
switch(typeid) {
case VARINT:
{
if (value instanceof Boolean) {
return out.writeVarint(((Boolean) value ? 1 : 0));
} else if (value instanceof Number) {
return out.writeZigzag(((Number) value).longValue());
} else if (value instanceof PEnumValue) {
return out.writeZigzag(((PEnumValue) value).asInteger());
} else {
throw new SerializerException("");
}
}
case FIXED_64:
{
return out.writeDouble((Double) value);
}
case BINARY:
{
if (value instanceof CharSequence) {
byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8);
int len = out.writeVarint(bytes.length);
out.write(bytes);
return len + bytes.length;
} else if (value instanceof Binary) {
Binary bytes = (Binary) value;
int len = out.writeVarint(bytes.length());
bytes.write(out);
return len + bytes.length();
} else {
throw new SerializerException("");
}
}
case MESSAGE:
{
return writeMessage(out, (PMessage) value);
}
case COLLECTION:
{
if (value instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) value;
PMap<?, ?> desc = (PMap<?, ?>) descriptor;
int ktype = itemType(desc.keyDescriptor());
int vtype = itemType(desc.itemDescriptor());
int len = out.writeVarint(map.size() * 2);
len += out.writeVarint(ktype << 3 | vtype);
for (Map.Entry<Object, Object> entry : map.entrySet()) {
len += writeContainerEntry(out, ktype, desc.keyDescriptor(), entry.getKey());
len += writeContainerEntry(out, vtype, desc.itemDescriptor(), entry.getValue());
}
return len;
} else if (value instanceof Collection) {
Collection<Object> coll = (Collection<Object>) value;
PContainer<?> desc = (PContainer<?>) descriptor;
int vtype = itemType(desc.itemDescriptor());
int len = out.writeVarint(coll.size());
len += out.writeVarint(vtype);
for (Object item : coll) {
len += writeContainerEntry(out, vtype, desc.itemDescriptor(), item);
}
return len;
} else {
throw new SerializerException("");
}
}
default:
throw new SerializerException("");
}
}
use of net.morimekta.providence.descriptor.PContainer in project providence by morimekta.
the class BuilderCoreOverridesFormatter method appendOverrideAdder.
private void appendOverrideAdder(JMessage<?> message) throws GeneratorException {
writer.appendln("@Override").appendln("public _Builder addTo(int key, Object value) {").begin().appendln("switch (key) {").begin();
message.numericalOrderFields().stream().filter(field -> field.type() == PType.LIST || field.type() == PType.SET).forEachOrdered(field -> {
PContainer<?> ct = (PContainer<?>) field.field().getDescriptor();
PDescriptor itype = ct.itemDescriptor();
writer.formatln("case %d: %s((%s) value); break;", field.id(), field.adder(), helper.getValueType(itype));
});
writer.appendln("default: break;").end().appendln('}').appendln("return this;").end().appendln('}').newline();
}
use of net.morimekta.providence.descriptor.PContainer in project providence by morimekta.
the class BuilderCommonMemberFormatter method appendAdder.
private void appendAdder(JMessage message, JField field) throws GeneratorException {
BlockCommentBuilder comment = new BlockCommentBuilder(writer);
if (field.hasComment()) {
comment.comment(field.comment());
} else if (field.type() == PType.MAP) {
comment.comment("Adds a mapping to " + field.name() + ".");
} else {
comment.comment("Adds entries to " + field.name() + ".");
}
comment.newline();
if (field.type() == PType.MAP) {
comment.param_("key", "The inserted key").param_("value", "The inserted value");
} else {
comment.param_("values", "The added value");
}
comment.return_("The builder");
if (JAnnotation.isDeprecated(field)) {
String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
if (reason != null && reason.trim().length() > 0) {
comment.deprecated_(reason);
}
}
comment.finish();
if (JAnnotation.isDeprecated(field)) {
writer.appendln(JAnnotation.DEPRECATED);
}
writer.appendln(JAnnotation.NON_NULL);
switch(field.type()) {
case MAP:
{
PMap<?, ?> mType = (PMap<?, ?>) field.field().getDescriptor();
String mkType = helper.getValueType(mType.keyDescriptor());
String miType = helper.getValueType(mType.itemDescriptor());
writer.formatln("public _Builder %s(%s key, %s value) {", field.adder(), mkType, miType).begin();
if (message.isUnion()) {
writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
writer.appendln("modified = true;");
} else {
writer.formatln("optionals.set(%d);", field.index());
writer.formatln("modified.set(%d);", field.index());
}
writer.formatln("%s().put(key, value);", field.mutable()).appendln("return this;").end().appendln('}').newline();
break;
}
case SET:
case LIST:
{
PContainer<?> lType = (PContainer<?>) field.field().getDescriptor();
String liType = helper.getValueType(lType.itemDescriptor());
writer.formatln("public _Builder %s(%s... values) {", field.adder(), liType).begin();
if (message.isUnion()) {
writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
writer.appendln("modified = true;");
} else {
writer.formatln("optionals.set(%d);", field.index());
writer.formatln("modified.set(%d);", field.index());
}
writer.formatln("%s _container = %s();", field.fieldType(), field.mutable()).formatln("for (%s item : values) {", liType).begin().appendln("_container.add(item);").end().appendln('}').appendln("return this;").end().appendln('}').newline();
break;
}
}
}
use of net.morimekta.providence.descriptor.PContainer in project providence by morimekta.
the class BuilderCommonMemberFormatter method appendSetter.
private void appendSetter(JMessage message, JField field) throws GeneratorException {
BlockCommentBuilder comment = new BlockCommentBuilder(writer);
if (field.hasComment()) {
comment.comment(field.comment());
} else {
comment.comment("Sets the value of " + field.name() + ".");
}
comment.newline();
if (!field.isVoid()) {
comment.param_("value", "The new value");
}
comment.return_("The builder");
if (JAnnotation.isDeprecated(field)) {
String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
if (reason != null && reason.trim().length() > 0) {
comment.deprecated_(reason);
}
}
comment.finish();
if (JAnnotation.isDeprecated(field)) {
writer.appendln(JAnnotation.DEPRECATED);
}
writer.appendln(JAnnotation.NON_NULL);
if (field.isVoid()) {
// Void fields have no value.
writer.formatln("public _Builder %s() {", field.setter());
} else if (field.type() == PType.SET || field.type() == PType.LIST) {
PContainer<?> cType = (PContainer<?>) field.field().getDescriptor();
String iType = helper.getFieldType(cType.itemDescriptor());
writer.formatln("public _Builder %s(%s<%s> value) {", field.setter(), Collection.class.getName(), iType);
} else {
writer.formatln("public _Builder %s(%s value) {", field.setter(), field.valueType());
}
writer.begin();
if (!field.isPrimitiveJavaValue()) {
writer.formatln("if (value == null) {").formatln(" return %s();", field.resetter()).appendln('}').newline();
}
if (message.isUnion()) {
writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
writer.appendln("modified = true;");
} else {
writer.formatln("optionals.set(%d);", field.index());
writer.formatln("modified.set(%d);", field.index());
}
switch(field.type()) {
case VOID:
// Void fields have no value.
break;
case SET:
case LIST:
case MAP:
writer.formatln("%s = %s;", field.member(), field.fieldInstanceCopy("value"));
break;
case MESSAGE:
writer.formatln("%s = value;", field.member());
writer.formatln("%s_builder = null;", field.member());
break;
default:
writer.formatln("%s = value;", field.member());
break;
}
writer.appendln("return this;").end().appendln('}').newline();
if (field.type() == MESSAGE) {
appendSetterBuilderOverload(field);
}
}
use of net.morimekta.providence.descriptor.PContainer in project providence by morimekta.
the class JacksonMessageFormatter method appendReadValue.
private void appendReadValue(JField field) throws GeneratorException {
switch(field.type()) {
case MAP:
{
PMap mType = (PMap) field.field().getDescriptor();
PDescriptor kType = mType.keyDescriptor();
writer.formatln("%s kType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(kType));
PDescriptor iType = mType.itemDescriptor();
if (iType instanceof PMap) {
PMap imType = (PMap) iType;
PDescriptor ikType = imType.keyDescriptor();
PDescriptor iiType = imType.itemDescriptor();
if (iiType instanceof PContainer) {
throw new GeneratorException("Too many levels of containers: " + field.toString());
}
// double level of container...
writer.formatln("%s iType = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), HashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
} else if (iType instanceof PContainer) {
PContainer icType = (PContainer) iType;
PDescriptor iiType = icType.itemDescriptor();
if (iiType instanceof PContainer) {
throw new GeneratorException("Too many levels of containers: " + field.toString());
}
// double level of container...
writer.formatln("%s iType = ctxt.getTypeFactory().constructArrayType(%s.class);", MapType.class.getName(), helper.getFieldType(iiType));
} else {
writer.formatln("%s iType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(iType));
}
writer.formatln("%s type = ctxt.getTypeFactory().constructMapType(%s.class, kType, iType);", MapType.class.getName(), HashMap.class.getName());
writer.formatln("builder.%s(ctxt.readValue(jp, type));", field.setter());
break;
}
case SET:
case LIST:
{
PContainer cType = (PContainer) field.field().getDescriptor();
PDescriptor iType = cType.itemDescriptor();
if (iType instanceof PMap) {
PMap imType = (PMap) iType;
PDescriptor ikType = imType.keyDescriptor();
PDescriptor iiType = imType.itemDescriptor();
// double level of container...
writer.formatln("%s itype = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), LinkedHashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
} else if (iType instanceof PContainer) {
PContainer icType = (PContainer) iType;
PDescriptor iiType = icType.itemDescriptor();
// double level of container...
writer.formatln("%s itype = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iiType));
writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
} else {
writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iType));
writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
}
break;
}
case BINARY:
case STRING:
case MESSAGE:
case ENUM:
writer.formatln("builder.%s(ctxt.readValue(jp, %s.class));", field.setter(), field.instanceType());
break;
default:
writer.formatln("builder.%s(ctxt.readValue(jp, %s.TYPE));", field.setter(), field.instanceType());
break;
}
}
Aggregations