Comments (5)
There are two issues here:
The first is the problem of referencing anchors in ignored structure. This is a non-trivial problem due to the way NimYAML deserializes data currently. There are some interesting nuances:
anchors:
- &a
id: &b anchor
items:
- id: *b
- *a
If NimYAML was to say „well I'll remember the anchored events for later“ then in this instance, it'd remember both the &a
and the &b
anchored events, where the second is within the first. Then at *b
, we can create a string from anchor
. At *a
, we will now have the situation where we need to generate a value but parts of the value have already been constructed. Not only will the construction of *a
need to construct a value seen earlier, it also needs to inject that value into the stored events of &a
to make this fully correct.
The second issue is that you want to deserialize an alias node into a non-ref type. This would currently fail even without the ignored part, e.g.
items:
- &a
id: droggeljug
- *a
You'd get
Error: unhandled exception: Anchor on non-ref type [YamlConstructionError]
Should NimYAML support that in this special case? I don't think so; the rule
you can only have anchors on ref types
is simple while
you can have anchors on ref types and in ignored sections as long as they are referred to at most once
makes the API rather confusing.
When we take the second issue out of the equation by making Item
a ref object
, I think NimYAML should be able to properly load this. This will not be trivial to implement and may take a while, but I'll try to do it.
from nimyaml.
Thank you again for your quick reply and thorough analysis!
I am absolutely happy to make Item
a ref object
to simplify things. It absolutely makes sense to allow anchors only on ref types, I should have spotted this one.
from nimyaml.
Workaround:
var node: YamlNode
load(input, node)
var stream = represent(node, tsNone, asNone)
discard stream.next() # skip stream start event
var main: Main
construct(stream, main)
This loads the YAML into a node graph, then generates a stream from those with asNone
which will duplicate a node each time an alias would be produced. That stream can then be loaded into the given type. I would suggest using this approach for situations where YAML anchors are used like variables, however I seriously need to fix the problem of this going in an endless loop when there's a loop in the node graph.
from nimyaml.
This use-case has now been implemented via
import yaml / [serialization, dom]
let input = """
anchors:
- &myAnchor
id: anchor
items:
- *myAnchor
"""
type
Main {.ignore: ["anchors"].} = object
items: seq[Item]
Item = object
id: string
var main: Main
loadFlattened(input, main)
echo $main
loadFlattened
replaces aliases with resolved content before loading and basically implements the workaround I've shown. It is available from the dom
API since it uses the DOM as shown.
Error messages have been updated appropriately.
from nimyaml.
Related Issues (20)
- Option to Ignore unknown fields HOT 1
- Handle multi-line arrays HOT 2
- Currently Failing Suite Tests HOT 1
- Strings of length >= 80 with a single non-space character serialize to invalid YAML HOT 2
- Comment after block/multi-line text HOT 2
- Future compile-time error in dom.nim HOT 2
- loadDom SIGSEGV: Illegal storage access. HOT 7
- [Documentation] Simple example for loading a simple .yml file? HOT 3
- broken enum HOT 3
- Compilation hint: 'dump' cannot raise 'YamlSerializationError' [XCannotRaiseY] HOT 5
- dumping leaves part of string on same line as >- making the file unable to be loaded from HOT 8
- `| >` wrapping functionality can generate invalid YAML HOT 1
- Fails to parse a triple dot in string lit
- defaultVal is not observed on stropped names HOT 2
- Feature: Let user choose/disable maximum line length for presentation HOT 4
- BUG: loading zero to unsigned integer generates a YamlConstructionError
- BUG: loading to float32 generates an error HOT 3
- Can't get optional fields to work HOT 3
- ignore pragma doesn't work on nested maps HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nimyaml.