master
go 101 lines 3.28 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package funcapi
4
5 // ValueOptions defines how the UI should format values in this column.
6 type ValueOptions struct {
7 // Transform controls value formatting (number, duration, datetime, text, xml).
8 Transform FieldTransform
9 // DecimalPoints sets numeric precision when using number formatting.
10 DecimalPoints int
11 // DefaultValue includes a default value in the UI response.
12 DefaultValue any
13 }
14
15 // Column defines a table column for function responses.
16 type Column struct {
17 // Index is the 0-based position in each row array and must match data order.
18 Index int
19 // Name is the column tooltip shown in the UI.
20 // The column header is the map key used in the columns map, not this field.
21 Name string
22 // Type controls the base data type and default rendering.
23 Type FieldType
24 // Units sets the unit label shown next to values (use for numeric columns).
25 Units string
26 // Visualization selects the visual style (value, pill, bar, rich content).
27 Visualization FieldVisual
28 // Sort sets the default sort direction.
29 Sort FieldSort
30 // Sortable allows users to change the sort order in the UI.
31 Sortable bool
32 // Sticky pins the column during horizontal scroll.
33 Sticky bool
34 // Summary defines how values aggregate when grouping rows.
35 Summary FieldSummary
36 // Filter selects the filter UI type (range for numbers, multiselect for categories, facet for logs).
37 Filter FieldFilter
38 // FullWidth lets the column expand to fill available width.
39 FullWidth bool
40 // Wrap enables text wrapping for long values.
41 Wrap bool
42 // DefaultExpandedFilter expands this filter by default.
43 DefaultExpandedFilter bool
44 // UniqueKey marks the unique row identifier column.
45 UniqueKey bool
46 // Visible shows the column by default.
47 Visible bool
48 // ValueOptions controls value formatting (transform, decimals, defaults).
49 ValueOptions ValueOptions
50 // Max sets the upper bound for bar-with-integer visuals.
51 Max *float64
52 // PointerTo includes a pointer target identifier in the UI response.
53 PointerTo string
54 // Dummy includes a dummy flag in the UI response.
55 Dummy bool
56 }
57
58 // BuildColumn converts a Column definition to the JSON map used by the UI.
59 func (c Column) BuildColumn() map[string]any {
60 col := map[string]any{
61 "index": c.Index,
62 "unique_key": c.UniqueKey,
63 "name": c.Name,
64 "visible": c.Visible,
65 "type": c.Type.String(),
66 "visualization": c.Visualization.String(),
67 "sort": c.Sort.String(),
68 "sortable": c.Sortable,
69 "sticky": c.Sticky,
70 "summary": c.Summary.String(),
71 "filter": c.Filter.String(),
72 "full_width": c.FullWidth,
73 "wrap": c.Wrap,
74 "default_expanded_filter": c.DefaultExpandedFilter,
75 }
76
77 if c.Units != "" {
78 col["units"] = c.Units
79 }
80 if c.Max != nil {
81 col["max"] = *c.Max
82 }
83 if c.PointerTo != "" {
84 col["pointer_to"] = c.PointerTo
85 }
86 if c.Dummy {
87 col["dummy"] = true
88 }
89
90 valueOpts := map[string]any{
91 "transform": c.ValueOptions.Transform.String(),
92 "decimal_points": c.ValueOptions.DecimalPoints,
93 "default_value": c.ValueOptions.DefaultValue,
94 }
95 if c.Units != "" {
96 valueOpts["units"] = c.Units
97 }
98 col["value_options"] = valueOpts
99
100 return col
101 }