duplicate key diagnostics

Issue #395 resolved
Alexander Petrossian (PAF) created an issue

currently we have no line number/column where this duplicate key is. ideally report line number:column as well as where the key happened first time.

in gcc it looks like we all know:

rualpe-ws:tmp paf$ cat a.c
void a() {}
void a() {}
rualpe-ws:tmp paf$ gcc a.c
a.c:2:6: error: redefinition of 'a'
void a() {}
     ^
a.c:1:6: note: previous definition is here
void a() {}
     ^
1 error generated.
rualpe-ws:tmp paf$ 

Comments (11)

  1. Andrey Somov

    I see 2 questions here.

    1. The position of the duplicate key is reported. If you think this is not the case, please provide a test to show the problem. (otherwise close the issue)
    2. Reporting of the location of the first occurrence is a separate issue. Let us not mix everything together.
  2. Alexander Petrossian (PAF) reporter

    this is in 1.18 version, but link above goes to repo, where I see no changes.

    what we get on client side:

    Caused by: java.lang.IllegalStateException: duplicate key: global_code
        at org.yaml.snakeyaml.constructor.SafeConstructor.processDuplicateKeys(SafeConstructor.java:102)
        at org.yaml.snakeyaml.constructor.SafeConstructor.flattenMapping(SafeConstructor.java:73)
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:357)
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:532)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:143)
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:129)
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:505)
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:436)
    
  3. Alexander Petrossian (PAF) reporter

    I see no position in this exception. Only name of key. With files of about 3K lines and that key all over them, this error is very hard to handle by a guy who does it in the middle of the night on a production node...

  4. Alexander Petrossian (PAF) reporter

    tried 1.19 version. attaching small test.

    Connected to the target VM, address: '127.0.0.1:62333', transport: 'socket'
    Exception in thread "main" java.lang.IllegalStateException: duplicate key: key1
        at org.yaml.snakeyaml.constructor.SafeConstructor.processDuplicateKeys(SafeConstructor.java:99)
        at org.yaml.snakeyaml.constructor.SafeConstructor.flattenMapping(SafeConstructor.java:70)
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:182)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:443)
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:519)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:207)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:196)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:161)
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:147)
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:437)
        at com.company.Main.main(Main.java:22)
    Disconnected from the target VM, address: '127.0.0.1:62333', transport: 'socket'
    

    I would like file name (if known) and line number of an error.

  5. Andrey Somov
    1. DuplicateKeyException is created
    2. The line number is reported (it was implemented only for Javabeans - that was the confusion)
    3. Filename is not known - SnakeYAML works with streams, not with files.

    Feel free to check the latest source.

  6. Alexander Petrossian (PAF) reporter

    thanks! no file name -- fine with us, we'll add it on a client side. is it possible you'd release this (1.20?)

  7. Alexander Petrossian (PAF) reporter

    It does work, already incorporated snapshot version into our app:

    while constructing a mapping
     in 'reader', line 6, column 17:
                        down: {speed: 41984}
                        ^
    found duplicate key qci
     in 'reader', line 10, column 17:
                        qci: 7
                        ^
    

    thanks Andrey! спасибо!

  8. Log in to comment