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

Use reflect api to improve find api, more tests #149

Merged
merged 3 commits into from
Dec 6, 2019

Conversation

asutula
Copy link
Member

@asutula asutula commented Dec 4, 2019

This allows the user to specify a dummy type when calling find and then use a simple type assertion when receiving the results.

This was a change we decided to make to help the create/save/find apis feel consistent.

closes #143

@asutula asutula self-assigned this Dec 4, 2019
Copy link
Member

@sanderpick sanderpick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool that this works 🔥

Comment on lines 89 to 102
sliceType := reflect.TypeOf(dummySlice)
elementType := sliceType.Elem().Elem()
length := len(x.ModelFindReply.GetEntities())
results := reflect.MakeSlice(sliceType, length, length)
for i, result := range x.ModelFindReply.GetEntities() {
target := reflect.New(elementType).Interface()
err := json.Unmarshal(result, target)
if err != nil {
return nil, err
}
val := results.Index(i)
val.Set(reflect.ValueOf(target))
}
return results.Interface(), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to make a reusable function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea of course. Good call.

@asutula asutula merged commit af2a2eb into master Dec 6, 2019
@asutula asutula deleted the asutula/find-improvements branch December 6, 2019 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consistent return type for client methods
2 participants