Add helper function FirstStringArg

This commit is contained in:
Kuba Podgórski 2021-03-16 16:32:35 +01:00
parent 1dac4c7622
commit d370452449
2 changed files with 31 additions and 36 deletions

View file

@ -1125,12 +1125,9 @@ func (e *executor) executeSum(ctx context.Context, qcx *Qcx, index string, c *pq
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeSum")
defer span.Finish()
fieldName := c.ArgString("field")
if fieldName == "" {
fieldName = c.ArgString("_field")
}
if fieldName == "" {
return ValCount{}, errors.New("Sum(): field required")
fieldName, err := c.FirstStringArg("field", "_field")
if err != nil {
return ValCount{}, errors.Wrap(err, "Sum(): field required")
}
if len(c.Children) > 1 {
@ -1233,12 +1230,8 @@ func (e *executor) executeMin(ctx context.Context, qcx *Qcx, index string, c *pq
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeMin")
defer span.Finish()
field := c.Args["field"]
if field == "" {
field = c.ArgString("_field")
}
if field == "" {
return ValCount{}, errors.New("Min(): field required")
if _, err := c.FirstStringArg("field", "_field"); err != nil {
return ValCount{}, errors.Wrap(err, "Min(): field required")
}
if len(c.Children) > 1 {
@ -1273,12 +1266,8 @@ func (e *executor) executeMax(ctx context.Context, qcx *Qcx, index string, c *pq
span, ctx := tracing.StartSpanFromContext(ctx, "Executor.executeMax")
defer span.Finish()
field := c.ArgString("field")
if field == "" {
field = c.ArgString("_field")
}
if field == "" {
return ValCount{}, errors.New("Max(): field required")
if _, err := c.FirstStringArg("field", "_field"); err != nil {
return ValCount{}, errors.Wrap(err, "Max(): field required")
}
if len(c.Children) > 1 {
@ -1901,12 +1890,9 @@ func (e *executor) executeSumCountShard(ctx context.Context, qcx *Qcx, index str
filter = row
}
fieldName := c.ArgString("field")
if fieldName == "" {
fieldName = c.ArgString("_field")
}
if fieldName == "" {
return ValCount{}, errors.New("Sum(): field required")
fieldName, err := c.FirstStringArg("field", "_field")
if err != nil {
return ValCount{}, errors.Wrap(err, "Sum(): field required")
}
field := e.Holder.Field(index, fieldName)
@ -1958,12 +1944,9 @@ func (e *executor) executeMinShard(ctx context.Context, qcx *Qcx, index string,
filter = row
}
fieldName, ok := c.Args["field"].(string)
if !ok || fieldName == "" {
fieldName = c.ArgString("_field")
}
if fieldName == "" {
return ValCount{}, errors.New("Min(): field required")
fieldName, err := c.FirstStringArg("field", "_field")
if err != nil {
return ValCount{}, errors.Wrap(err, "Min(): field required")
}
field := e.Holder.Field(index, fieldName)
@ -1993,12 +1976,9 @@ func (e *executor) executeMaxShard(ctx context.Context, qcx *Qcx, index string,
filter = row
}
fieldName := c.ArgString("field")
if fieldName == "" {
fieldName = c.ArgString("_field")
}
if fieldName == "" {
return ValCount{}, errors.New("Max(): field required")
fieldName, err := c.FirstStringArg("field", "_field")
if err != nil {
return ValCount{}, errors.Wrap(err, "Max(): field required")
}
field := e.Holder.Field(index, fieldName)

View file

@ -723,6 +723,21 @@ func (c *Call) StringArg(key string) (string, bool, error) {
}
}
func (c *Call) FirstStringArg(keys ...string) (string, error) {
for _, k := range keys {
val, ok, err := c.StringArg(k)
if err != nil {
return "", err
}
if !ok {
continue
}
return val, nil
}
return "", fmt.Errorf("keys: %v not found", keys)
}
// CallArg is for reading the value at key from call.Args as a Call. If the
// key is not in Call.Args, the value of the returned value will be nil, and
// the error will be nil. An error is returned if the value is not a Call.