Skip to content

Commit 9f084ce

Browse files
Dragomir-Ivanovsmyrman
authored andcommitted
Use proper type switch in evalProjectionArray.
1 parent 9a75e4d commit 9f084ce

File tree

1 file changed

+31
-46
lines changed

1 file changed

+31
-46
lines changed

schema/query/projection_evaluator.go

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ 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+
// schema.Reference has higher priority than schema.FieldGetter
98+
case *schema.Reference:
9799
// Execute sub-request in batch
98100
e := &In{Field: "id", Values: payload}
99101
q := &Query{
100102
Projection: pf.Children,
101103
Predicate: Predicate{e},
102104
}
103-
rbr.request(ref.Path, q, func(payloads []map[string]interface{}, validator schema.Validator) error {
105+
rbr.request(fieldType.Path, q, func(payloads []map[string]interface{}, validator schema.Validator) error {
104106
var v interface{}
105107
var err error
106108
for i := range payloads {
@@ -119,28 +121,13 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
119121
resMu.Unlock()
120122
return nil
121123
})
122-
} else if dict, ok := validator.(*schema.Dict); ok {
123-
for _, val := range payload {
124-
subval, ok := val.(map[string]interface{})
125-
if !ok {
126-
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
127-
}
128-
var err error
129-
if subval, err = evalProjection(ctx, pf.Children, subval, dict, rbr); err != nil {
130-
return nil, fmt.Errorf("%s.%v", pf.Name, err)
131-
}
132-
var v interface{}
133-
if v, err = resolveFieldHandler(ctx, pf, def, subval); err != nil {
134-
return nil, err
135-
}
136-
res = append(res, v)
137-
}
138-
} else if array, ok := validator.(*schema.Array); ok {
124+
// schema.Array has higher priority than schema.FieldGetter
125+
case *schema.Array:
139126
for i, val := range payload {
140127
if subval, ok := val.([]interface{}); ok {
141128
var err error
142129
var subvalp *[]interface{}
143-
if subvalp, err = evalProjectionArray(ctx, pf, subval, &array.Values, rbr); err != nil {
130+
if subvalp, err = evalProjectionArray(ctx, pf, subval, &fieldType.Values, rbr); err != nil {
144131
return nil, fmt.Errorf("%s: error applying projection on array item #%d: %v", pf.Name, i, err)
145132
}
146133
var v interface{}
@@ -152,26 +139,24 @@ func evalProjectionArray(ctx context.Context, pf ProjectionField, payload []inte
152139
return nil, fmt.Errorf("%s. is not an array", pf.Name)
153140
}
154141
}
155-
} else {
142+
case schema.FieldGetter:
156143
for _, val := range payload {
157144
subval, ok := val.(map[string]interface{})
158145
if !ok {
159-
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
146+
return nil, fmt.Errorf("%s: invalid value: not a dict/object", pf.Name)
160147
}
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)
148+
var err error
149+
if subval, err = evalProjection(ctx, pf.Children, subval, fieldType, rbr); err != nil {
150+
return nil, fmt.Errorf("%s.%v", pf.Name, err)
173151
}
152+
var v interface{}
153+
if v, err = resolveFieldHandler(ctx, pf, def, subval); err != nil {
154+
return nil, err
155+
}
156+
res = append(res, v)
174157
}
158+
default:
159+
return nil, fmt.Errorf("%s. unknown field type", pf.Name)
175160
}
176161

177162
return resp, nil
@@ -238,18 +223,6 @@ func evalProjection(ctx context.Context, p Projection, payload map[string]interf
238223
resMu.Unlock()
239224
return nil
240225
})
241-
} else if dict, ok := def.Validator.(*schema.Dict); ok {
242-
subval, ok := val.(map[string]interface{})
243-
if !ok {
244-
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
245-
}
246-
var err error
247-
if subval, err = evalProjection(ctx, pf.Children, subval, dict, rbr); err != nil {
248-
return nil, fmt.Errorf("%s.%v", pf.Name, err)
249-
}
250-
if res[name], err = resolveFieldHandler(ctx, pf, def, subval); err != nil {
251-
return nil, err
252-
}
253226
} else if array, ok := def.Validator.(*schema.Array); ok {
254227
if payload, ok := val.([]interface{}); ok {
255228
var err error
@@ -263,6 +236,18 @@ func evalProjection(ctx context.Context, p Projection, payload map[string]interf
263236
} else {
264237
return nil, fmt.Errorf("%s: invalid value: not an array", pf.Name)
265238
}
239+
} else if fg, ok := def.Validator.(schema.FieldGetter); ok {
240+
subval, ok := val.(map[string]interface{})
241+
if !ok {
242+
return nil, fmt.Errorf("%s: invalid value: not a dict", pf.Name)
243+
}
244+
var err error
245+
if subval, err = evalProjection(ctx, pf.Children, subval, fg, rbr); err != nil {
246+
return nil, fmt.Errorf("%s.%v", pf.Name, err)
247+
}
248+
if res[name], err = resolveFieldHandler(ctx, pf, def, subval); err != nil {
249+
return nil, err
250+
}
266251
} else {
267252
return nil, fmt.Errorf("%s: field has no children", pf.Name)
268253
}

0 commit comments

Comments
 (0)