Skip to content

Commit 3395ee4

Browse files
Use proper type switch in evalProjectionArray.
1 parent 6c8a96c commit 3395ee4

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

schema/query/projection_evaluator.go

+10-27
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
9393
name = pf.Alias
9494
}
9595

96-
if ref, ok := validator.(*schema.Reference); ok {
96+
switch fieldType := validator.(type) {
97+
case *schema.Reference:
9798
// Execute sub-request in batch
9899
e := &In{Field: "id", Values: payload}
99100
q := &Query{
100101
Projection: pf.Children,
101102
Predicate: Predicate{e},
102103
}
103-
rbr.request(ref.Path, q, func(payloads []map[string]interface{}, validator schema.Validator) error {
104+
rbr.request(fieldType.Path, q, func(payloads []map[string]interface{}, validator schema.Validator) error {
104105
var v interface{}
105106
var err error
106107
for i := range payloads {
@@ -119,14 +120,14 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
119120
resMu.Unlock()
120121
return nil
121122
})
122-
} else if dict, ok := validator.(*schema.Dict); ok {
123+
case schema.FieldGetter:
123124
for _, val := range payload {
124125
subval, ok := val.(map[string]interface{})
125126
if !ok {
126-
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
127+
return nil, fmt.Errorf("%s: invalid value: not a dict/object", pf.Name)
127128
}
128129
var err error
129-
if subval, err = evalProjection(ctx, pf.Children, subval, dict, rbr); err != nil {
130+
if subval, err = evalProjection(ctx, pf.Children, subval, fieldType, rbr); err != nil {
130131
return nil, fmt.Errorf("%s.%v", pf.Name, err)
131132
}
132133
var v interface{}
@@ -135,12 +136,12 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
135136
}
136137
res = append(res, v)
137138
}
138-
} else if array, ok := validator.(*schema.Array); ok {
139+
case *schema.Array:
139140
for i, val := range payload {
140141
if subval, ok := val.([]interface{}); ok {
141142
var err error
142143
var subvalp *[]interface{}
143-
if subvalp, err = evalProjectionArray(ctx, pf, subval, &array.Values, rbr); err != nil {
144+
if subvalp, err = evalProjectionArray(ctx, pf, subval, &fieldType.Values, rbr); err != nil {
144145
return nil, fmt.Errorf("%s: error applying projection on array item #%d: %v", pf.Name, i, err)
145146
}
146147
var v interface{}
@@ -152,26 +153,8 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
152153
return nil, fmt.Errorf("%s. is not an array", pf.Name)
153154
}
154155
}
155-
} else {
156-
for _, val := range payload {
157-
subval, ok := val.(map[string]interface{})
158-
if !ok {
159-
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
160-
}
161-
if fg, ok := validator.(schema.FieldGetter); ok {
162-
var err error
163-
if subval, err = evalProjection(ctx, pf.Children, subval, fg, rbr); err != nil {
164-
return nil, fmt.Errorf("%s.%v", pf.Name, err)
165-
}
166-
var v interface{}
167-
if v, err = resolveFieldHandler(ctx, pf, def, subval); err != nil {
168-
return nil, err
169-
}
170-
res = append(res, v)
171-
} else {
172-
return nil, fmt.Errorf("%s. is not an object", pf.Name)
173-
}
174-
}
156+
default:
157+
return nil, fmt.Errorf("%s. unknown field type", pf.Name)
175158
}
176159

177160
return resp, nil

0 commit comments

Comments
 (0)