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
This commit is contained in:
parent
446691719f
commit
831e91a45d
2 changed files with 31 additions and 5 deletions
|
@ -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"
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue