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

Request: vary put values #202

Open
xxxserxxx opened this issue Mar 5, 2022 · 2 comments
Open

Request: vary put values #202

xxxserxxx opened this issue Mar 5, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@xxxserxxx
Copy link

Is your feature request related to a problem? Please describe.
I often use dasel to mangle test data, and would like to be able to vary the values that I'm puting into the output.

Describe the solution you'd like
I'd like to be able to specify a node selector string in the value.

cat input.json | dasel put -p json string -m -V '"Item" .[]' '.Items.[*].Name'

Where -V (or some other flag) tells dasel to run the query on the current node and use the result as the value. This example would set the attribute "Name": "Item <index>" for each Item object in the Items array.

Describe alternatives you've considered
If the set is large enough, I'd write a Ruby or awk script. My test sets tend to be small, so I usually just edit them by hand.

I also considered using --merge-input-documents, but I don't believe it'd replace the existing objects -- just add to them. In any case, it'd be fairly involved to create the merge document, and possibly more trouble than writing code to modify the data.

Additional context
I'd like to turn this:

{"name": "Real data",
 "things": {
   {"title": "foo", "id": 4843},
   {"title": "top secret", "id": 4448},
   {"title": "blah", "id": 3333}
}}

into this:

{"name": "Fake data",
 "things": {
   {"title": "thing 1", "id": 1},
   {"title": "thing 2", "id": 2},
   {"title": "thing 3", "id": 3}
}}
@xxxserxxx xxxserxxx added the enhancement New feature or request label Mar 5, 2022
@TomWright
Copy link
Owner

This is an interesting use-case.

Note to myself - This could be achieved with the --format flag if the selector was made available in the format templates here:

return template.FuncMap{

@xxxserxxx
Copy link
Author

xxxserxxx commented Mar 8, 2022

Being able to insert values relative to the document would, I think, be a more interesting generally, but for my use case it'd be sufficient if I could call a program (or shell function) for each node. I could generate values externally (if there was a mechanism for it), but (as I said) I imagine it might be more valuable in general to be able to, e.g., set a value from some permutation of another attribute in the node. E.g., set the Name attribute to the equivalent of template-ish "{{.FirstName}} {{.LastName}}"

To be clear, I like the direction you're thinking in. It's frankly better than what I was imagining.

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

No branches or pull requests

2 participants