From 2af7beeffeb03590211c7bf6db518cfaf08ab6fc Mon Sep 17 00:00:00 2001 From: Thomas Poignant Date: Mon, 20 Nov 2023 17:47:31 +0100 Subject: [PATCH] Fix schema generator (#1291) * Fix JSON schema generator Signed-off-by: Thomas Poignant * Fix JSON schema generator Signed-off-by: Thomas Poignant --------- Signed-off-by: Thomas Poignant Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- cmd/jsonschema-generator/main.go | 18 +++++++++++------- internal/dto/dto.go | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/jsonschema-generator/main.go b/cmd/jsonschema-generator/main.go index cb01b05546d..acf6b880a0d 100644 --- a/cmd/jsonschema-generator/main.go +++ b/cmd/jsonschema-generator/main.go @@ -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" @@ -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() @@ -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 } diff --git a/internal/dto/dto.go b/internal/dto/dto.go index 08a3ffe6515..04c9477559f 100644 --- a/internal/dto/dto.go +++ b/internal/dto/dto.go @@ -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