-
Notifications
You must be signed in to change notification settings - Fork 7
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
Proposal: Template functionality #45
Comments
Small Update: I started to work on the template thing yesterday and got a (very early) version to work. I'll keep developing it in my branch. As of now it can handle templates like these:
The Generators are currently (all) available via Here's a asciinema showing me execute the two templates. https://asciinema.org/a/5kkf2d7k4qlalklmpx0mu2row |
@KevinGimbel I'm sorry it took me a couple of days to reply (busy times!) and I feel a bit bad about the fact I'm not sure this is the right direction. The thing is: I'd like to keep the API as simple as possible and the fact we will have a I've looked briefly at https://github.com/peteraba/fakie and the format seems nice to me. What do you think of that approach? I'm still up for introducing a more advanced format/set of APIs but I'd like to try avoiding a complex format for simple scenarios like: $ echo '{"email":"{{email}}", "subject": "welcome!"}' | fakedata As far as I understand we'd need to loop inside the template only with this proposal? I wonder if we could offer both approaches (like in "simple templates by row" and "complex ones where you loop on your own). What do you think? |
Thank you for your feedback!
As far as the implementation goes this is true. I am not happy with the fact a loop is required inside the template yet, it was simply the fastest approach.
I hope so! I am not yet sure how this would work but it's on my "list". Maybe we can tweek the template implementation so that it will loop when a) I'll take a look at Some changes and additions I have in mind:
The current state is the result of 1-2h of work, so there's still a good bit of work ahead. I didn't open a PR so far because it's really just a first basic implementation - I'll keep working on it as I have the time and comment/update this issue along the way. |
Hi,
|
@jorinvo I don't think I understand what you mean about the
Loop by default is the only thing I have a strong opinion about. It's a much simpler/better user experience that way so I can't agree more. Detection of range doesn't sound that easy but, if it's possible, that's a great feature.
Not sure I fully understand this. Probably an implementation detail? I generally get to the code only when I really understand what the feature is supposed to to. So my bad if I'm not getting it!
I can't agree more. I would still suggest we ignore it until the first implementation of the feature reaches master. But it's definitely something I'd like to add.
👍 |
Sorry, maybe an example is more clear:
That would output 9 lines:
And this one 3:
I can use the second example, if I like to handle all the looping myself. For each "line" Then I could do something like this:
to do this:
Does this makes sense or am I missing something? |
I see! Thank you for the much more detailed explanation! Very much appreciated. I think this would be too much overhead on the user (and it exposes the internals of Go templating too in a way). For simple use cases like: echo '{{range Loop 3}} {{name}} {{email}}{{end}}' | fakedata -l 3 I really think users should be able to write: echo '{{name}} {{email}}' | fakedata -l=3 The basic idea is that in most cases (maybe a personal opinion, I'm not so sure about it), users won't need to control the looping so I'd say we have to serve the most common case with the simplest feature. I think the fact that similar tools use this approach is a good indication that we shouldn't complicate the simplest use case. I'm even thinking about dropping (in the first iteration of the feature) the support for a "range template" (this feature needs a name :)) so that we get the first version of this into master much faster. It would unlock To recap, this is what I would do:
The order of the last two features isn't relevant though (I think we could do them in parallel which makes it nicer). |
The first example you gave will yield 9 lines. The second 3. |
@jorinvo I'm not following you then. Because all your examples do use a I know range is part of the Go syntax and I think that may be the source of confusion here. @KevinGimbel and I weren't debating how to implement loops in Go templating (I think we all know how it works but if we don't that's fine, we'll learn by doing :)). We discussed the public API for users. And I think we agree we don't want them to specify a loop unless they really need something special. So maybe we're just saying the same thing? By default, no need to specify |
That's basically what I'm saying. There is no need to use My argument was about the issue of adding extra flags / ways of detecting if |
@jorinvo 👍 "communication is hard" :) |
Quite some input! Thanks @lucapette and @jorinvo. What I get out of the discussion is the following:
|
@KevinGimbel 👍 as it's a lot of work I'd take out reading from stdin from the first take but not saying no to it if you feel like we can do it in one big PR :) |
I think |
@lucapette @jorinvo I'll look into reading |
@jorinvo @KevinGimbel I wasn't specifically talking about the production code but more all the things around building it (docs, integration tests, and so on). Reading from stdin per se is not that hard, I agree. If we support both ways (pipe and argument) then we'd need some automated detection of input coming from stdin (right? I don't think I'm complicating it but maybe I'm missing something). I'm thinking we should support offer both approaches otherwise we'd be forcing users to pipe templates even when they have it stored in a file which may be not as nice as |
I agree, if you plan on supporting both, it's probably simpler to only implement one first. |
This commit adds the abillity to use cli pipes to pipe templates into `fakedata`. It also adds all generators as named functions to the template funcMap.
Update: CLI pipelines and templates work now. By default the I added a few comments to the commit to explain a few changes, see: 5508aa1 Well the biggest change is that I opted in for defining all generators by hand as you can see here. I spent a few hours trying to load/assign the generators automatically but it just did not work. I created a loop to go over all generators and then tried to assign their In the end - and because some functions needed to be declared by "hand" anyway, I decided to just add the generators by myself. This way we also have a bit more control over them and can handle custom params like with Showcase:
|
You may have seen that the Travis CI build is still failing. I'm on it. 😅 https://travis-ci.org/lucapette/fakedata/jobs/240295071 |
@KevinGimbel this is looking pretty promising! Do you mind opening a PR already and mark it as wip? I think I'd get a better sense of how to plan shipping it in the context of #43 and #48 (seems hard! |
@lucapette I created a pull request, see #49 |
Closing this issue because the PR has been merged. 🎉 |
fakedata
template specsSynopsis
Despite being powerful already,
fakedata
will benefit from a template mechanism. With the underlying Go template engine we can implement a easy-to-use template mechanism which allows users to generate fake datasets in all kinds of formats, regardless of build-in formatters likesql
,csv
, et all.Go Templates allow to combine values with
printf
so we get a way to combine generators our of the box, e.g.{{ printf "%s, %s" Lastname Firstname }}
to print out names in an alternative format to what we are used to in Germany (and other countries).Command Line Interface changes
In order to support templates we need to add a new "top level" command to
fakedata
which can accomplish the following tasks:Stdout
or to a fileBy introducing a new top level command we avoid interference with existing generators and separate this functionality from the "direct" generator invocation.
Template usage
All generators are available as template functions and can be used inside the template to generate data as seen below:
The example above will generate Name and E-Mail pairs like so:
As mentioned above we can now also combine generators inside the templates which gives a huge advantage over using generators in a "as-is" way. Users can define their own format or create specific datasets if needed.
Additional template functions
Go templates have a
FuncMap
which is a mapping of functions available inside the parsed template. We either need to manually add all generators or find a way to automatically make them available.I would like to propose the addition of functions like
Last
,First
,Odd
, andEven
. I believe these "little helpers" can provide a great interface when generating data, e.g. for adding a final semicolon to the end of an SQL statement or to close a JSON block properly.Running the template generator on the template will create a file with valid JSON as seen below:
So this is my proposal written off the top of my head. Are there any things I missed? Feel free to add your input, I believe I will start work on Friday or next week (Monday/Tuesday).
The text was updated successfully, but these errors were encountered: