Skip to content

Commit 3f357ed

Browse files
brennanjlchappjc
authored andcommitted
adds all-scalar flag
1 parent 699e332 commit 3f357ed

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

cmd/kwil-cli/cmds/database/batch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func batchCmd() *cobra.Command {
6868
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
6969
}
7070

71+
allScalar, err := getAllScalarsFlag(cmd)
72+
if err != nil {
73+
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
74+
}
75+
7176
fileType, err := getFileType(filePath)
7277
if err != nil {
7378
return display.PrintErr(cmd, fmt.Errorf("error getting file type: %w", err))
@@ -87,7 +92,7 @@ func batchCmd() *cobra.Command {
8792
return display.PrintErr(cmd, fmt.Errorf("error building inputs: %w", err))
8893
}
8994

90-
tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs)
95+
tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs, allScalar)
9196
if err != nil {
9297
return display.PrintErr(cmd, fmt.Errorf("error creating action inputs: %w", err))
9398
}

cmd/kwil-cli/cmds/database/call.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,17 @@ func callCmd() *cobra.Command {
7070
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
7171
}
7272

73+
allScalar, err := getAllScalarsFlag(cmd)
74+
if err != nil {
75+
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
76+
}
77+
7378
inputs, err := parseInputs(args)
7479
if err != nil {
7580
return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err))
7681
}
7782

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

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

153160
for _, a := range schema.Actions {
154161
if strings.EqualFold(a.Name, proc) {
155-
return buildActionInputs(a, inputs)
162+
return buildActionInputs(a, inputs, skipArr)
156163
}
157164
}
158165

@@ -189,7 +196,10 @@ func decodeMany(inputs []string) ([][]byte, bool) {
189196
return b64Arr, b64Ok
190197
}
191198

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

200210
val, ok := input[inputField]
201211
if !ok {
202-
fmt.Println(len(newTuple))
203212
// if not found, we should just add nil
204213
newTuple = append(newTuple, nil)
205214
continue
206215
}
207216

208-
split, err := splitIgnoringQuotedCommas(val)
209-
if err != nil {
210-
return nil, err
217+
var split []string
218+
if !skipArr {
219+
var err error
220+
split, err = splitIgnoringQuotedCommas(val)
221+
if err != nil {
222+
return nil, err
223+
}
224+
} else {
225+
split = []string{val}
211226
}
212227

213228
// attempt to decode base64 encoded values

cmd/kwil-cli/cmds/database/execute.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ func executeCmd() *cobra.Command {
5252
return display.PrintErr(cmd, fmt.Errorf("error getting selected action or procedure: %w", err))
5353
}
5454

55+
allScalar, err := getAllScalarsFlag(cmd)
56+
if err != nil {
57+
return display.PrintErr(cmd, fmt.Errorf("error getting all scalar flag: %w", err))
58+
}
59+
60+
action = strings.ToLower(action)
61+
5562
parsedArgs, err := parseInputs(args)
5663
if err != nil {
5764
return display.PrintErr(cmd, fmt.Errorf("error parsing inputs: %w", err))
5865
}
5966

60-
inputs, err := buildExecutionInputs(ctx, cl, dbid, action, parsedArgs)
67+
inputs, err := buildExecutionInputs(ctx, cl, dbid, action, parsedArgs, allScalar)
6168
if err != nil {
6269
return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err))
6370
}

cmd/kwil-cli/cmds/database/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func getSelectedDbid(cmd *cobra.Command, conf *config.KwilCliConfig) (string, er
8181
// This includes the `execute`, `call`, and `batch` commands.
8282
func bindFlagsTargetingProcedureOrAction(cmd *cobra.Command) {
8383
bindFlagsTargetingDatabase(cmd)
84+
bindAllScalarsFlag(cmd)
8485
cmd.Flags().StringP(actionNameFlag, "a", "", "the target action name")
8586
err := cmd.Flags().MarkDeprecated(actionNameFlag, "pass the action name as the first argument")
8687
if err != nil {
@@ -120,3 +121,11 @@ func bindFlagsTargetingDatabase(cmd *cobra.Command) {
120121
cmd.Flags().StringP(ownerFlag, "o", "", "the target database owner")
121122
cmd.Flags().StringP(dbidFlag, "i", "", "the target database id")
122123
}
124+
125+
func bindAllScalarsFlag(cmd *cobra.Command) {
126+
cmd.Flags().Bool("all-scalars", false, "informs the client that all values should be scalar and never be treated as arrays")
127+
}
128+
129+
func getAllScalarsFlag(cmd *cobra.Command) (bool, error) {
130+
return cmd.Flags().GetBool("all-scalars")
131+
}

0 commit comments

Comments
 (0)