spruce/cmd/spruce-docgen/main.go

135 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"log"
"os"
"strings"
"text/template"
"unicode"
)
var schemaFile = "./schema/cv.schema.json"
var schemaReferenceTemplate = `= JSON schema reference
NOTE: This page was auto-generated with spruce-docgen.
== {{ .Title }}
{{ .Description }}
[%header,cols=3*]
|===
|Field
|Type
|Description
{{- range $key, $property := .Properties }}
{{ print "" }}
|{{ $key }}
|{{ type $property }}
|{{ $property.Description }}
{{- end -}}{{ print "" }}
|===
{{ print "" }}
{{- range $i, $schema := .Defs }}
=== {{ capitalise $i }}
{{ print "" }}
[%header,cols=3*]
|===
|Field
|Type
|Description
{{- range $key, $property := $schema.Properties }}
{{ print "" }}
|{{ $key }}
|{{ type $property }}
|{{ $property.Description }}
{{- end -}}{{ print "" }}
|===
{{ print "" }}
{{- end }}
`
// schema minimally represents the JSON schema format
type schema struct {
Title string `json:"title"`
Description string `json:"description"`
Type string `json:"type"`
Properties map[string]*schema `json:"properties"`
Items *schema `json:"items"`
Required []string `json:"required"`
Ref string `json:"$ref"`
Defs map[string]*schema `json:"$defs"`
AdditionalProperties any `json:"additionalProperties"`
}
func main() {
file, err := os.Open(schemaFile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var data schema
if err := decoder.Decode(&data); err != nil {
log.Fatal(err)
}
funcMap := template.FuncMap{
"capitalise": title,
"type": getType,
}
t := template.Must(template.New("asciidoc").Funcs(funcMap).Parse(schemaReferenceTemplate))
if err = t.Execute(os.Stdout, data); err != nil {
log.Fatal(err)
}
}
func title(str string) string {
runes := []rune(str)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
func getType(s schema) string {
if s.Type != "" && s.Type != "array" {
return s.Type
}
if s.Type == "array" {
if s.Items == nil {
return "list(UNKNOWN)"
}
if s.Items.Type != "" {
return "list(" + s.Items.Type + ")"
}
if s.Items.Ref != "" {
return "list(<<" + title(refType(s.Items.Ref)) + ">>)"
}
}
if s.Type == "" && s.Ref != "" {
return "<<" + title(refType(s.Ref)) + ">>"
}
return "UNKNOWN"
}
func refType(str string) string {
if ! strings.HasPrefix(str, "#/$defs/") {
return "UNKNOWN"
}
split := strings.Split(str, "/")
return split[len(split)-1]
}