Skip to content

Commit

Permalink
use reflect.Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Oct 17, 2023
1 parent 644281f commit ce97662
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion csv/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (w *Writer[T]) cellString(ctx context.Context, cell *retable.Cell) (string,
return w.escapeStr(w.nilValue, false), nil
}
v := cell.Value
if v.Kind() == reflect.Ptr {
if v.Kind() == reflect.Pointer {
v = v.Elem()
}
return w.escapeStr(fmt.Sprint(v.Interface()), false), nil
Expand Down
2 changes: 1 addition & 1 deletion htmltable/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (w *Writer[T]) WriteView(ctx context.Context, dest io.Writer, view retable.
templData.RawCells[col] = w.nilValue
continue
}
if val.Kind() == reflect.Ptr {
if val.Kind() == reflect.Pointer {
val = val.Elem()
}
str, isRaw = fmt.Sprint(val.Interface()), false
Expand Down
2 changes: 1 addition & 1 deletion strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func cellString(ctx context.Context, cell *Cell, formatters *TypeFormatters) (st
return "", nil
}
v := cell.Value
if v.Kind() == reflect.Ptr {
if v.Kind() == reflect.Pointer {
v = v.Elem()
}
return fmt.Sprint(v.Interface()), nil
Expand Down
4 changes: 2 additions & 2 deletions structrowsviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (viewer *StructRowsViewer) String() string {
// NewView implements the Viewer interface for StructRowsViewer.
func (viewer *StructRowsViewer) NewView(table any) (View, error) {
rows := reflect.ValueOf(table)
for rows.Kind() == reflect.Ptr && !rows.IsNil() {
for rows.Kind() == reflect.Pointer && !rows.IsNil() {
rows = rows.Elem()
}
if rows.Kind() != reflect.Slice || rows.Kind() == reflect.Array {
return nil, fmt.Errorf("table must be slice or array kind but is %T", table)
}
rowType := rows.Type().Elem()
if rowType.Kind() == reflect.Ptr {
if rowType.Kind() == reflect.Pointer {
rowType = rowType.Elem()
}
if rowType.Kind() != reflect.Struct {
Expand Down
2 changes: 1 addition & 1 deletion typeformatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (f *TypeFormatters) FormatCell(ctx context.Context, cell *Cell) (str string
}
// If pointer type had no direct formatter
// check if dereferenced value type has a formatter
if cellType.Kind() == reflect.Ptr && !cell.Value.IsNil() {
if cellType.Kind() == reflect.Pointer && !cell.Value.IsNil() {
derefCellType := cellType.Elem()
if typeFmt, ok := f.Types[derefCellType]; ok {
str, raw, err := typeFmt.FormatCell(ctx, cell.DerefValue())
Expand Down
10 changes: 5 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// StructFieldTypes returns the exported fields of a struct type
// including the inlined fields of any anonymously embedded structs.
func StructFieldTypes(structType reflect.Type) (fields []reflect.StructField) {
if structType.Kind() == reflect.Ptr {
if structType.Kind() == reflect.Pointer {
structType = structType.Elem()
}
for i := 0; i < structType.NumField(); i++ {
Expand All @@ -30,7 +30,7 @@ func StructFieldTypes(structType reflect.Type) (fields []reflect.StructField) {
// StructFieldValues returns the reflect.Value of exported struct fields
// including the inlined fields of any anonymously embedded structs.
func StructFieldValues(structValue reflect.Value) (values []reflect.Value) {
if structValue.Kind() == reflect.Ptr {
if structValue.Kind() == reflect.Pointer {
structValue = structValue.Elem()
}
structType := structValue.Type()
Expand All @@ -55,7 +55,7 @@ func StructFieldIndex(structPtr, fieldPtr any) (int, error) {
return 0, errors.New("expected struct pointer, got <nil>")
}
structVal := reflect.ValueOf(structPtr)
if structVal.Kind() != reflect.Ptr {
if structVal.Kind() != reflect.Pointer {
return 0, fmt.Errorf("expected struct pointer, got %T", structPtr)
}
if structVal.IsNil() {
Expand All @@ -67,7 +67,7 @@ func StructFieldIndex(structPtr, fieldPtr any) (int, error) {
return 0, errors.New("expected struct field pointer, got <nil>")
}
fieldVal := reflect.ValueOf(fieldPtr)
if fieldVal.Kind() != reflect.Ptr {
if fieldVal.Kind() != reflect.Pointer {
return 0, fmt.Errorf("expected struct field pointer, got %T", fieldPtr)
}
if fieldVal.IsNil() {
Expand Down Expand Up @@ -138,7 +138,7 @@ func ValueIsNil(val reflect.Value) bool {
return true
}
switch val.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map,
case reflect.Pointer, reflect.Interface, reflect.Slice, reflect.Map,
reflect.Chan, reflect.Func, reflect.UnsafePointer:
return val.IsNil()
case reflect.Struct:
Expand Down

0 comments on commit ce97662

Please sign in to comment.