A library for resolving embedded expressions in YAML, JSON and other. Empower your data with calculations!
$ gem install json-yaml-embedded-expressions
example.yaml
:
packages:
"Office":
subpath: utils/office
modules:
- basename: writer
path: ="#{top.install_path}/#{parent.parent.subpath}/#{basename}"
- basename: spreadsheet
path: ="#{top.install_path}/#{parent.parent.subpath}/#{basename}"
modules_count: =modules.size
install_path: /home/alice
all_paths: =packages["Office"].modules.map(&:path)
Command line:
$ yaml-embedded-expressions example.yaml
---
packages:
Office:
subpath: utils/office
modules:
- basename: writer
path: /home/alice/utils/office/writer
- basename: spreadsheet
path: /home/alice/utils/office/spreadsheet
modules_count: 2
install_path: /home/alice
all_paths:
- /home/alice/utils/office/writer
- /home/alice/utils/office/spreadsheet
Command line (JSON):
$ json-embedded-expressions example.json | json_pp
{
"packages" : {
"Office" : {
"subpath" : "utils/office",
"modules" : [
{
"path" : "/home/alice/utils/office/writer",
"basename" : "writer"
},
{
"basename" : "spreadsheet",
"path" : "/home/alice/utils/office/spreadsheet"
}
],
"modules_count" : 2
}
},
"install_path" : "/home/alice",
"all_paths" : [
"/home/alice/utils/office/writer",
"/home/alice/utils/office/spreadsheet"
]
}
In Ruby:
require 'yaml/embedded_expressions'
require 'easy_hash'
d = (YAML.load(File.read('example.yaml'))
YAML::EmbeddedExpressions.resolve!(d)
puts d
# {
# "packages" => {
# "Office" => {
# "subpath" => "utils/office",
# "modules" => [
# {
# "basename" => "writer",
# "path" => "/home/alice/utils/office/writer"
# },
# {
# "basename" => "spreadsheet",
# "path" => "/home/alice/utils/office/spreadsheet"
# }
# ],
# "modules_count" => 2
# }
# },
# "install_path" => "/home/alice",
# "all_paths" => [
# "/home/alice/utils/office/writer",
# "/home/alice/utils/office/spreadsheet"
# ]
# }
Embedded expression is a string of the form =expr
(and not ==expr
) where
expr
is a Ruby expression. The expression is evaluated in context of the map/array it is contained in. Inside expr
you may use self
, parent
and top
. Map entries may be accessed/assigned via map['key']
. If using command-line utilities or required easy_hash
then map.key
also works.
Embedded expressions in map's keys are ignored, e. g., the following is not evaluated: {"=2+2": "foobar"}
.