-
Notifications
You must be signed in to change notification settings - Fork 168
Added JSON input validation for CLI commands #1771
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
Changes from 1 commit
58ab2f2
0aca374
b0aeee5
dbb3c2b
3ddf6b5
5f839e3
5a0197e
1280428
7594836
cf2ab73
35f2d58
d25d95b
d4a3346
82d6640
71bc701
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package jsonloader | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
| ) | ||
|
|
||
| func LoadJSON(data []byte) (dyn.Value, error) { | ||
| var root map[string]interface{} | ||
| err := json.Unmarshal(data, &root) | ||
| if err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
|
|
||
| loc := dyn.Location{ | ||
| Line: 1, | ||
| Column: 1, | ||
| } | ||
| return newLoader().load(&root, loc) | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package jsonloader | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn/convert" | ||
| "github.com/databricks/databricks-sdk-go/service/jobs" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const jsonData = ` | ||
| { | ||
| "job_id": 123, | ||
| "new_settings": { | ||
| "name": "xxx", | ||
| "email_notifications": { | ||
| "on_start": [], | ||
| "on_success": [], | ||
| "on_failure": [] | ||
| }, | ||
| "webhook_notifications": { | ||
| "on_start": [], | ||
| "on_failure": [] | ||
| }, | ||
| "notification_settings": { | ||
| "no_alert_for_skipped_runs": true, | ||
| "no_alert_for_canceled_runs": true | ||
| }, | ||
| "timeout_seconds": 0, | ||
| "max_concurrent_runs": 1, | ||
| "tasks": [ | ||
| { | ||
| "task_key": "xxx", | ||
| "email_notifications": {}, | ||
| "notification_settings": {}, | ||
| "timeout_seconds": 0, | ||
| "max_retries": 0, | ||
| "min_retry_interval_millis": 0, | ||
| "retry_on_timeout": "true" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| func TestJsonLoader(t *testing.T) { | ||
| v, err := LoadJSON([]byte(jsonData)) | ||
| require.NoError(t, err) | ||
|
|
||
| var r jobs.ResetJob | ||
| err = convert.ToTyped(&r, v) | ||
| require.NoError(t, err) | ||
| } | ||
|
andrewnester marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package jsonloader | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn" | ||
| ) | ||
|
|
||
| type loader struct { | ||
| } | ||
|
|
||
| func newLoader() *loader { | ||
| return &loader{} | ||
| } | ||
|
|
||
| func errorf(loc dyn.Location, format string, args ...interface{}) error { | ||
| return fmt.Errorf("json (%s): %s", loc, fmt.Sprintf(format, args...)) | ||
| } | ||
|
|
||
| func (d *loader) load(node any, loc dyn.Location) (dyn.Value, error) { | ||
| var value dyn.Value | ||
| var err error | ||
|
|
||
| if node == nil { | ||
| return dyn.NilValue, nil | ||
| } | ||
|
|
||
| if reflect.TypeOf(node).Kind() == reflect.Ptr { | ||
| return d.load(reflect.ValueOf(node).Elem().Interface(), loc) | ||
| } | ||
|
|
||
| switch reflect.TypeOf(node).Kind() { | ||
| case reflect.Map: | ||
| value, err = d.loadMapping(node.(map[string]interface{}), loc) | ||
| case reflect.Slice: | ||
| value, err = d.loadSequence(node.([]interface{}), loc) | ||
| case reflect.String, reflect.Bool, | ||
| reflect.Float64, reflect.Float32, | ||
| reflect.Int, reflect.Int32, reflect.Int64, | ||
| reflect.Uint, reflect.Uint32, reflect.Uint64: | ||
| value, err = d.loadScalar(node, loc) | ||
|
|
||
| default: | ||
| return dyn.InvalidValue, errorf(loc, "unknown node kind: %v", reflect.TypeOf(node).Kind()) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
|
|
||
| return value, nil | ||
| } | ||
|
|
||
| func (d *loader) loadScalar(node any, loc dyn.Location) (dyn.Value, error) { | ||
| switch reflect.TypeOf(node).Kind() { | ||
| case reflect.String: | ||
| return dyn.NewValue(node.(string), []dyn.Location{loc}), nil | ||
| case reflect.Bool: | ||
| return dyn.NewValue(node.(bool), []dyn.Location{loc}), nil | ||
| case reflect.Float64, reflect.Float32: | ||
| return dyn.NewValue(node.(float64), []dyn.Location{loc}), nil | ||
| case reflect.Int, reflect.Int32, reflect.Int64: | ||
| return dyn.NewValue(node.(int64), []dyn.Location{loc}), nil | ||
| case reflect.Uint, reflect.Uint32, reflect.Uint64: | ||
| return dyn.NewValue(node.(uint64), []dyn.Location{loc}), nil | ||
| default: | ||
| return dyn.InvalidValue, errorf(loc, "unknown scalar type: %v", reflect.TypeOf(node).Kind()) | ||
| } | ||
| } | ||
|
|
||
| func (d *loader) loadSequence(node []interface{}, loc dyn.Location) (dyn.Value, error) { | ||
| dst := make([]dyn.Value, len(node)) | ||
| for i, value := range node { | ||
| v, err := d.load(value, loc) | ||
| if err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
| dst[i] = v | ||
| } | ||
| return dyn.NewValue(dst, []dyn.Location{loc}), nil | ||
| } | ||
|
|
||
| func (d *loader) loadMapping(node map[string]interface{}, loc dyn.Location) (dyn.Value, error) { | ||
| dst := make(map[string]dyn.Value) | ||
| index := 0 | ||
| for key, value := range node { | ||
| index += 1 | ||
| v, err := d.load(value, dyn.Location{ | ||
| Line: loc.Line + index, | ||
| Column: loc.Column, | ||
| }) | ||
| if err != nil { | ||
| return dyn.InvalidValue, err | ||
| } | ||
| dst[key] = v | ||
| } | ||
| return dyn.NewValue(dst, []dyn.Location{loc}), nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| package flags | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/databricks/cli/libs/dyn/convert" | ||
| "github.com/databricks/cli/libs/dyn/jsonloader" | ||
| ) | ||
|
|
||
| type JsonFlag struct { | ||
|
|
@@ -33,7 +35,27 @@ func (j *JsonFlag) Unmarshal(v any) error { | |
| if j.raw == nil { | ||
| return nil | ||
| } | ||
| return json.Unmarshal(j.raw, v) | ||
|
|
||
| dv, err := jsonloader.LoadJSON(j.raw) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do the same for the YAML flag? I believe that's the only reason we still depend on Not blocking for this PR, of course. |
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = convert.ToTyped(v, dv) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, diags := convert.Normalize(v, dv) | ||
| if len(diags) > 0 { | ||
| summary := "" | ||
| for _, diag := range diags { | ||
| summary += fmt.Sprintf("- %s\n", diag.Summary) | ||
| } | ||
| return fmt.Errorf("json input error:\n%v", summary) | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| } | ||
|
andrewnester marked this conversation as resolved.
|
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (j *JsonFlag) Type() string { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.