Skip to content

Commit

Permalink
feat: add config properties
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jan 10, 2024
1 parent 9acaa6a commit 463e67f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type ConfigItem struct {
CostTotal7d float64 `gorm:"column:cost_total_7d;default:null" json:"cost_total_7d,omitempty"`
CostTotal30d float64 `gorm:"column:cost_total_30d;default:null" json:"cost_total_30d,omitempty"`
Tags *types.JSONStringMap `json:"tags,omitempty" faker:"tags"`
Properties *types.Properties `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at" gorm:"autoUpdateTime:false"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions schema/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ table "config_items" {
null = true
type = jsonb
}
column "properties" {
null = true
type = jsonb
}
column "parent_id" {
null = true
type = uuid
Expand Down
18 changes: 18 additions & 0 deletions types/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

import "encoding/json"

// asMap marshals the given struct into a map.
func asMap(t any, removeFields ...string) map[string]any {
m := make(map[string]any)
b, _ := json.Marshal(&t)
if err := json.Unmarshal(b, &m); err != nil {
return m
}

for _, field := range removeFields {
delete(m, field)
}

return m
}
20 changes: 20 additions & 0 deletions types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Link struct {
Text `json:",inline"`
}

// +kubebuilder:object:generate=true
// Property is a realized v1.Property without the lookup definition
type Property struct {
Label string `json:"label,omitempty"`
Expand All @@ -55,8 +56,27 @@ type Property struct {
Links []Link `json:"links,omitempty"`
}

func (p Property) AsMap(removeFields ...string) map[string]any {
return asMap(p, removeFields...)
}

type Properties []*Property

func (m Properties) MarshalJSON() ([]byte, error) {
if m == nil {
return nil, nil
}
t := ([]*Property)(m)
return json.Marshal(t)
}

func (m *Properties) UnmarshalJSON(b []byte) error {
t := []*Property{}
err := json.Unmarshal(b, &t)
*m = Properties(t)
return err
}

func (p Properties) AsJSON() []byte {
if len(p) == 0 {
return []byte("[]")
Expand Down
30 changes: 30 additions & 0 deletions types/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 463e67f

Please sign in to comment.