diff --git a/go.mod b/go.mod index 748994f04..20fed51f3 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/fatih/color v1.13.0 github.com/golang-jwt/jwt/v4 v4.4.3 github.com/google/go-github/v39 v39.2.0 - github.com/jackdelahunt/survey-json-schema v0.12.0 + github.com/jackdelahunt/survey-json-schema v0.13.0 github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 // indirect github.com/landoop/tableprinter v0.0.0-20201125135848-89e81fc956e7 github.com/mattn/go-isatty v0.0.16 diff --git a/go.sum b/go.sum index 75ffc5c23..da04b1548 100644 --- a/go.sum +++ b/go.sum @@ -498,8 +498,8 @@ github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/jackdelahunt/survey-json-schema v0.12.0 h1:YqzvIspId/Gb1Iqf0x/pb9AC9wyQjw2yhBPKTkF5l/s= -github.com/jackdelahunt/survey-json-schema v0.12.0/go.mod h1:pEx3fKFqHHzCMAu/tAkYpMAiMXtWYEbwJR8UBsWx2Vs= +github.com/jackdelahunt/survey-json-schema v0.13.0 h1:vi8EgaLXR/t/9OPCyEii0WDHvRvQpKQFG5VvPbrj9Ig= +github.com/jackdelahunt/survey-json-schema v0.13.0/go.mod h1:pEx3fKFqHHzCMAu/tAkYpMAiMXtWYEbwJR8UBsWx2Vs= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= diff --git a/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/schema_type.go b/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/schema_type.go index f10ebf435..206c32736 100644 --- a/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/schema_type.go +++ b/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/schema_type.go @@ -3,11 +3,16 @@ package surveyjson import ( "encoding/json" "fmt" + "strings" "github.com/iancoleman/orderedmap" "github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/util" ) +const ( + commaSeperator = ", " +) + // Based of https://www.ietf.org/archive/id/draft-handrews-json-schema-validation-01.txt type JSONSchemaType struct { Version string `json:"$schema,omitempty"` @@ -130,3 +135,63 @@ func (t *Items) UnmarshalJSON(b []byte) error { } return nil } + +func (schema *JSONSchemaType) ToString() string { + contents := "{ " + + if schema.Type != "" { + contents += "type: " + schema.Type + commaSeperator + } + + if schema.Properties != nil && len(schema.Properties.Keys()) > 0 { + propertiesKeys := schema.Properties.Keys() + contents += "properties: { " + for index, key := range propertiesKeys { + if v, ok := schema.Properties.Get(key); ok { + value := v.(*JSONSchemaType) + contents += key + " : " + value.ToString() + + if index < len(schema.OneOf)-1 { + contents += commaSeperator + } + } + } + contents += " }" + } + + if len(schema.OneOf) > 0 { + contents += "oneOf: [" + + for index, schemaType := range schema.OneOf { + contents += schemaType.ToString() + + if index < len(schema.OneOf)-1 { + contents += commaSeperator + } + } + + contents += "], " + } + + if len(schema.AllOf) > 0 { + contents += "allOf: [" + + for index, schemaType := range schema.AllOf { + contents += schemaType.ToString() + + if index < len(schema.AllOf)-1 { + contents += commaSeperator + } + } + + contents += "], " + } + + if strings.HasSuffix(contents, commaSeperator) { + contents = strings.TrimSuffix(contents, commaSeperator) + } + + contents += " }" + + return contents +} diff --git a/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/survey.go b/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/survey.go index 434f08e56..0ec58aa16 100644 --- a/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/survey.go +++ b/vendor/github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/survey.go @@ -138,6 +138,10 @@ func (o *JSONSchemaOptions) Recurse(ctx SchemaContext) error { err = o.RecurseString(ctx) case "integer": err = o.RecurseInteger(ctx) + default: + if len(ctx.SchemaType.OneOf) != 0 { + err = o.RecurseOneOf(ctx) + } } if err != nil { @@ -376,6 +380,59 @@ func (o *JSONSchemaOptions) RecurseInteger(ctx SchemaContext) error { return nil } +func (o *JSONSchemaOptions) RecurseOneOf(ctx SchemaContext) error { + options := make([]string, len(ctx.SchemaType.OneOf)) + + for i := 0; i < len(options); i++ { + options[i] = ctx.SchemaType.OneOf[i].ToString() + } + + prompt := &survey.Select{ + Message: fmt.Sprintf("Select one of these types to use for %v", ctx.Name), + Options: options, + } + + var answer int + + err := survey.AskOne(prompt, &answer) + if err != nil { + return err + } + + result := orderedmap.New() + + subContext := SchemaContext{ + Name: strconv.Itoa(answer), + Prefixes: ctx.Prefixes, + RequiredFields: ctx.SchemaType.Required, + ParentType: ctx.SchemaType, + SchemaType: ctx.SchemaType.OneOf[answer], + Output: result, + AdditionalValidators: ctx.AdditionalValidators, + ExistingValues: ctx.ExistingValues, + Definitions: ctx.Definitions, + Required: false, + } + + err = o.Recurse(subContext) + if err != nil { + return err + } + + if len(result.Keys()) > 0 { + rootValue, ok := result.Get(strconv.Itoa(answer)) + if !ok { + return errors.New("Cannot get root value from one of result") + } + + ctx.Output.Set(ctx.Name, rootValue) + } else { + ctx.Output.Set(ctx.Name, make(map[string]*JSONSchemaType)) + } + + return nil +} + func (o *JSONSchemaOptions) RecurseNull(ctx SchemaContext) error { ctx.Output.Set(ctx.Name, nil) return nil diff --git a/vendor/modules.txt b/vendor/modules.txt index 5087fa8e4..4e7435d5e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -149,7 +149,7 @@ github.com/inconshreveable/go-update/internal/osext # github.com/inconshreveable/mousetrap v1.0.1 ## explicit; go 1.18 github.com/inconshreveable/mousetrap -# github.com/jackdelahunt/survey-json-schema v0.12.0 +# github.com/jackdelahunt/survey-json-schema v0.13.0 ## explicit; go 1.16 github.com/jackdelahunt/survey-json-schema/pkg/surveyjson github.com/jackdelahunt/survey-json-schema/pkg/surveyjson/util