You can use this class from the metastore. A lot of work has pored into this class. The plan is to actually donate this to the proto-java implementation if it has been battle tested.
https://github.com/anemos-io/metastore/blob/master/putils/src/main/java/io/anemos/metastore/putils/ProtoLanguageFileWriter.java It currently only support proto3. _/ _/ Alex Van Boxel On Fri, Feb 21, 2020 at 11:02 AM Nadav Samet <[email protected]> wrote: > Hi Ittai, > > It sounds like you are expecting your plugin to emit proto files in text > format. Your code is assigning binary data into the file's content, and > what protoc does is just writing it to the files it creates. There's > nothing that would automatically detect that you are passing a > DescriptorProto and would transform that into text representation. If you > want the output to be in text format you need to manually create a string > with the file content you want. > > > On Fri, Feb 21, 2020 at 11:40 AM ittai zeidman <[email protected]> wrote: > >> Hi, >> I have proto files which contain valid proto messages and services and my >> own DSL which I want to transpile into proto messages and services. >> My idea is to invoke protoc and have it process "my proto" with a protoc >> plugin that will emmit "standard proto" (which I'll later feed into protoc >> again). >> The reason I'm going through the intermediate proto files is because we need >> them to integrate with other systems. >> >> I'm trying to write this plugin using protobuf-java (jvm developer) but the >> proto file that is emitted is incorrect. >> >> I think (and hope) I'm doing something stupid and maybe someone here can >> point me to the right direction. >> >> >> Thanks in advance... >> >> >> Some of my code (to generate a hardcoded proto file with one message and >> one field): >> >> DescriptorProtos.DescriptorProto protoMessage = >> DescriptorProtos.DescriptorProto.newBuilder() >> .setName("messageGreeting") >> .addField(DescriptorProtos.FieldDescriptorProto.newBuilder() >> .setName("greeting") >> .setType(FieldDescriptorProto.Type.TYPE_STRING) >> .setNumber(0) >> .setDefaultValue("hi") >> .build() >> ) >> .build(); >> DescriptorProtos.FileDescriptorProto proto = >> DescriptorProtos.FileDescriptorProto.newBuilder() >> .addMessageType( >> protoMessage >> ) >> .build(); >> ByteArrayOutputStream byteArrayOutputStream = new >> ByteArrayOutputStream(); >> >> proto.writeTo(byteArrayOutputStream); // should be lazy? >> >> CodeGeneratorResponse.File file = CodeGeneratorResponse.File.newBuilder() >> .setName("yo.proto") >> >> .setContentBytes(ByteString.copyFrom(byteArrayOutputStream.toByteArray)) >> >> // I also tried: >> >> // .mergeFrom(protoMessage) >> // .setContent(byteArrayOutputStream.toString) // should be lazy? >> // .mergeFrom(byteArrayOutputStream.toByteArray) >> .build(); >> file.toByteArray >> >> >> Unfortunately what I get is: >> >> cat yo.proto: >> "% >> messageGreeting >> >> >> greeting( :hi% >> >> and in a text editor: >> 2225 0a0f 6d65 7373 6167 6547 7265 6574 >> 696e 6712 120a 0867 7265 6574 696e 6718 >> 0028 093a 0268 69 >> >> -- >> 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 [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/protobuf/03f1f05d-a9ef-4490-bf6a-469f16212f9f%40googlegroups.com >> <https://groups.google.com/d/msgid/protobuf/03f1f05d-a9ef-4490-bf6a-469f16212f9f%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > -Nadav > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/protobuf/CANZcNEryh5FBtZQ%3D4_EZuy5N21JeMxJ6NTjhR2T68DM5s9OWtw%40mail.gmail.com > <https://groups.google.com/d/msgid/protobuf/CANZcNEryh5FBtZQ%3D4_EZuy5N21JeMxJ6NTjhR2T68DM5s9OWtw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/CALCMntvYb%2BO4wVH6bYnQGYDrTTzZP-HJEN9an99VJs91bbhQWQ%40mail.gmail.com.
