From 831e91a45d0484d58425b442290fcffcd18c2b98 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 18 Aug 2023 15:06:38 +0100 Subject: [PATCH] fix: improve spruce-docgen Improve spruce-docgen by improving how additionalProperties is unmarshalled. If the value is either true or false then AdditionalProperties is set to the default schema value. Update getType to return discovered maps. Update refType to return UNKNOWN if the ref string is invalid. Regenerated docs/schema.asciidoc --- cmd/spruce-docgen/main.go | 32 +++++++++++++++++++++++++++++--- docs/schema.asciidoc | 4 ++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cmd/spruce-docgen/main.go b/cmd/spruce-docgen/main.go index 44f84bd..c495e06 100644 --- a/cmd/spruce-docgen/main.go +++ b/cmd/spruce-docgen/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "log" "os" @@ -10,6 +11,7 @@ import ( ) var schemaFile = "./schema/cv.schema.json" + var schemaReferenceTemplate = `= JSON schema reference NOTE: This page was auto-generated with spruce-docgen. @@ -60,7 +62,23 @@ type schema struct { Required []string `json:"required"` Ref string `json:"$ref"` Defs map[string]*schema `json:"$defs"` - AdditionalProperties any `json:"additionalProperties"` + AdditionalProperties *schema `json:"additionalProperties"` +} + +func (s *schema) UnmarshalJSON(b []byte) error { + if bytes.Equal(b, []byte("true")) || bytes.Equal(b, []byte("false")) { + *s = schema{} + } else { + type rawSchema schema + var res rawSchema + if err := json.Unmarshal(b, &res); err != nil { + return err + } + + *s = schema(res) + } + + return nil } func main() { @@ -99,7 +117,7 @@ func title(str string) string { } func getType(s schema) string { - if s.Type != "" && s.Type != "array" { + if s.Type != "" && s.Type != "array" && s.Type != "object" { return s.Type } @@ -121,11 +139,19 @@ func getType(s schema) string { return "<<" + title(refType(s.Ref)) + ">>" } + if s.Type == "object" { + if s.AdditionalProperties != nil && s.AdditionalProperties.Type != "" { + return "map(" + s.AdditionalProperties.Type + ")" + } + + return "object" + } + return "UNKNOWN" } func refType(str string) string { - if ! strings.HasPrefix(str, "#/$defs/") { + if !strings.HasPrefix(str, "#/$defs/") { return "UNKNOWN" } diff --git a/docs/schema.asciidoc b/docs/schema.asciidoc index 44135bd..85b472d 100644 --- a/docs/schema.asciidoc +++ b/docs/schema.asciidoc @@ -13,7 +13,7 @@ A short written summary of your skills, achievements and experiences in relation |Description |contact -|object +|map(string) |Your contact information. You can use any key/value pairs here. |education @@ -41,7 +41,7 @@ A short written summary of your skills, achievements and experiences in relation |Your last name. |links -|object +|map(string) |URLs to your online presence such as GitHub or your website. You can use any key/value pairs here. |skills