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

Rough json schema generation #749

Merged
merged 4 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/fixed-20250208-001347.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: fixed
body: Missing yaml struct tags on a few configs for json schema
time: 2025-02-08T00:13:47.416161947-08:00
custom:
Issue: "687"
1 change: 1 addition & 0 deletions .changie.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=docs/schema.json
changesDir: .changes
unreleasedDir: unreleased
headerPath: header.tpl.md
Expand Down
26 changes: 26 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"go/ast"
godoc "go/doc"
Expand All @@ -14,6 +15,7 @@ import (
"sort"
"strings"

"github.com/invopop/jsonschema"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

Expand Down Expand Up @@ -102,6 +104,30 @@ func (g *Gen) Run(cmd *cobra.Command, args []string) error {
// This auto generates a h4 which is added to our table of contents.
cmd.Root().DisableAutoGenTag = true

jsonReflector := jsonschema.Reflector{}
jsonReflector.FieldNameTag = "yaml"
jsonReflector.ExpandedStruct = true

err = jsonReflector.AddGoComments("github.com/miniscruff/changie", "./")
if err != nil {
return fmt.Errorf("creating jsonschema go comments: %w", err)
}

jsonSchemaFile, err := os.Create(filepath.Join("docs", "schema.json"))
if err != nil {
return fmt.Errorf("creating or opening json schema file: %w", err)
}
defer jsonSchemaFile.Close()

schema := jsonReflector.Reflect(&core.Config{})
schemaEncoder := json.NewEncoder(jsonSchemaFile)
schemaEncoder.SetIndent("", " ")

err = schemaEncoder.Encode(schema)
if err != nil {
return fmt.Errorf("creating or opening config index: %w", err)
}

return doc.GenMarkdownTreeCustom(cmd.Root(), "docs/cli", filePrepender, linkHandler)
}

Expand Down
6 changes: 3 additions & 3 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ type KindConfig struct {
// By default it will use label if no key is provided.
// example: yaml
// key: feature
Key string `yaml:",omitempty"`
Key string `yaml:"key,omitempty"`
// Label is the value used in the prompt when selecting a kind.
// example: yaml
// label: Feature
Label string `yaml:",omitempty" required:"true"`
Label string `yaml:"label,omitempty" required:"true"`
// Format will override the root kind format when building the kind header.
// example: yaml
// format: '### {{.Kind}} **Breaking Changes**'
Format string `yaml:",omitempty"`
Format string `yaml:"format,omitempty"`
// Change format will override the root change format when building changes specific to this kind.
// example: yaml
// changeFormat: 'Breaking: {{.Custom.Body}}
Expand Down
8 changes: 4 additions & 4 deletions core/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Custom struct {
// This should only contain alpha numeric characters, usually starting with a capital.
// example: yaml
// key: Issue
Key string `yaml:"" required:"true"`
Key string `yaml:"key" required:"true"`

// Specifies the type of choice which changes the prompt.
//
Expand All @@ -63,7 +63,7 @@ type Custom struct {
// block | Multiline text | [minLength](#custom-minlength) and [maxLength](#custom-maxlength)
// int | Whole numbers | [minInt](#custom-minint) and [maxInt](#custom-maxint)
// enum | Limited set of strings | [enumOptions](#custom-enumoptions) is used to specify values
Type CustomType `yaml:"" required:"true"`
Type CustomType `yaml:"type" required:"true"`

// If true, an empty value will not fail validation.
// The optional check is handled before min so you can specify that the value is optional but if it
Expand All @@ -82,12 +82,12 @@ type Custom struct {
// PROJ-{{.Custom.TicketNumber}}
// {{- end}}
// {{.Body}}
Optional bool `yaml:",omitempty" default:"false"`
Optional bool `yaml:"optional,omitempty" default:"false"`
// Description used in the prompt when asking for the choice.
// If empty key is used instead.
// example: yaml
// label: GitHub Username
Label string `yaml:",omitempty" default:""`
Label string `yaml:"label,omitempty" default:""`
// If specified the input value must be greater than or equal to minInt.
MinInt *int64 `yaml:"minInt,omitempty" default:"nil"`
// If specified the input value must be less than or equal to maxInt.
Expand Down
Loading
Loading