Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diff_mode property to DashboardTableColumn resource to specify primary keys in a table resource #601

Open
wants to merge 5 commits into
base: tp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/resources/dashboard_table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package resources

import (
"fmt"

"github.com/hashicorp/hcl/v2"
typehelpers "github.com/turbot/go-kit/types"
"github.com/turbot/pipe-fittings/constants"
"github.com/turbot/pipe-fittings/cty_helpers"
"github.com/turbot/pipe-fittings/hclhelpers"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/printers"
"github.com/turbot/pipe-fittings/schema"
Expand Down Expand Up @@ -77,11 +80,23 @@ func (t *DashboardTable) Equals(other *DashboardTable) bool {
// OnDecoded implements HclResource
func (t *DashboardTable) OnDecoded(block *hcl.Block, resourceMapProvider modconfig.ModResourcesProvider) hcl.Diagnostics {
t.SetBaseProperties()
var diags hcl.Diagnostics
// populate columns map
if len(t.ColumnList) > 0 {
t.Columns = make(map[string]*DashboardTableColumn, len(t.ColumnList))
for _, c := range t.ColumnList {
t.Columns[c.Name] = c
// validate column properties
if err := c.Validate(); err != nil {
// append the validation error to diagnostics
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Detail: err.Error(),
Summary: fmt.Sprintf("column '%s' has invalid value", c.Name),
Subject: hclhelpers.BlockRangePointer(block),
})
return diags
}
}
}
return t.QueryProviderImpl.OnDecoded(block, resourceMapProvider)
Expand Down
68 changes: 62 additions & 6 deletions internal/resources/dashboard_table_column.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
package resources

import "github.com/turbot/pipe-fittings/utils"
import (
"github.com/turbot/pipe-fittings/sperr"
"github.com/turbot/pipe-fittings/utils"
)

type DiffMode string

const (
DiffModeInclude DiffMode = "include"
DiffModeExclude DiffMode = "exclude"
DiffModeKey DiffMode = "key"
)

// all and none are valid values for display and wrap
var validDisplayAndWrap = map[string]struct{}{
"all": {},
"none": {},
}

// include, exclude and key are valid values for diff_mode
var validDiffMode = map[string]struct{}{
string(DiffModeInclude): {},
string(DiffModeExclude): {},
string(DiffModeKey): {},
}

type DashboardTableColumn struct {
Name string `hcl:"name,label" json:"name" snapshot:"name"`
Display *string `cty:"display" hcl:"display" json:"display,omitempty" snapshot:"display"`
Wrap *string `cty:"wrap" hcl:"wrap" json:"wrap,omitempty" snapshot:"wrap"`
HREF *string `cty:"href" hcl:"href" json:"href,omitempty" snapshot:"href"`
Name string `hcl:"name,label" json:"name" snapshot:"name"`
Display *string `cty:"display" hcl:"display" json:"display,omitempty" snapshot:"display"`
Wrap *string `cty:"wrap" hcl:"wrap" json:"wrap,omitempty" snapshot:"wrap"`
HREF *string `cty:"href" hcl:"href" json:"href,omitempty" snapshot:"href"`
DiffMode *DiffMode `cty:"diff_mode" hcl:"diff_mode,optional" json:"diff_mode,omitempty" snapshot:"diff_mode"`
}

// Validate checks the validity of the column's properties and sets default values.
func (c *DashboardTableColumn) Validate() error {
// validate display
if c.Display != nil {
if _, ok := validDisplayAndWrap[*c.Display]; !ok {
return sperr.New(`invalid value for display: %s (allowed values: 'all', 'none')`, *c.Display)
}
}

// validate wrap
if c.Wrap != nil {
if _, ok := validDisplayAndWrap[*c.Wrap]; !ok {
return sperr.New(`invalid value for wrap: %s (allowed values: 'all', 'none')`, *c.Wrap)
}
}

// Set default DiffMode if not set
if c.DiffMode == nil {
defaultDiffMode := DiffModeInclude
c.DiffMode = &defaultDiffMode
}

// validate DiffMode
if _, ok := validDiffMode[string(*c.DiffMode)]; !ok {
return sperr.New(`invalid value for diff_mode: %s (allowed values: 'include', 'exclude', 'key')`, *c.DiffMode)
}

return nil
}

func (c DashboardTableColumn) Equals(other *DashboardTableColumn) bool {
Expand All @@ -17,5 +72,6 @@ func (c DashboardTableColumn) Equals(other *DashboardTableColumn) bool {
return utils.SafeStringsEqual(c.Name, other.Name) &&
utils.SafeStringsEqual(c.Display, other.Display) &&
utils.SafeStringsEqual(c.Wrap, other.Wrap) &&
utils.SafeStringsEqual(c.HREF, other.HREF)
utils.SafeStringsEqual(c.HREF, other.HREF) &&
utils.SafeStringsEqual(c.DiffMode, other.DiffMode)
}
36 changes: 36 additions & 0 deletions tests/acceptance/test_data/mods/dashboard_table/dashboard.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
dashboard "testing_tables" {
title = "Testing table blocks"

container {
table {
title = "Employee table"
width = 4

sql = <<-EOQ
SELECT
011 AS id,
'dwight' AS name,
'manager' AS role
UNION ALL
SELECT
012 AS id,
'jim' AS name,
'developer' AS role
UNION ALL
SELECT
013 AS id,
'pam' AS name,
'designer' AS role;
EOQ

column "id" {
display = "all"
diff_mode = "key"
}

column "name" {
display = "all"
}
}
}
}
4 changes: 4 additions & 0 deletions tests/acceptance/test_data/mods/dashboard_table/mod.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod "dashboard_table"{
title = "Dashboard using table blocks"
description = "Dashboard for testing table blocks"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"schema_version": "20240607",
"inputs": {},
"variables": {},
"start_time": "2024-11-28T19:25:55.998731+05:30",
"end_time": "2024-11-28T19:25:56.032481+05:30",
"layout": {
"name": "dashboard_table.dashboard.testing_card_blocks",
"children": [
{
"name": "dashboard_table.container.dashboard_testing_card_blocks_anonymous_container_0",
"children": [
{
"name": "dashboard_table.table.container_dashboard_testing_card_blocks_anonymous_container_0_anonymous_table_0",
"panel_type": "table"
}
],
"panel_type": "container"
}
],
"panel_type": "dashboard"
},
"panels": {
"dashboard_table.container.dashboard_testing_card_blocks_anonymous_container_0": {
"dashboard": "dashboard_table.dashboard.testing_card_blocks",
"name": "dashboard_table.container.dashboard_testing_card_blocks_anonymous_container_0",
"panel_type": "container",
"status": "complete"
},
"dashboard_table.dashboard.testing_card_blocks": {
"dashboard": "dashboard_table.dashboard.testing_card_blocks",
"name": "dashboard_table.dashboard.testing_card_blocks",
"panel_type": "dashboard",
"status": "complete",
"title": "Testing card blocks"
},
"dashboard_table.table.container_dashboard_testing_card_blocks_anonymous_container_0_anonymous_table_0": {
"dashboard": "dashboard_table.dashboard.testing_card_blocks",
"name": "dashboard_table.table.container_dashboard_testing_card_blocks_anonymous_container_0_anonymous_table_0",
"panel_type": "table",
"status": "complete",
"title": "Employee table",
"width": 4,
"data": {
"columns": [
{
"name": "id",
"data_type": "INT4"
},
{
"name": "name",
"data_type": "TEXT"
},
{
"name": "role",
"data_type": "TEXT"
}
],
"rows": [
{
"id": 11,
"name": "dwight",
"role": "manager"
},
{
"id": 12,
"name": "jim",
"role": "developer"
},
{
"id": 13,
"name": "pam",
"role": "designer"
}
]
},
"properties": {
"columns": {
"id": {
"name": "id",
"display": "all",
"primary_key": true
},
"name": {
"name": "name",
"display": "all"
}
},
"name": "container_dashboard_testing_card_blocks_anonymous_container_0_anonymous_table_0"
}
}
},
"metadata": {
"view": {
"group_by": [
{
"type": "benchmark"
},
{
"type": "control"
},
{
"type": "result"
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"properties": {
"columns": {
"Alternative Names": {
"diff_mode": "include",
"name": "Alternative Names",
"wrap": "all"
}
Expand Down