google-code-export / protostuff

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

GWT json compiler produces unnecessary line wrap #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
protostuff 1.0.0M4
GWT 2.0.4

If we compile simple proto file:

package foo;

option optimize_for = LITE_RUNTIME;

message Start {
  optional string guid = 1;
}

with properties: 

modules = foo
foo.source = foo.proto
foo.outputDir = ../java
foo.output = gwt_overlay
foo.encoding = UTF-8
foo.generator = gwt_numeric_json
foo.options = 
numeric:true,generate_helper_methods:true,dev_mode:true,use_global_json:true

we get Start.java looking like this (i removed javadoc for small size):

public final class Start extends JavaScriptObject  {
    public static native Start create() /*-{
        return {                                  <---- this one could be as "return {};" though it's not an error

        };
    }-*/;

    public static native JsArray<Start> createArray() /*-{
        return [];
    }-*/;

    public static native Start get(JavaScriptObject jso) /*-{
        return jso;
    }-*/;

    public static native JsArray<Start> getArray(JavaScriptObject jso) /*-{
        return jso;
    }-*/;

    public static native Start parse(String json) /*-{
        return eval("(" + json + ")");
    }-*/;

    public static native JsArray<Start> parseArray(String json) /*-{
        return eval("(" + json + ")");
    }-*/;

    public static native String stringify(Start obj) /*-{
        var buf = [];
        var _1 = obj["1
        "];
        if(_1 != null)
            buf.push("\"1                             <---- unnecessary line wrap
            \":\"" + _1 + "\"");

        return buf.length == 0 ? "{}" : "{" + buf.join(",") + "}";
    }-*/;

    public static native boolean isInitialized(Start obj) /*-{
        return true;
    }-*/;

    protected Start() {}

    // getters and setters

    // guid

    public native String getGuid() /*-{
        return this["1                         <---- unnecessary line wrap
        "] || "";
    }-*/;

    public native void setGuid(String guid) /*-{
        this["1                                <---- unnecessary line wrap
    "] = guid;
    }-*/;

    public native void clearGuid() /*-{
        delete this["1                        <---- unnecessary line wrap
    "];
    }-*/;

    public native boolean hasGuid() /*-{
        return this["1                        <---- unnecessary line wrap
    "] != null;
    }-*/;
}

And this class won't be compiled with gwt js compiler because of methods 
stringify, getGuid, setGuid, clearGuid, hasGuid
All of them have similar problem - unnecessary line wrap like this one:
        return this["1  
    "] != null;

should be as return this["1"] != null;

Original issue reported on code.google.com by nordlig....@gmail.com on 29 Sep 2010 at 2:22

GoogleCodeExporter commented 9 years ago
This is an <elseif> bug on stringtemplate.
I've checked in the workaround.

Original comment by david.yu...@gmail.com on 30 Sep 2010 at 3:07

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
thanks for the fix!
just a small warning left

   public static native boolean isInitialized(Candle obj) /*-{
        return 
            obj["1"] != null;
    }-*/;

between return and obj should not be break too!
i fixed that in gwt_overlay.stg for myself but would like to see that in trunk 
too

Original comment by nordlig....@gmail.com on 30 Sep 2010 at 10:36

GoogleCodeExporter commented 9 years ago
I don't seem to be getting warnings in the isInitialized block (using 2.0.4 
with gwt eclipse plugin)
The problem with putting it all in one line is it's readability.

Original comment by david.yu...@gmail.com on 11 Oct 2010 at 1:12

GoogleCodeExporter commented 9 years ago

Original comment by david.yu...@gmail.com on 11 Oct 2010 at 1:18

GoogleCodeExporter commented 9 years ago
try using intellij idea. it's javascript validator shows warning.
and it's write because some javascript engines will ignore next line after 
'return' with line break.

Original comment by nordlig....@gmail.com on 11 Oct 2010 at 1:18

GoogleCodeExporter commented 9 years ago
Alright, I've added the necessary changes.
Note that gwt compiles your code obfuscated .. so mostly likely will not 
contain the line wrap.

Cheers

Original comment by david.yu...@gmail.com on 11 Oct 2010 at 1:43

GoogleCodeExporter commented 9 years ago
Thank you

Original comment by nordlig....@gmail.com on 11 Oct 2010 at 1:45

GoogleCodeExporter commented 9 years ago
it would also be cool to change method create() in gwt_overlay.stg:

public static native <message.name> create() /*-{
    return {};
}-*/;

Original comment by nordlig....@gmail.com on 27 Oct 2010 at 7:28