elnabo / json2object

Type safe Haxe/JSON (de)serializer
MIT License
66 stars 17 forks source link

unknown Identifier when using @:default with enum abstracts #34

Closed AlexHaxe closed 6 years ago

AlexHaxe commented 6 years ago

I am trying to use a json configuration file, where all fields are @:optional, so that users only need to supply values for keys that deviate from my defaults. Many fields use @:enum abstracts, so I tried to @:default(ABSTRACT_VALUE) which didn't compile, complaining about unknown Identifier : ABSTRACT_VALUE. I had to specify the full module name, to get it to compile - which of course looks a bit ugly (see https://github.com/HaxeCheckstyle/tokentree-formatter/blob/master/src/tokentreeformatter/config/IndentationConfig.hx).

I tried to break it down into a smaller testcase, it consists of two files (I used neko target):

package test;

import json2object.JsonParser;

class Main {
    public static function main() {
        var parser = new JsonParser<TestData>();
        var data = parser.fromJson("{}", "test.json");
        trace(data);
    }
}
package test;

typedef TestData = {
    @:default(VAL2) @:optional var test:TestEnum;
}

@:enum
abstract TestEnum(String) {
    var VAL1 = "value1";
    var VAL2 = "value2";
    var VAL3 = "value3";
}

Compiler (Haxe 3.4.7 and Haxe 4 preview 4) complains: test/TestData.hx:4: characters 11-15 : Unknown identifier : VAL2

ibilon commented 6 years ago

Should be working now, the error position if you get an incorrect value in a @:default is not yet correct though.