Skip to content

Commit

Permalink
When getting cache values, dereference pointers.
Browse files Browse the repository at this point in the history
This allows using pointer fields (e.g. *string) as client indices.

Signed-off-by: Nadia Pinaeva <[email protected]>
  • Loading branch information
npinaeva committed Aug 7, 2024
1 parent 95e9dd2 commit 58643c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ func valueFromIndex(info *mapper.Info, columnKeys []model.ColumnKey) (interface{
}

func valueFromColumnKey(info *mapper.Info, columnKey model.ColumnKey) (interface{}, error) {
val, err := info.FieldByColumn(columnKey.Column)
val, err := info.DerefFieldByColumn(columnKey.Column)
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions mapper/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func (i *Info) FieldByColumn(column string) (interface{}, error) {
return reflect.ValueOf(i.Obj).Elem().FieldByName(fieldName).Interface(), nil
}

// DerefFieldByColumn returns the field value that corresponds to a column.
// If value is a pointer, it will be dereferenced.
func (i *Info) DerefFieldByColumn(column string) (interface{}, error) {
fieldName, ok := i.Metadata.Fields[column]
if !ok {
return nil, NewErrColumnNotFound(column, i.Metadata.TableName)
}
v := reflect.ValueOf(i.Obj).Elem().FieldByName(fieldName)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface(), nil
}

// FieldByColumn returns the field value that corresponds to a column
func (i *Info) hasColumn(column string) bool {
_, ok := i.Metadata.Fields[column]
Expand Down

0 comments on commit 58643c1

Please sign in to comment.