Skip to content

Commit

Permalink
Fix schema generator (#1291)
Browse files Browse the repository at this point in the history
* Fix JSON schema generator

Signed-off-by: Thomas Poignant <[email protected]>

* Fix JSON schema generator

Signed-off-by: Thomas Poignant <[email protected]>

---------

Signed-off-by: Thomas Poignant <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
thomaspoignant and kodiakhq[bot] authored Nov 20, 2023
1 parent a576f77 commit 2af7bee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions cmd/jsonschema-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/invopop/jsonschema"
"github.com/jessevdk/go-flags"
"github.com/thomaspoignant/go-feature-flag/internal/dto"
Expand All @@ -14,14 +15,16 @@ func main() {
var opts struct {
SchemaLocation string `long:"schema-location" description:"Location where the schema will be saved." required:"true"` //nolint: lll
}

_, err := flags.Parse(&opts)
if flags.WroteHelp(err) {
os.Exit(0)
}
if err != nil {
log.Fatal("impossible to parse command line parameters", err)
}

if opts.SchemaLocation == "" {
log.Fatal("schema-location is required")
}

a := map[string]dto.DTOv1{}
d := jsonschema.Reflect(a)
jsonSchema, err := d.MarshalJSON()
Expand All @@ -34,17 +37,18 @@ func main() {
log.Fatal("impossible to prettify jsonschema", err)
}

err = os.WriteFile(opts.SchemaLocation, []byte(prettyJSONSchema), 0600)
fmt.Println("Writing jsonschema to", opts.SchemaLocation)
err = os.WriteFile(opts.SchemaLocation, prettyJSONSchema, 0600)
if err != nil {
log.Fatalf("impossible to write jsonschema file to %s: %s", opts.SchemaLocation, err)
}
}

// PrettyString will prettify the JSON string
func PrettyString(str string) (string, error) {
func PrettyString(str string) ([]byte, error) {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
return "", err
return []byte(""), err
}
return prettyJSON.String(), nil
return prettyJSON.Bytes(), nil
}
4 changes: 2 additions & 2 deletions internal/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type DTOv1 struct {
// in your flag.
Scheduled *[]flag.ScheduledStep `json:"scheduledRollout,omitempty" yaml:"scheduledRollout,omitempty" toml:"scheduledRollout,omitempty" jsonschema:"title=scheduledRollout,description=Configure an update on some fields of your flag over time."` // nolint: lll

// Experimentation is your struct to configure an experimentation, it will allow you to configure a start date and
// an end date for your flag.
// Experimentation is your struct to configure an experimentation.
// It will allow you to configure a start date and an end date for your flag.
// When the experimentation is not running, the flag will serve the default value.
Experimentation *ExperimentationDto `json:"experimentation,omitempty" yaml:"experimentation,omitempty" toml:"experimentation,omitempty" jsonschema:"title=experimentation,description=Configure an experimentation. It will allow you to configure a start date and an end date for your flag."` // nolint: lll

Expand Down

0 comments on commit 2af7bee

Please sign in to comment.