hof icon indicating copy to clipboard operation
hof copied to clipboard

How to iterate calls to hof and pass in json data

Open magick93 opened this issue 2 years ago • 5 comments

Hello Can you tell me if the following is possible, if and if so, what is needed to achieve this:

I have a json fie - an array of ojbects - and I'd like to use this to call hof where each json object is used as the input data, which is then used when manifesting generated code.

From reading the documentation it seems that data - and schema - is in the .cue files. Do I need to convert the json to cue?

magick93 avatar Feb 25 '22 19:02 magick93

All JSON is valid CUE, so you should be able to do this. You can even have a bit more control over how it is used and referenced. There are probably slight differences in how data files work. For JSON you might even get away with just renaming them to .cue

Is it just the input data, or also the schemas and generators, which would be in JSON?

To iterate, call hof once and use CUE's looping / comprehension features to define a generator for each sub object you wish. You can probably have a single generator and use the repeated template pattern https://docs.hofstadter.io/first-example/simple-server/generator/#server-generator

I think this should all work, so if it doesn't, let me know

verdverm avatar Feb 26 '22 07:02 verdverm

Just input data.

And the goal is to generate several .md files, and prepopulate the frontmatter. But then to be able to update the marketdown indepentant of the code generation workflow.

Will I be able to re-run, and update these .md files, and update the frontmatter without losing edits to the content?

magick93 avatar Feb 26 '22 07:02 magick93

Ah, yes, you should definitely be able to do that. I updated my previous comment about repeated templates. I will think on this a bit more to see if there is a good way to handle arbitrary nested markdown content with globs or something. There seems to be a general pastern here

verdverm avatar Feb 26 '22 07:02 verdverm

Thanks, much appreciated

magick93 avatar Feb 26 '22 22:02 magick93

@magick93 I've got a one-liner for you now

hof render data.json -T "template.md;[]content/{{ .filepath }}.md"

This is the repeated template pattern, denoted by the ;[]<filepath> portion of the -T/--template flag

Links to the new command's docs

  • https://docs.hofstadter.io/getting-started/test-drive/file-rendering/
  • https://docs.hofstadter.io/getting-started/test-drive/type-rendering/
  • https://docs.hofstadter.io/hof-render/

verdverm avatar Jul 04 '22 02:07 verdverm

Please reopen if you have any more questions or uncertainties

verdverm avatar Mar 10 '23 03:03 verdverm

@verdverm you have been very helpful with your support. And you've developed a very powerful tool!

Thank you!

magick93 avatar Mar 10 '23 03:03 magick93