I want to throw exception if there is a duplicate key in the YAML.. is that possible? how?

59 views
Skip to first unread message

sanjeev mishra

unread,
Nov 3, 2016, 3:37:52 AM11/3/16
to yamlbeans-users
I have a yaml file like below:

materials:
      git:
        git: "git@<path_to_git_project>.git"
        branch: ci
      upstream:
        pipeline: P1
        stage: test
      upstream:
        pipeline: P2
        stage: itest


The above will give me object with HashMap 'upstream' value set to another HashMap containing {pipeline => P2, stage => itest} because 'upstream' is duplicated and the last value is overrides earlier value. However, I wanted to throw an exception saying "you have duplicates in the YAML file". Is there a recommended way of doing so?

Any quick help will be greatly appreciated!!

Nate

unread,
Nov 3, 2016, 6:30:59 AM11/3/16
to yamlbea...@googlegroups.com
There's not a way to do it. Look in YamlReader#readValueInternal about halfway down where it reads name/value pairs. You'll need to keep the names in a set, then you'll know if a name was encountered twice and you can throw an exception.

-Nate

--
--
You received this message because you are subscribed to the "yamlbeans-users" group:
http://groups.google.com/group/yamlbeans-users

---
You received this message because you are subscribed to the Google Groups "yamlbeans-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yamlbeans-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sanjeev mishra

unread,
Nov 3, 2016, 7:08:41 AM11/3/16
to yamlbeans-users
thx Nate.. would you please mind pointing to the exact line there.. Its big method and I am really finding it difficult to figure this out. If it makes sense, I would like to submit a pull request for this functionality in this library itself.. maybe something like an optional field like 'validate=false' when passed true, will also do this duplicate key validation!! what do you think?
To unsubscribe from this group and stop receiving emails from it, send an email to yamlbeans-use...@googlegroups.com.

Nate

unread,
Nov 3, 2016, 7:36:22 AM11/3/16
to yamlbea...@googlegroups.com
Most JSON parsers (Crockford's is probably the only one) and JavaScript just give back the last value rather than throwing a parsing error:
http://stackoverflow.com/a/23195243/187883

I'm not against adding a feature to treat it as an error, but it should be disabled by default.

Look here:
https://github.com/EsotericSoftware/yamlbeans/blob/master/src/com/esotericsoftware/yamlbeans/YamlReader.java#L302

Further down, if object is a Map, you can just check the map to see if it already contains the key and throw an exception if so. Otherwise Beans is used to set the property. Before doing that, check a HashSet to see if the key has already been encountered. readValueInternal is reentrant, so you'll need a new set per readValueInternal call.


To unsubscribe from this group and stop receiving emails from it, send an email to yamlbeans-users+unsubscribe@googlegroups.com.

sanjeev mishra

unread,
Nov 3, 2016, 11:42:25 PM11/3/16
to yamlbeans-users
thx for the directions Nate.

I tried overriding the method, 'readValue' (https://github.com/EsotericSoftware/yamlbeans/blob/master/src/com/esotericsoftware/yamlbeans/YamlReader.java#L112) by extending YamlReader class in my project and use that class instead of YamlReader. I was thinking of writing a new readValueInternalNoDuplicate with the changed logic that you pointed out earlier and call it instead in the 'readValue' method towards the end. Essentially keeping all 'readValue' method as is and just changing the last line to call this new method readValueInternalNoDuplicate. However, I am really stuck because the 'readValue' method uses private and final vars like parser, config, and anchors etc. So, I will kind of have to redeclare them in my class and duplicate what YamlReader already has. 

Do you think that we should rather change the access modifiers on these vars because YanlReader says that one can override 'readValue' ?

Or better, if I make this change to the YamlBeans library itself and you can release a new version of the same and then I use that version instead.

Let me know what you think. I really want to do this.

sanjeev mishra

unread,
Nov 4, 2016, 4:16:44 AM11/4/16
to yamlbeans-users
I ended up raising a pull request (https://github.com/EsotericSoftware/yamlbeans/pull/58) for the addition of this feature Nate. Hope you can merge it
Reply all
Reply to author
Forward
0 new messages