16

I'm trying to create a HUGO-based API documentation site which reads JSON schemas, and prints them in HTML.

I'm almost there, but I'm stumped on how exactly to pass in the data I want to a partial.

Given a standard JSON schema file, such as the following:

{"paths": {
  "/auth/login": {
    "get": {
    "operationId": "login",
    "responses": {
      "200": {
        "description": "",
        "schema": {
          "ref": "#/definitions/loginResponse"
        }
      }
    }
  },
},
"definitions": {
  "loginResponse": {
    "type": "object"
  }
}}

I'd like to display the details of that path, and then render a partial using the schema definition in "ref". I found a way to read that ref param and parse it into a reference for the definition. "Target" below looks like:

Target: .definitions.loginResponse

{{ range $path, $methods := .paths }}
  <h4>{{ $path }}</h4>

  {{ range $method, $items := $methods }}
    <h5>{{ $method }}</h5>
    <ul>
      {{ range $status, $info := .responses }}
        <li>
          <div>{{ $status }}</div>
          <h6>Ref: {{ $info.schema.ref }}</h6>
          <p>Target: {{ $target := (printf ".definitions.%s" (index (findRE "[^/]+(/?$)" $info.schema.ref) 0))}}</p>
          <div>{{ partial "schema" $target }}</div>
        </li>
      {{ end }}
    </ul>
  {{end}}
{{end}}

The trouble is, $target is a string. In Javascript, I'd just be able to pass it in as a key to get that object param: schema["definitions.loginResponse"].

No such luck in HUGO, however. I simply can't find a way to go from that target key string to the actual param.

Help! Am I missing something obvious? Am I going about this all wrong?

Garrett Amini
  • 423
  • 3
  • 10
  • 1
    Can you share more details of the directory structure? Is the json file in "data" dir? How do you get to the point where the partial is invoked? Are you create a new type? – Phani Kandula Sep 14 '18 at 03:44
  • The JSON schema files are in /data, the template is in /layouts/_default, and the schema partial is in /layouts/partials. I'm able to get data from the schema as well as render the template and partial, I simply can't pass the data to the partial. – Garrett Amini Sep 14 '18 at 20:29

2 Answers2

2

Figured it out! In case anyone runs into this same problem, param lookup is done simply with Hugo's index function.

{{ $target := index $info.schema "$ref" | findRE "[^/]+(/?$)" }}

With $target, under the parent context, it's merely {{ index . $target }}.

Garrett Amini
  • 423
  • 3
  • 10
0

I think you need to use dict https://gohugo.io/functions/dict/

Your {{ partial "schema" $target }} can be changed to {{ partial "schema" (dict "target_name" $target) }}

In the "schema" partial, you can access value with "{{ .target_name }}"

Phani Kandula
  • 387
  • 2
  • 3
  • Thank you for taking a look! It appears that using this, the "schema" partial gets a new map that looks like this: `{"target_name": ".definitions.loginResponse"}` Unfortunately, I still can't look up the data using that string. I want so badly to just be able to do this: `schema["definitions.loginResponse"]` ! – Garrett Amini Sep 18 '18 at 17:12