Skip to content

Commit

Permalink
Unexpected cache type no longer panics.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchoi-viant committed Oct 9, 2023
1 parent 6f0ca61 commit 5dfa459
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 15 additions & 6 deletions shared/client/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"encoding/json"
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -77,15 +78,23 @@ func (r *Response) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
return nil
}

func (r *Response) DataItemType() reflect.Type {
func (r *Response) DataItemType() (reflect.Type, error) {
if r.Data == nil {
return reflect.TypeOf(&struct{}{})
return reflect.TypeOf(&struct{}{}), nil
}
dataType := reflect.TypeOf(r.Data).Elem()
if dataType.Kind() == reflect.Slice {
return dataType.Elem()

dataType := reflect.TypeOf(r.Data)
// in go1.18 this was renamed to Pointer
if dataType.Kind() != reflect.Ptr && dataType.Kind() != reflect.Interface {
return nil, fmt.Errorf("expected Ptr or Interface, got %v", dataType)
}

elemType := dataType.Elem()
if elemType.Kind() == reflect.Slice {
return elemType.Elem(), nil
}
return dataType

return elemType, nil
}

//NKeys returns object keys JSON (gojay API)
Expand Down
6 changes: 5 additions & 1 deletion shared/client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ func (s *Service) Run(ctx context.Context, input interface{}, response *Response

func (s *Service) loadFromCache(ctx context.Context, cached *[]interface{}, batchSize int, response *Response, cachable Cachable) (int, error) {
*cached = make([]interface{}, batchSize)
dataType := response.DataItemType()
dataType, err := response.DataItemType()
if err != nil {
return 0, err
}

if batchSize > 0 {
cachedCount, err := s.readFromCacheInBatch(ctx, batchSize, dataType, cachable, response, *cached)
if err != nil && !common.IsTransientError(err) {
Expand Down

0 comments on commit 5dfa459

Please sign in to comment.