syntax = "proto3";
option java_multiple_files = true;package proto3spike.dtos;
message EnvelopeDto { string operationName = 1; AnyDto payload = 2;}
message AnyDto { string canonicalName = 1; bytes value = 2;}package proto3spike;
import com.google.protobuf.InvalidProtocolBufferException;import com.google.protobuf.Message;import com.google.protobuf.MessageLite;import proto3spike.dtos.AnyDto;
public class AnyDtoMapper {
public static AnyDto mapToAnyDto(Message payload) throws InvalidProtocolBufferException { String canonicalName = payload.getClass().getCanonicalName(); return AnyDto.newBuilder() .setCanonicalName(canonicalName) .setValue(payload.toByteString()) .build(); }
public static Message mapFromAnyDto(AnyDto dto) throws Exception { Class c = Class.forName(dto.getCanonicalName()); MessageLite defaultInstance = com.google.protobuf.Internal.getDefaultInstance(c); return (Message)defaultInstance .getParserForType() .parseFrom(dto.getValue()); }}/** * @fileoverview * @enhanceable * @public */// GENERATED CODE -- DO NOT EDIT!
var jspb = require('google-protobuf');var goog = jspb;var global = Function('return this')();
goog.exportSymbol('proto.proto3spike.dtos.AnyDto', null, global);goog.exportSymbol('proto.proto3spike.dtos.EnvelopeDto', null, global);
/** * Generated by JsPbCodeGenerator. * @param {Array=} opt_data Optional initial data array, typically from a * server response, or constructed directly in Javascript. The array is used * in place and becomes part of the constructed object. It is not cloned. * If no data is provided, the constructed object will be empty, but still * valid. * @extends {jspb.Message} * @constructor */proto.proto3spike.dtos.EnvelopeDto = function(opt_data) { jspb.Message.initialize(this, opt_data, 0, -1, null, null);};goog.inherits(proto.proto3spike.dtos.EnvelopeDto, jspb.Message);if (goog.DEBUG && !COMPILED) { proto.proto3spike.dtos.EnvelopeDto.displayName = 'proto.proto3spike.dtos.EnvelopeDto';}
// ... rest of the file ...--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Thanks for the reply. I agree, mapping from a generated class name isn't a good idea. However just re-reading my initial post, while I mentioned my use of the canonical name of the java Message, in fact this is really just the proto package name. It would be nice if metadata for the package name (as defined by the 'package' keyword) was available on both the Java side and the generated JS proto messages to support more dynamic use cases. To some degree it is available as the package name corresponds to the java class's canonical name, and on the JS side it corresponds to the namespace the messages exists on the global proto object. It would be nice if this was more formal (for example each message having typing information/metadata).