|
| 1 | +package tableview |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "unicode" |
| 9 | + |
| 10 | + "github.com/databricks/databricks-sdk-go/listing" |
| 11 | +) |
| 12 | + |
| 13 | +const maxAutoColumns = 8 |
| 14 | + |
| 15 | +var autoCache sync.Map // reflect.Type -> *TableConfig |
| 16 | + |
| 17 | +// AutoDetect creates a TableConfig by reflecting on the element type of the iterator. |
| 18 | +// It picks up to maxAutoColumns top-level scalar fields. |
| 19 | +// Returns nil if no suitable columns are found. |
| 20 | +func AutoDetect[T any](iter listing.Iterator[T]) *TableConfig { |
| 21 | + var zero T |
| 22 | + t := reflect.TypeOf(zero) |
| 23 | + if t.Kind() == reflect.Ptr { |
| 24 | + t = t.Elem() |
| 25 | + } |
| 26 | + |
| 27 | + if cached, ok := autoCache.Load(t); ok { |
| 28 | + return cached.(*TableConfig) |
| 29 | + } |
| 30 | + |
| 31 | + cfg := autoDetectFromType(t) |
| 32 | + if cfg != nil { |
| 33 | + autoCache.Store(t, cfg) |
| 34 | + } |
| 35 | + return cfg |
| 36 | +} |
| 37 | + |
| 38 | +func autoDetectFromType(t reflect.Type) *TableConfig { |
| 39 | + if t.Kind() != reflect.Struct { |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + var columns []ColumnDef |
| 44 | + for i := range t.NumField() { |
| 45 | + if len(columns) >= maxAutoColumns { |
| 46 | + break |
| 47 | + } |
| 48 | + field := t.Field(i) |
| 49 | + if !field.IsExported() || field.Anonymous { |
| 50 | + continue |
| 51 | + } |
| 52 | + if !isScalarKind(field.Type.Kind()) { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + header := fieldHeader(field) |
| 57 | + columns = append(columns, ColumnDef{ |
| 58 | + Header: header, |
| 59 | + Extract: func(v any) string { |
| 60 | + val := reflect.ValueOf(v) |
| 61 | + if val.Kind() == reflect.Ptr { |
| 62 | + if val.IsNil() { |
| 63 | + return "" |
| 64 | + } |
| 65 | + val = val.Elem() |
| 66 | + } |
| 67 | + f := val.Field(i) |
| 68 | + return fmt.Sprintf("%v", f.Interface()) |
| 69 | + }, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + if len(columns) == 0 { |
| 74 | + return nil |
| 75 | + } |
| 76 | + return &TableConfig{Columns: columns} |
| 77 | +} |
| 78 | + |
| 79 | +func isScalarKind(k reflect.Kind) bool { |
| 80 | + switch k { |
| 81 | + case reflect.String, reflect.Bool, |
| 82 | + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 83 | + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 84 | + reflect.Float32, reflect.Float64: |
| 85 | + return true |
| 86 | + default: |
| 87 | + return false |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// fieldHeader converts a struct field to a display header. |
| 92 | +// Uses the json tag if available, otherwise the field name. |
| 93 | +func fieldHeader(f reflect.StructField) string { |
| 94 | + tag := f.Tag.Get("json") |
| 95 | + if tag != "" { |
| 96 | + name, _, _ := strings.Cut(tag, ",") |
| 97 | + if name != "" && name != "-" { |
| 98 | + return snakeToTitle(name) |
| 99 | + } |
| 100 | + } |
| 101 | + return f.Name |
| 102 | +} |
| 103 | + |
| 104 | +func snakeToTitle(s string) string { |
| 105 | + words := strings.Split(s, "_") |
| 106 | + for i, w := range words { |
| 107 | + if w == "id" { |
| 108 | + words[i] = "ID" |
| 109 | + } else if len(w) > 0 { |
| 110 | + runes := []rune(w) |
| 111 | + runes[0] = unicode.ToUpper(runes[0]) |
| 112 | + words[i] = string(runes) |
| 113 | + } |
| 114 | + } |
| 115 | + return strings.Join(words, " ") |
| 116 | +} |
0 commit comments