Include a yaml file inside another

8,767 views
Skip to first unread message

Me

unread,
May 5, 2011, 6:27:27 AM5/5/11
to SnakeYAML
Hi,

Wondering if there is a way to do the above in SnakeYAML? Potentially
similar to what being discussed in the thread below:

http://stackoverflow.com/questions/2278241/how-to-include-a-yaml-file-inside-a-yaml-file

Or what is the best / simplest way implements an extension for that?

(Merge is good, but not ideal for us, use case / requirements: one
yaml file per environment based on a 'default')

Many thanks in advanced.

- A

Andrey

unread,
May 5, 2011, 4:35:50 PM5/5/11
to snakeya...@googlegroups.com
It is not implemented in SnakeYAML because it is not defined in the YAML specification.
 
You may use a custom tag. Then SnakeYAML does not merge anything but you can combine the settings yourself.
Something like this
 
!include /path/to/file
 
 
-
Andrey

DemonWasp

unread,
May 6, 2011, 5:04:28 PM5/6/11
to SnakeYAML
The simplest way to do this would be to create a custom tag with a
corresponding custom Construct subclass (see the Dice example). Your
custom Construct instance would then create another Yaml instance to
load the other file and return the root node of that file as the
value. This approach will accomplish the idea of "include the contents
of this file as this value", but it won't let you use anchors from one
file in another.

A more sophisticated approach would require substantial modification,
but could potentially let anchors work between files. This approach
would be much harder and potentially more error-prone. If you choose
to follow this approach, look at Composer. Your main challenge is
going to be dealing with the "anchors" Map and making sure that its
state accurately reflects where you are at construction-time (usually,
all anchors are cleared at the end of composeDocument(), which is
called via Yaml.loadFromReader() -> BaseConstructor.getSingleData() ->
Composer.getSingleNode() ), before any construction at all happens.

Before starting on the second option, answer the following questions
carefully (where "parent" is the document with the !include node, and
"child" is the document specified for inclusion):
1) If an anchor appears in the parent document, is it accessible by
the child document?
2) If an anchor appears in the child document, is it accessible by the
parent document (after the !include node)?

If your answer to 1) is "yes", then you need to prevent anchors from
being cleared by the end of a document.
If your answer to 2) is "yes", then you will need to dig very, very
deeply. You will need to make the modification at the Parser or
Scanner level, rather than the Compose or Construct levels.

I seriously recommend taking the simplest possible route on this one.

/Jordan


On May 5, 6:27 am, Me <rubbish...@googlemail.com> wrote:
> Hi,
>
> Wondering if there is a way to do the above in SnakeYAML?  Potentially
> similar to what being discussed in the thread below:
>
> http://stackoverflow.com/questions/2278241/how-to-include-a-yaml-file...

Josef Bajada

unread,
Nov 21, 2012, 1:53:29 PM11/21/12
to snakeya...@googlegroups.com, rubbi...@googlemail.com
I wonder if I am being naive here. Would this approach work (with anchors across files)?

1. Load the master file contents in a String
2. Whenever a !include <file> is encountered, load the contents of the file in a separate string, and perform this function (steps 1 to 4) recursively for the child file.
3. Replace the !include <file> with the contents of the child file.
4. Return the whole constructed string.

Then just feed the final String to a StringReader which can then be given to Yaml.loadAll() or Yaml.load()

Anyone sees any pitfalls with that?

Andrey Somov

unread,
Nov 22, 2012, 11:10:28 AM11/22/12
to snakeya...@googlegroups.com

The pitfalls:
1) !include may be a part of a string content -> exclude: "please do not !include this"
2) the parts of the whole document will look like a YAML but they cannot be parsed (because of inconsistent anchors)

-
Andrey

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To view this discussion on the web visit https://groups.google.com/d/msg/snakeyaml-core/-/DpVc11sK4BIJ.

To post to this group, send email to snakeya...@googlegroups.com.
To unsubscribe from this group, send email to snakeyaml-cor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/snakeyaml-core?hl=en.

Francesco Bianco

unread,
Feb 23, 2018, 5:03:01 PM2/23/18
to SnakeYAML
I suggest to use an external command-line tool that parse and created a compose file
Reply all
Reply to author
Forward
0 new messages