Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anchors don't survive when files are included using the Include Schema #51

Open
djerius opened this issue Nov 25, 2023 · 2 comments
Open

Comments

@djerius
Copy link

djerius commented Nov 25, 2023

I'd like to use included yaml files to keep common configuration data. For example:

# db.yml
db : &db
  host : hostname
  port : 5432

And in the yaml files used by the applications,

# app1.yml
config : !include db.yml
args :
   db : *db

But (perhaps unsurprisingly) the anchor is not carried over from the included file:

my $include = YAML::PP::Schema::Include->new;
my $yp      = YAML::PP->new( schema => [ q{+}, 'Merge', $include ] );
$include->yp($yp);
p $yp->load_file('app1.yml');

Results in

No anchor defined for alias 'db' at .../YAML/PP/Parser.pm line 61.
 at .../YAML/PP/Loader.pm line 94.

I find that I use anchors quite a bit to avoid duplication, and the ability for them to exist across included file boundaries would be quite helpful.

@perlpunk
Copy link
Owner

perlpunk commented Nov 25, 2023

That's definetely a use case I can imagine, and I've been thinking about it recently, since I saw a colleague trying to do something where this would have helped.
Bad news first: The Include plugin isn't designed to do this currently.
What it does: While parsing the main file, it sees the include. It will then start an (almost) completely new process of loading a YAML document from the included file and return it as a data struture.
The upper process takes this value as the value in place for that include entry. The anchors and aliases in the loaded data have already been processed at this time.
In retrospective a tag name !loadfile instead of !include might have been clearer ;-)

I can think of a different kind of include plugin, which I'd rather call "Inline" maybe, where from the perspective of the parser of the main YAML the referenced document would look like it was directly in the main file itself.
This way your use case should be possible. And possibly also the other way around - having the anchor defined in the main YAML and using the alias in the Inline, but it would have the disadvantage that the inlined file wouldn't be a really valid YAML file on its own).
I'm currently swamped with other tasks, though... But I hope I can look into this at some point.

@djerius
Copy link
Author

djerius commented Nov 25, 2023

Thanks for thinking about this. For now I've hacked my config loading code so that if it sees a + sign in the file name it knows it's two files that get concatenated before getting parsed, e.g. 'config.yaml+app1.yaml'. Crude, but it gets it out the door.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants