use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CommonOverridesFormatter method appendAsString.
private void appendAsString(JMessage<?> message) {
writer.appendln("@Override").appendln(JAnnotation.NON_NULL).appendln("public String asString() {").begin().appendln("StringBuilder out = new StringBuilder();").appendln("out.append(\"{\");").newline();
if (message.isUnion()) {
writer.formatln("switch (%s) {", UNION_FIELD).begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %s: {", caseFieldConstant(field)).begin().formatln("out.append(\"%s:\")", field.name());
switch(field.type()) {
case VOID:
writer.formatln(" .append(\"true\");");
break;
case BOOL:
case I32:
case I64:
writer.formatln(" .append(%s);", field.member());
break;
case BYTE:
case I16:
writer.formatln(" .append((int) %s);", field.member());
break;
case DOUBLE:
case SET:
case LIST:
case MAP:
writer.formatln(" .append(%s.asString(%s));", Strings.class.getName(), field.member());
break;
case STRING:
writer.formatln(" .append('\\\"').append(%s.escape(%s)).append('\\\"');", Strings.class.getName(), field.member());
break;
case BINARY:
writer.formatln(" .append(\"b64(\").append(%s.toBase64()).append(')');", field.member());
break;
case MESSAGE:
case ENUM:
writer.formatln(" .append(%s.asString());", field.member());
break;
default:
writer.formatln(" .append(%s.toString());", field.member());
break;
}
writer.appendln("break;").end().appendln('}');
}
writer.end().appendln('}');
} else {
boolean hasFirstFlag = false;
boolean allowSeparator = false;
boolean requireSeparator = false;
List<JField> fieldList = message.declaredOrderFields();
JField[] fields = fieldList.toArray(new JField[fieldList.size()]);
for (int i = 0; i < fields.length; ++i) {
JField field = fields[i];
boolean last = (i == (fields.length - 1));
if (!field.alwaysPresent()) {
if (!last && !requireSeparator && !hasFirstFlag) {
writer.appendln("boolean first = true;");
hasFirstFlag = true;
}
writer.formatln("if (%s()) {", field.presence());
writer.begin();
}
if (requireSeparator) {
writer.appendln("out.append(',');");
} else {
if (allowSeparator) {
if (hasFirstFlag) {
if (last || field.alwaysPresent()) {
writer.appendln("if (!first) out.append(',');");
} else {
writer.appendln("if (first) first = false;").appendln("else out.append(',');");
}
}
} else if (hasFirstFlag) {
writer.appendln("first = false;");
}
}
writer.formatln("out.append(\"%s:\")", field.name());
switch(field.type()) {
case BOOL:
case I32:
case I64:
writer.formatln(" .append(%s);", field.member());
break;
case BYTE:
case I16:
writer.formatln(" .append((int) %s);", field.member());
break;
case DOUBLE:
case MAP:
case SET:
case LIST:
writer.formatln(" .append(%s.asString(%s));", Strings.class.getName(), field.member());
break;
case STRING:
writer.appendln(" .append('\\\"')").formatln(" .append(%s.escape(%s))", Strings.class.getName(), field.member()).appendln(" .append('\\\"');");
break;
case BINARY:
writer.appendln(" .append(\"b64(\")").formatln(" .append(%s.toBase64())", field.member()).appendln(" .append(')');");
break;
case MESSAGE:
case ENUM:
writer.formatln(" .append(%s.asString());", field.member());
break;
default:
writer.formatln(" .append(%s.toString());", field.member());
break;
}
if (field.alwaysPresent()) {
requireSeparator = true;
} else {
writer.end().appendln('}');
allowSeparator = true;
}
}
}
writer.appendln("out.append('}');").appendln("return out.toString();").end().appendln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CoreOverridesFormatter method appendPresence.
private void appendPresence(JMessage<?> message) {
writer.appendln("@Override").appendln("public boolean has(int key) {").begin().appendln("switch(key) {").begin();
if (message.isUnion()) {
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %s: return %s == _Field.%s;", field.id(), UNION_FIELD, field.fieldEnum());
}
} else {
for (JField field : message.numericalOrderFields()) {
if (field.alwaysPresent()) {
writer.formatln("case %d: return true;", field.id());
} else {
writer.formatln("case %d: return %s != null;", field.id(), field.member());
}
}
}
writer.appendln("default: return false;").end().appendln('}').end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BinaryReaderBuilderFormatter method appendMethods.
@Override
public void appendMethods(JMessage<?> message) {
writer.appendln("@Override").formatln("public void readBinary(%s reader, boolean strict) throws %s {", BigEndianBinaryReader.class.getName(), IOException.class.getName()).begin();
writer.appendln("byte type = reader.expectByte();").formatln("while (type != %d) {", BinaryType.STOP).begin();
writer.appendln("int field = reader.expectShort();").appendln("switch (field) {").begin();
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %d: {", field.id()).begin();
writer.formatln("if (type == %d) {", forType(field.type())).begin();
appendReadFieldValue(field.member(), message, field, field.field().getDescriptor());
if (message.isUnion()) {
writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
} else {
writer.formatln("optionals.set(%d);", field.index());
}
// if type
writer.end().appendln("} else {").formatln(" throw new %s(\"Wrong type \" + %s.asString(type) + \" for %s.%s, should be %s\");", SerializerException.class.getName(), BinaryType.class.getName(), message.descriptor().getQualifiedName(), field.name(), BinaryType.asString(forType(message.descriptor().getType()))).appendln('}');
writer.appendln("break;").end().appendln('}');
}
writer.appendln("default: {").formatln(" %s.readFieldValue(reader, new %s(field, type), null, false);", BinaryFormatUtils.class.getName(), BinaryFormatUtils.FieldInfo.class.getName().replaceAll("\\$", ".")).appendln(" break;").appendln('}');
// switch
writer.end().appendln('}');
writer.appendln("type = reader.expectByte();").end().appendln("}");
// readBinary
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class JacksonMessageFormatter method appendJacksonDeserializer.
private void appendJacksonDeserializer(JMessage<?> message) throws GeneratorException {
writer.formatln("public static class _Deserializer extends %s<%s> {", JsonDeserializer.class.getName(), message.instanceType()).begin();
writer.appendln("@Override").formatln("public %s deserialize(%s jp,", message.instanceType(), JsonParser.class.getName()).formatln(" %s %s ctxt)", message.instanceType().replaceAll("[\\S]", " "), DeserializationContext.class.getName()).formatln(" throws %s,", IOException.class.getName()).formatln(" %s {", JsonProcessingException.class.getName()).begin();
writer.appendln("_Builder builder = builder();").newline();
writer.formatln("if (jp.isExpectedStartObjectToken()) {").begin().formatln("while (jp.nextToken() != %s.END_OBJECT) {", JsonToken.class.getName()).begin().formatln("if (jp.getCurrentToken() != %s.FIELD_NAME) {", JsonToken.class.getName()).formatln(" throw new %s(jp, \"Invalid field name token \" + jp.getText());", JsonParseException.class.getName()).appendln('}').newline().appendln("String field = jp.getCurrentName();").appendln("jp.nextToken();").appendln("switch (field) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case \"%d\":", field.id()).formatln("case \"%s\": {", field.name()).begin();
appendReadValue(field);
writer.appendln("break;").end().appendln('}');
}
writer.end().appendln("}").end().appendln("}");
if (message.jsonCompactible()) {
writer.end().appendln("} else if (jp.isExpectedStartArrayToken()) {").begin();
writer.appendln("int idx = 0;").formatln("while (jp.nextToken() != %s.END_ARRAY) {", JsonToken.class.getName()).begin().appendln("switch (idx++) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %d: {", field.index()).begin();
appendReadValue(field);
writer.appendln("break;").end().appendln('}');
}
writer.appendln("default:").formatln(" throw new %s(jp, \"Unexpected value: \" + jp.getText());", JsonParseException.class.getName()).end().appendln('}').end().appendln('}');
}
writer.end().appendln("} else {").formatln(" throw new %s(jp, \"Invalid token for object deserialization \" + jp.getText());", JsonParseException.class.getName()).appendln('}').newline().appendln("return builder.build();");
writer.end().formatln("}").end().formatln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class JacksonMessageFormatter method appendJacksonSerializer.
private void appendJacksonSerializer(JMessage<?> message) throws GeneratorException {
writer.formatln("public static class _Serializer extends %s<%s> {", JsonSerializer.class.getName(), message.instanceType()).begin().appendln("@Override").formatln("public void serialize(%s instance, %s generator, %s provider)", message.instanceType(), JsonGenerator.class.getName(), SerializerProvider.class.getName()).formatln(" throws %s, %s {", IOException.class.getName(), JsonProcessingException.class.getName()).begin();
if (message.isUnion()) {
writer.appendln("generator.writeStartObject();").formatln("switch (instance.%s) {", UNION_FIELD).begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %s: {", field.fieldEnum()).begin();
appendWriteValue(field);
writer.appendln("break;").end().appendln('}');
}
writer.end().appendln('}').appendln("generator.writeEndObject();");
} else {
if (message.jsonCompactible()) {
writer.formatln("if (instance.jsonCompact()) {").begin().formatln("generator.writeStartArray();");
int ifStack = 0;
for (JField field : message.numericalOrderFields()) {
if (!(field.alwaysPresent() || field.isRequired())) {
writer.formatln("if (instance.%s != null) {", field.member()).begin();
++ifStack;
}
writer.formatln("provider.defaultSerializeValue(instance.%s, generator);", field.member());
}
while (ifStack-- > 0) {
writer.end().appendln('}');
}
writer.formatln("generator.writeEndArray();");
writer.end().appendln("} else {").begin();
}
writer.formatln("generator.writeStartObject();");
for (JField field : message.numericalOrderFields()) {
if (!(field.alwaysPresent() || field.isRequired())) {
writer.formatln("if (instance.%s != null) {", field.member()).begin();
}
appendWriteValue(field);
if (!(field.alwaysPresent() || field.isRequired())) {
writer.end().appendln('}');
}
}
writer.appendln("generator.writeEndObject();");
if (message.jsonCompactible()) {
writer.end().appendln('}');
}
}
writer.end().formatln(// end serialize()
"}").end().formatln(// end _Serializer
"}").newline();
}
Aggregations