Skip to content

Commit

Permalink
update enums and add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pskrbasu committed Nov 29, 2024
1 parent fd1506d commit 37d0828
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
11 changes: 11 additions & 0 deletions internal/resources/dashboard_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,22 @@ 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(),
Subject: block.DefRange.Ptr(),
})
return diags
}
}
}
return t.QueryProviderImpl.OnDecoded(block, resourceMapProvider)
Expand Down
51 changes: 44 additions & 7 deletions internal/resources/dashboard_table_column.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
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"
)

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"`
PrimaryKey *bool `cty:"primary_key" hcl:"primary_key" json:"primary_key,omitempty" snapshot:"primary_key"`
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 && *c.Display != "all" && *c.Display != "none" {
return sperr.New(`invalid value for display: %s (allowed values: 'all', 'none')`, *c.Display)
}

// validate Wrap
if c.Wrap != nil && *c.Wrap != "all" && *c.Wrap != "none" {
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 c.DiffMode != nil && *c.DiffMode != DiffModeInclude && *c.DiffMode != DiffModeExclude && *c.DiffMode != DiffModeKey {
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 @@ -19,5 +56,5 @@ func (c DashboardTableColumn) Equals(other *DashboardTableColumn) bool {
utils.SafeStringsEqual(c.Display, other.Display) &&
utils.SafeStringsEqual(c.Wrap, other.Wrap) &&
utils.SafeStringsEqual(c.HREF, other.HREF) &&
utils.SafeBoolEqual(c.PrimaryKey, other.PrimaryKey)
utils.SafeStringsEqual(c.DiffMode, other.DiffMode)
}
6 changes: 3 additions & 3 deletions tests/acceptance/test_data/mods/dashboard_table/dashboard.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dashboard "testing_card_blocks" {
title = "Testing card blocks"
dashboard "testing_tables" {
title = "Testing table blocks"

container {
table {
Expand All @@ -25,7 +25,7 @@

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

column "name" {
Expand Down

0 comments on commit 37d0828

Please sign in to comment.