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

[v0.9] add all-scalar flag #1101

Open
wants to merge 1 commit into
base: release-v0.9
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion cmd/kwil-cli/cmds/database/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func batchCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
}

allScalar, err := getAllScalarsFlag(cmd)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
}

fileType, err := getFileType(filePath)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting file type: %w", err))
Expand All @@ -87,7 +92,7 @@ func batchCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error building inputs: %w", err))
}

tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs)
tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs, allScalar)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error creating action inputs: %w", err))
}
Expand Down
31 changes: 23 additions & 8 deletions cmd/kwil-cli/cmds/database/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ func callCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
}

allScalar, err := getAllScalarsFlag(cmd)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
}

inputs, err := parseInputs(args)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err))
}

tuples, err := buildExecutionInputs(ctx, clnt, dbid, action, inputs)
tuples, err := buildExecutionInputs(ctx, clnt, dbid, action, inputs, allScalar)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error creating action/procedure inputs: %w", err))
}
Expand Down Expand Up @@ -144,15 +149,17 @@ func (r *respCall) MarshalText() (text []byte, err error) {

// buildProcedureInputs will build the inputs for either
// an action or procedure executon/call.
func buildExecutionInputs(ctx context.Context, client clientType.Client, dbid string, proc string, inputs []map[string]string) ([][]any, error) {
// If skipArr is true, it will treat all values as a scalar value if it can't detect
// what the expected type is (which is the case for an action).
func buildExecutionInputs(ctx context.Context, client clientType.Client, dbid string, proc string, inputs []map[string]string, skipArr bool) ([][]any, error) {
schema, err := client.GetSchema(ctx, dbid)
if err != nil {
return nil, fmt.Errorf("error getting schema: %w", err)
}

for _, a := range schema.Actions {
if strings.EqualFold(a.Name, proc) {
return buildActionInputs(a, inputs)
return buildActionInputs(a, inputs, skipArr)
}
}

Expand Down Expand Up @@ -189,7 +196,10 @@ func decodeMany(inputs []string) ([][]byte, bool) {
return b64Arr, b64Ok
}

func buildActionInputs(a *types.Action, inputs []map[string]string) ([][]any, error) {
// buildActionInputs will build the inputs for an action execution/call.
// if skipArr is true, it will treat all values as a scalar value.
// This is useful within CSV, where we do not expected arrays
func buildActionInputs(a *types.Action, inputs []map[string]string, skipArr bool) ([][]any, error) {
tuples := [][]any{}
for _, input := range inputs {
newTuple := []any{}
Expand All @@ -199,15 +209,20 @@ func buildActionInputs(a *types.Action, inputs []map[string]string) ([][]any, er

val, ok := input[inputField]
if !ok {
fmt.Println(len(newTuple))
// if not found, we should just add nil
newTuple = append(newTuple, nil)
continue
}

split, err := splitIgnoringQuotedCommas(val)
if err != nil {
return nil, err
var split []string
if !skipArr {
var err error
split, err = splitIgnoringQuotedCommas(val)
if err != nil {
return nil, err
}
} else {
split = []string{val}
}

// attempt to decode base64 encoded values
Expand Down
9 changes: 8 additions & 1 deletion cmd/kwil-cli/cmds/database/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ func executeCmd() *cobra.Command {
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
}

allScalar, err := getAllScalarsFlag(cmd)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
}

action = strings.ToLower(action)

parsedArgs, err := parseInputs(args)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error parsing inputs: %w", err))
}

inputs, err := buildExecutionInputs(ctx, cl, dbid, action, parsedArgs)
inputs, err := buildExecutionInputs(ctx, cl, dbid, action, parsedArgs, allScalar)
if err != nil {
return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err))
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/kwil-cli/cmds/database/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func getSelectedDbid(cmd *cobra.Command, conf *config.KwilCliConfig) (string, er
// This includes the `execute`, `call`, and `batch` commands.
func bindFlagsTargetingProcedureOrAction(cmd *cobra.Command) {
bindFlagsTargetingDatabase(cmd)
bindAllScalarsFlag(cmd)
cmd.Flags().StringP(actionNameFlag, "a", "", "the target action name")
err := cmd.Flags().MarkDeprecated(actionNameFlag, "pass the action name as the first argument")
if err != nil {
Expand Down Expand Up @@ -120,3 +121,11 @@ func bindFlagsTargetingDatabase(cmd *cobra.Command) {
cmd.Flags().StringP(ownerFlag, "o", "", "the target database owner")
cmd.Flags().StringP(dbidFlag, "i", "", "the target database id")
}

func bindAllScalarsFlag(cmd *cobra.Command) {
cmd.Flags().Bool("all-scalars", false, "informs the client that all values should be scalar and never be treated as arrays")
}

func getAllScalarsFlag(cmd *cobra.Command) (bool, error) {
return cmd.Flags().GetBool("all-scalars")
}
Loading