Skip to content

Commit 89de5d9

Browse files
patch: Improves display name detection
1 parent e97f051 commit 89de5d9

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

pkg/plugin/datasource.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -511,36 +511,35 @@ func dataFrameFromGrid(grid haystack.Grid) (*data.Frame, error) {
511511

512512
// Set Grafana field info from Haystack grid info
513513
config := &data.FieldConfig{}
514-
config.DisplayName = disFromGrid(grid, col)
514+
config.DisplayName = disFromMeta(col.Meta(), col.Name())
515515
config.Unit = unitFromGrid(grid, col)
516516
field.Config = config
517517
fields = append(fields, field)
518518
}
519519

520520
frame := data.NewFrame("response", fields...)
521-
522-
switch id := grid.Meta().Get("id").(type) {
523-
case haystack.Ref:
524-
frame.Name = id.Dis()
525-
default:
526-
frame.Name = ""
527-
}
521+
frameName := disFromMeta(grid.Meta(), "")
522+
frame.Name = frameName
528523
return frame, nil
529524
}
530525

531-
// disFromGrid returns the display name of a column in a haystack grid
532-
func disFromGrid(grid haystack.Grid, col haystack.Col) string {
533-
// If column has 'id' meta, use the Ref name
534-
id := col.Meta().Get("id")
535-
if id != nil {
536-
switch id := id.(type) {
537-
case haystack.Ref:
538-
return id.Dis()
539-
default:
540-
return col.Name()
526+
// disFromMeta returns the display name using metadata. It falls back to the provided string if no other name can be found
527+
func disFromMeta(meta haystack.Dict, name string) string {
528+
// Use meta 'dis'
529+
colDis, success := meta.Get("dis").(haystack.Str)
530+
if success {
531+
return colDis.String()
532+
}
533+
// Then 'id.dis' or 'id'
534+
colRef, success := meta.Get("id").(haystack.Ref)
535+
if success {
536+
if colRef.Dis() != "" {
537+
return colRef.Dis()
541538
}
539+
return colRef.ToZinc()
542540
}
543-
return col.Name()
541+
// Then the input name. We shouldn't get here because all records should have an
542+
return name
544543
}
545544

546545
// unitFromGrid returns the unit of a column in a haystack grid

0 commit comments

Comments
 (0)