Question
The latest version supports creating cell paths using variables and mutating using them with eg:
$env.config.max_history_size = 20000
I think a common real-world use-case is to dynamically mutate certain nested keys inside a JSON/YAML if they exist. I got very close to succeeding but seem to fail right before the finish line:
def add_build_args_to_compose [
path,
args
] {
let compose = open $path
mut res = $compose
let build_services = do {
$compose.services
| transpose service vals
| each {|x| if $x.vals.build? != null { $x.service } }
}
for bs in $build_services {
mut to_get = [services $bs build] | into cell-path
$res | get $to_get | $in.args = $args # "$in needs to be a mutable variable"
# $res | get $to_get | upsert args $args # does not seems to mutate
}
$res
I'm trying to fix a design flaw in docker-compose where they don't allow reading ARG values from a file. I want to mutate only the services that have a build key such that they get the passed args and then write to a new docker-compose.yml file.
Trying to to mutate $in fails because its not mutable (...what?)
and upserting/updating does not seem to mutate $res at all.
Is there something i'm missing? I would ideally like to do:
$res.services.($bs).build.args = $args
OR
$res.($to_get).args = $args
Is there some syntax i'm missing or is it impossible to mutate using variables inside a cell-path?
Alternatively, the update command could be extended to be used like what Clojure seemingly has:
https://clojuredocs.org/clojure.core/update-in
I personally have this need at work fairly often where i get some JSON/YAML and it needs to be dynamically mutated somewhere deep inside. Immutability is great and all but in this case it seems way too convoluted. If it stays like this i will have to make a python script and call that to mutate JSON/YAML and i really don't want to do that.
Additional context and details
No response
Question
The latest version supports creating cell paths using variables and mutating using them with eg:
I think a common real-world use-case is to dynamically mutate certain nested keys inside a JSON/YAML if they exist. I got very close to succeeding but seem to fail right before the finish line:
def add_build_args_to_compose [ path, args ] { let compose = open $path mut res = $compose let build_services = do { $compose.services | transpose service vals | each {|x| if $x.vals.build? != null { $x.service } } } for bs in $build_services { mut to_get = [services $bs build] | into cell-path $res | get $to_get | $in.args = $args # "$in needs to be a mutable variable" # $res | get $to_get | upsert args $args # does not seems to mutate } $resI'm trying to fix a design flaw in docker-compose where they don't allow reading ARG values from a file. I want to mutate only the services that have a build key such that they get the passed args and then write to a new docker-compose.yml file.
Trying to to mutate $in fails because its not mutable (...what?)
and upserting/updating does not seem to mutate $res at all.
Is there something i'm missing? I would ideally like to do:
OR
Is there some syntax i'm missing or is it impossible to mutate using variables inside a cell-path?
Alternatively, the update command could be extended to be used like what Clojure seemingly has:
https://clojuredocs.org/clojure.core/update-in
I personally have this need at work fairly often where i get some JSON/YAML and it needs to be dynamically mutated somewhere deep inside. Immutability is great and all but in this case it seems way too convoluted. If it stays like this i will have to make a python script and call that to mutate JSON/YAML and i really don't want to do that.
Additional context and details
No response