use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method readMessage.
private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(TTupleProtocol protocol, PMessageDescriptor<Message, Field> descriptor) throws SerializerException, TException {
PMessageBuilder<Message, Field> builder = descriptor.builder();
if (descriptor.getVariant() == PMessageVariant.UNION) {
int fieldId = protocol.readI16();
PField fld = descriptor.findFieldById(fieldId);
if (fld != null) {
builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
} else {
throw new SerializerException("Unable to read unknown union field " + fieldId + " in " + descriptor.getQualifiedName());
}
} else {
PField[] fields = descriptor.getFields();
int numOptionals = countOptionals(fields);
BitSet optionals = null;
int optionalPos = 0;
for (PField fld : fields) {
if (fld.getRequirement() == PRequirement.REQUIRED) {
builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
} else {
if (optionals == null) {
optionals = protocol.readBitSet(numOptionals);
}
if (optionals.get(optionalPos)) {
builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
}
++optionalPos;
}
}
}
if (strict) {
try {
builder.validate();
} catch (IllegalStateException e) {
throw new SerializerException(e, e.getMessage());
}
}
return builder.build();
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method deserialize.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws SerializerException {
TMessage tm = null;
PServiceCallType type = null;
try {
TTransport transport = new TIOStreamTransport(input);
TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
tm = protocol.readMessageBegin();
type = PServiceCallType.findById(tm.type);
if (type == null) {
throw new SerializerException("Unknown call type for id " + tm.type);
} else if (type == PServiceCallType.EXCEPTION) {
PApplicationException exception = readMessage(protocol, PApplicationException.kDescriptor);
return new PServiceCall(tm.name, type, tm.seqid, exception);
}
PServiceMethod method = service.getMethod(tm.name);
if (method == null) {
throw new SerializerException("No such method " + tm.name + " on " + service.getQualifiedName());
}
PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
Message message = readMessage(protocol, descriptor);
protocol.readMessageEnd();
return new PServiceCall<>(tm.name, type, tm.seqid, message);
} catch (TTransportException e) {
throw new SerializerException(e, "Unable to serialize into transport protocol").setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
} catch (TException e) {
throw new SerializerException(e, "Transport exception in protocol").setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method deserialize.
@Nonnull
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> Message deserialize(@Nonnull InputStream input, @Nonnull PMessageDescriptor<Message, Field> descriptor) throws IOException {
try {
TTransport transport = new TIOStreamTransport(input);
TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
return readMessage(protocol, descriptor);
} catch (TTransportException e) {
throw new SerializerException(e, "Unable to serialize into transport protocol");
} catch (TException e) {
throw new SerializerException(e, "Transport exception in protocol");
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class FormatUtils method collectConfigIncludes.
public static void collectConfigIncludes(File rc, Map<String, File> includes) throws IOException {
if (!rc.exists()) {
return;
}
rc = rc.getCanonicalFile();
if (!rc.isFile()) {
throw new ProvidenceConfigException("Rc file is not a file " + rc.getPath());
}
try {
SimpleTypeRegistry registry = new SimpleTypeRegistry();
registry.registerRecursively(ProvidenceTools.kDescriptor);
ProvidenceConfig loader = new ProvidenceConfig(registry);
ProvidenceTools config = loader.getConfig(rc);
if (config.hasIncludes()) {
File basePath = rc.getParentFile();
if (config.hasIncludesBasePath()) {
String base = config.getIncludesBasePath();
if (base.charAt(0) == '~') {
base = System.getenv("HOME") + base.substring(1);
}
basePath = new File(base);
if (!basePath.exists() || !basePath.isDirectory()) {
throw new ProvidenceConfigException("Includes Base path in " + rc.getPath() + " is not a directory: " + basePath);
}
}
for (String path : config.getIncludes()) {
File include = new File(basePath, path);
collectIncludes(include, includes);
}
}
} catch (SerializerException e) {
System.err.println("Config error: " + e.getMessage());
System.err.println(e.asString());
System.err.println();
throw new ArgumentException(e, "Exception when parsing " + rc.getCanonicalFile());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class JsonGenerator method generate.
@Override
public void generate(ProgramTypeRegistry registry) throws IOException, GeneratorException {
ProgramType doc = registry.getProgramType();
if (doc.hasIncludes()) {
doc = doc.mutate().setIncludes(doc.getIncludes().stream().map(path -> path.replaceAll("(\\.thrift)$", ".json")).collect(Collectors.toList())).build();
}
OutputStream out = getFileManager().create(null, doc.getProgramName() + ".json");
try {
serializer.serialize(out, doc);
out.write('\n');
} catch (SerializerException e) {
throw new GeneratorException("Unable to serialize document.", e);
}
getFileManager().finalize(out);
}
Aggregations