-
Notifications
You must be signed in to change notification settings - Fork 6
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
Replace hardcoded argument parsing, help text with mappings #13
Conversation
This is nice, thanks!
I like where it's going 👍
Indeed.
I think we should declare two dictionaries, one for the images formats and one for the diagrams types.
|
That makes a lot of sense to me, yeah.
I hadn't even realized that mapping exists, when I put this together — I thought every output format was available for every input format. But from reading the Java service code I see that indeed, not every processor supports every output format. So, that adds an additional wrinkle, and makes the list of formats I added to the help not entirely correct. If those mappings are available in the code, though, it could support something like this instead: $ kroki-cli convert --help
…
-f, --format string output format (default: infer from output file extension
otherwise svg); available formats depend on diagram type
--list-formats list available output formats for the selected diagram type
(requires input filename or --type option)
…
$ kroki-cli convert -t plantuml -f list
Valid output formats for PlantUML: [base64 jpeg png svg txt utxt]
$ kroki-cli convert -t vega --list-formats
Valid output formats for Vega: [pdf png svg] (And, of course, it could also validate the requested export format based on the input format.) On the dictionaries...
Part of me thinks that there should be some concept of a "primary" vs. "alternate" or "alias" name(s) for things, to avoid redundancy. If you list the output formats supported, it doesn't make sense to display both And then file extensions, same thing? If you run
It gets even trickier with diagram types, because looking at
So what's the right line to draw?
So it seems clear that all of Blockdiag's names have to be separate |
Yes I think we should have a primary name "jpeg" and then alias/alternatives.
Regarding file extensions, we should let the users use any valid file extension associated with an ImageFormat. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types In my opinion, 5 file extensions is excessive and I believe that
Indeed 👍
They are not separated diagram types, just a diagram type with a primary name and an alternative name.
Yes that's intended.
I'm not sure I understand why it's bothering you to have more than on name to reference a diagram type? I might be wrong but some users might be more familiar with "dot" than "graphviz" and vice-versa even if they are using the exact same syntax for their diagrams. |
@ferdnyc If you want I can take a stab at it? I might have better suggestions/ideas if I dive into the code 😄 |
d968d16
to
58ca9fe
Compare
@ferdnyc I rebased on master and made a few changes, let me know what you think. I'm quite happy with the result. It brings a few enhancements and removes some code duplication so I would like to move forward with this. In my opinion, we should address the other concerns in subsequent pull requests. We can create an issue to track progress. |
58ca9fe
to
1338204
Compare
1338204
to
68b4fc7
Compare
Speaking of things falling out of sync (yuzutech/kroki#279)...
I noticed when running
kroki help convert
that the list of possible arguments to--type
was incomplete. (See bolded text.) Furthermore, the equivalent valid--format
arguments weren't listed at all:Thus began my journey of discovery.
The end result is that both lists are now displayed, generated semi-automatically (from two slices that list
SupportedDiagramTypes
andSupportedImageFormats
), with each supportedkroki.DiagramType
orkroki.ImageFormat
automatically mapping to its own lowercased name as both the--type
/--format
argument, and the recognized file extension that corresponds.The additional "alias" mappings (
"jpg": kroki.JPEG
,".dot": kroki.GraphViz
, etc.) are hardcoded into the initial mappings. Theinit()
then augments those mappings with the "standard" names taken from the slices..Those mappings are used in place of all the previously-hardcoded
switch
statements incmd/convert.go
, and theSupported,,,
slices are also used to embed the list of valid arguments into the help text. (The additional, redundant mapping values are not displayed, to avoid clutter, though they will still be recognized.)So, the help output looks like this now:
Functionality should be unchanged, all of the same information is there, it's just structured to (hopefully) make maintenance easier longer-term.
If support is subsequently added for any new
kroki.DiagramType
s orkroki.ImageFormat
s, those constants just have to be added to theSupportedDiagramTypes
orSupportedImageFormats
slices exported fromcmd/convert.go
and both the matching and the help text will update automatically.Honestly, the implementation is kind of a hack, and I'm not particularly thrilled with the way it still requires a lot of "manual" mapping construction (even if it's executed programmatically from
cmd/convert.go
's newinit()
function). It feels like there should be a better way. Ideallykroki-go
would publish this sort of information in some way that it could be queried directly from there, but publishing the names as constants does not lend itself to that type of introspection at all, so I really struggled to come up with a more robust API.I had to give myself a half-day crash-course on Go just to write this code, and I don't doubt it can be improved upon. Any suggestions for less stupid, ham-fisted ways of structuring the required data for use by the code would be very welcome.