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

feat(comments): parser #13

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

feat(comments): parser #13

wants to merge 1 commit into from

Conversation

sh0rez
Copy link
Member

@sh0rez sh0rez commented Aug 13, 2020

Introduces a comment parser that extracts docsonnet data from specially formed comments, by leveraging as much of Jsonnet as possible:

  • Comments are extracted from the snippet using Go code
  • @<name> functions are defined in dsl.libsonnet as locals, so that using a bit of string replacement the comments can actually be evaluated. This keeps them compatible with Jsonnet syntax and avoids writing a custom parser :^)
  • The evaluation result is pushed to the bottom of the snippet as a regular patch (Question: Should it be at the top instead, so it can be overwritten from Jsonnet?)

A new cmd/cparse currently exposes the functionality of parsing such Jsonnet:

  // @pkg("helm", "jsonnet-libs.org/helm")
  // @desc(|||
  //   Package `helm` converts Helm chart contents to regular Jsonnet
  //   objects to consume them with tools like [Grafana Tanka](https://tanka.dev)
  // |||)
  
  {
    // @fn("template")
    // @arg("name", string)
    // @arg("chart", string)
    // @arg("opts", object)
    // @desc("template renders a helm chart")
    template(name, chart, opts):: { /* ... */ }

TODO:

  • Ignore non-docsonnet comments
  • Be whitespace insensitive
  • Write unit tests
  • Integrate into docsonnet binary
  • Write extensive docs how to use this
  • Validate Function signature matches docsonnet

/cc @sbarzowski @ghostsquad

Introduces a comment parser that extracts docsonnet data from specially
formed comments
@sh0rez sh0rez added the enhancement New feature or request label Aug 13, 2020
@sh0rez sh0rez marked this pull request as draft August 13, 2020 22:24
@ghostsquad
Copy link

Ooooo interesting

@ghostsquad
Copy link

The downside here is that you are putting the burden of making compatible comments on the user without the benefits of such things like autocomplete, or linting, since a comment is still a comment, yet it is evaluated like code. I'm not sure that's actually valuable

@sh0rez
Copy link
Member Author

sh0rez commented Aug 17, 2020

@ghostsquad Docsonnet follows the idea of structured documentation, which is similar to how tools like jsdoc work. By parsing a small DSL out of comments, it can present pretty powerful and information rich docs.

I experienced this to be more powerful than the ultra minimal approach done by godoc.

Things like autocomplete, syntax highlighting, etc., could definitely be added once we get to actually building editor tooling

@ghostsquad
Copy link

ya, ok, you convinced me. I'm wondering if we can reduce the syntax burden at all without painting ourselves into a corner. Example:

before

// @arg("opts", object)

after

// @arg object opts description?

arg 1: type
arg 2: name
arg 3: optional description, encourage default: foo here

this would unfortunately be a bit more difficult to code, but maybe it's easier to read and write?

@jeschkies
Copy link

This is really interesting indeed. I would have expected a minimal version like godoc. However, Jsonnet libs don't have a predefined structure so it makes sense to leave this up to the author and DSL they use.

Please excuse my ignorance but what is the output structure?

@malcolmholmes
Copy link
Collaborator

Please excuse my ignorance but what is the output structure?

What do you mean by "output structure"? This PR as I understand it converts comments to plain Jsonnet docsonnet, e.g.:

{
    "#myObj": d.obj("myObj holds my functions")
    myObj:: {
        "#myFunc": d.fn("myFunc greets you", [d.arg("who", d.T.string)])
        myFunc(who):: "hello %s!" % who
    }
}

From there, Docsonnet itself can generate HTML docs, e.g. as seen here.

@jeschkies
Copy link

Ah. I didn't know that bit was already there.

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

Successfully merging this pull request may close these issues.

4 participants