kshchepanovskyi / protostuff-googlecode-exported

Automatically exported from code.google.com/p/protostuff
Apache License 2.0
0 stars 1 forks source link

.proto files that use "import" create non-compilable Java files #4

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create two .proto files: "Main.proto" and "Utils.proto". Have Main.proto
import Utils.proto and use one of its types.
2. Generate JSON files using Protostuff.
3. The resulting Java class MainJSON.java doesn't compile because it
attempts to use the Convertors defined in UtilsJSON.java, but it doesn't
import that class.

The full solution to this problem would be to automatically identify which
modules refer to other modules and add the appropriate "import" directive
in the Java files. That looks like it would be a nontrivial change because
Protostuff would have to look at all the modules as a whole, instead of one
at a time.

Instead, I have created a simpler fix that relies on the developer to
specify the dependencies explicitly. It's in the attached patch. This patch
does the following:

I added an option called "imports" to modules.properties. This option
contains the full names of the JSON classes that the module relies upon. In
the resulting Java files, this translated into "import" directives.

For example:

modules = utils,main

utils.fullClassname = com.example.foo.Utils
utils.outputPackage = com.example.foo.json
utils.outputDir = generated
utils.generator = json
utils.encoding = UTF-8

main.fullClassname = com.example.foo.Main
main.outputPackage = com.example.foo.json
main.outputDir = generated
main.generator = json
main.encoding = UTF-8
main.imports = com.example.foo.json.Utils

Original issue reported on code.google.com by oren.hur...@gmail.com on 23 Jan 2010 at 7:36

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by david.yu...@gmail.com on 6 Mar 2010 at 4:58

GoogleCodeExporter commented 9 years ago
I've looked at the patch and its a good workaround.  There are many limitations 
to
using the code generator from protobuf classes.

Have you looked at the protostuff-compiler?
It covers all the limitations of code-generation from generated classes.
That should be able to solve your problem.
I'll update the online docs today.

Original comment by david.yu...@gmail.com on 6 Mar 2010 at 7:32

GoogleCodeExporter commented 9 years ago
I modified it a bit where the imports are declared as an option.

E.g
greet.fullClassname = com.example.bar.model.GreetProto
greet.outputPackage = com.example.bar.json
greet.outputDir = generated
greet.generator = json
greet.encoding = UTF-8
greet.options =
k1:v1,k2:v2,imports:com.example.bar.json.FooProto&com.example.bar.json.BarProto

The separator for the multiple imports is "&"

Note that this is a only workaround if using the protostuff-codegen.  
It is very limited. 

For most cases, I'd recommend protostuff-compiler.
Attached patch is applied.
Thanks for the initial patch!

Original comment by david.yu...@gmail.com on 9 Mar 2010 at 4:46

Attachments: