Skip to content

Commit

Permalink
refactor: use enum for Kind type
Browse files Browse the repository at this point in the history
  • Loading branch information
b1zzu committed Feb 3, 2022
1 parent 9399d94 commit 7336cc2
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 99 deletions.
12 changes: 5 additions & 7 deletions pkg/rpdac/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ type IDashboardService interface {

type DashboardService service

const DashboardKind = "Dashboard"

type Dashboard struct {
Kind string `json:"kind"`
Name string `json:"name"`
Description string `json:"description"`
Widgets []*Widget `json:"widgets"`
Kind ObjectKind `json:"kind"`
Name string `json:"name"`
Description string `json:"description"`
Widgets []*Widget `json:"widgets"`

origin *reportportal.Dashboard
}
Expand Down Expand Up @@ -416,7 +414,7 @@ func (d *Dashboard) GetName() string {
return d.Name
}

func (d *Dashboard) GetKind() string {
func (d *Dashboard) GetKind() ObjectKind {
return d.Kind
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/rpdac/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ func TestApplyDashboard_Create(t *testing.T) {
})

inputDashboard := &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "MK E2E Tests Overview",
Description: "",
Widgets: []*Widget{
Expand Down Expand Up @@ -1057,7 +1057,7 @@ func TestApplyDashboard_Update(t *testing.T) {
})

inputDashboard := &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "MK E2E Tests Overview",
Description: "",
Widgets: []*Widget{
Expand Down Expand Up @@ -1258,7 +1258,7 @@ func TestApplyDashboard_Skip(t *testing.T) {
})

inputDashboard := &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "MK E2E Tests Overview",
Description: "",
Widgets: []*Widget{
Expand Down Expand Up @@ -1799,13 +1799,13 @@ func TestDashboardEquals(t *testing.T) {
{
description: "Compare equal dashboards but only one with the origin filed should return true",
left: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test",
Description: "My test description",
origin: &reportportal.Dashboard{ID: 1},
},
right: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test",
Description: "My test description",
},
Expand All @@ -1814,12 +1814,12 @@ func TestDashboardEquals(t *testing.T) {
{
description: "Compare dashboards with differt names should return false",
left: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test One",
Description: "My test description",
},
right: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test",
Description: "My test description",
},
Expand All @@ -1828,12 +1828,12 @@ func TestDashboardEquals(t *testing.T) {
{
description: "Compare dashboards with differt description should return false",
left: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test",
Description: "My test description",
},
right: &Dashboard{
Kind: "Dashboard",
Kind: DashboardKind,
Name: "Test",
Description: "My updated test description",
},
Expand Down
6 changes: 2 additions & 4 deletions pkg/rpdac/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ type IFilterService interface {

type FilterService service

const FilterKind = "Filter"

type Filter struct {
Kind string `json:"kind"`
Kind ObjectKind `json:"kind"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Expand Down Expand Up @@ -191,7 +189,7 @@ func (f *Filter) GetName() string {
return f.Name
}

func (f *Filter) GetKind() string {
func (f *Filter) GetKind() ObjectKind {
return f.Kind
}

Expand Down
26 changes: 13 additions & 13 deletions pkg/rpdac/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestGetFilter(t *testing.T) {
}

want := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Type: "Launch",
Description: "",
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestGetFilterByName(t *testing.T) {
}

want := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Type: "Launch",
Description: "",
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestCreateFilter(t *testing.T) {
})

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestApplyFilter_Create(t *testing.T) {
})

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestApplyFilter_Update(t *testing.T) {
})

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -457,7 +457,7 @@ func TestApplyFilter_Skip(t *testing.T) {
})

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -518,7 +518,7 @@ func TestToFilter(t *testing.T) {
got := ToFilter(inputFilter)

want := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestToFilter(t *testing.T) {
func TestFilterToNewFilter(t *testing.T) {

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestFilterToNewFilter(t *testing.T) {
func TestFilterToUpdateFilter(t *testing.T) {

inputFilter := &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "mk-e2e-test-suite",
Description: "",
Type: "Launch",
Expand Down Expand Up @@ -670,13 +670,13 @@ func TestFilterEquals(t *testing.T) {
{
description: "Compare equal filters but only one with the origin filed should return true",
left: &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "Test",
Description: "My test description",
origin: &reportportal.Filter{ID: 1},
},
right: &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "Test",
Description: "My test description",
},
Expand Down Expand Up @@ -855,7 +855,7 @@ func TestFilterEquals(t *testing.T) {
{
description: "Compare equal filters should return true",
left: &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "Test",
Description: "My test description",
Conditions: []FilterCondition{
Expand All @@ -868,7 +868,7 @@ func TestFilterEquals(t *testing.T) {
},
},
right: &Filter{
Kind: "Filter",
Kind: FilterKind,
Name: "Test",
Description: "My test description",
Conditions: []FilterCondition{
Expand Down
58 changes: 58 additions & 0 deletions pkg/rpdac/kind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package rpdac

import (
"encoding/json"
)

type ObjectKind int

const (
UnknownKind ObjectKind = iota
DashboardKind
FilterKind
)

var kinds = map[ObjectKind]string{
DashboardKind: "Dashboard",
FilterKind: "Filter",
}

func (k ObjectKind) String() string {
return kinds[k]
}

// MarshalJSON marshals the enum as a quoted json string
func (k ObjectKind) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
}

// UnmarshalJSON unmashals a quoted json string to the enum value
func (k *ObjectKind) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
for kind, value := range kinds {
if value == s {
*k = kind
}
}
return nil
}

func (k ObjectKind) MarshalYAML() (interface{}, error) {
return k.String(), nil
}

func (k *ObjectKind) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
for kind, value := range kinds {
if value == s {
*k = kind
}
}
return nil
}
79 changes: 79 additions & 0 deletions pkg/rpdac/kind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package rpdac

import (
"encoding/json"
"testing"

"gopkg.in/yaml.v2"
)

func TestObjectKindMarshalJSON(t *testing.T) {

input := &GenericObject{
Kind: DashboardKind,
Name: "Name",
}

got, err := json.Marshal(input)
if err != nil {
t.Errorf("json.Marshal retunred error: %s", err)
}

want := "{\"kind\":\"Dashboard\",\"name\":\"Name\"}"
testEqual(t, string(got), want)
}

func TestObjectKindMarshalYAML(t *testing.T) {

input := &GenericObject{
Kind: DashboardKind,
Name: "Name",
}

got, err := yaml.Marshal(input)
if err != nil {
t.Errorf("yaml.Marshal retunred error: %s", err)
}

want := `kind: Dashboard
name: Name
`
testEqual(t, string(got), want)
}

func TestObjectKindUnmarshalJSON(t *testing.T) {

input := "{\"kind\":\"Dashboard\",\"name\":\"Name\"}"

got := new(GenericObject)
err := json.Unmarshal([]byte(input), got)
if err != nil {
t.Errorf("json.Unmarshal retunred error: %s", err)
}

want := &GenericObject{
Kind: DashboardKind,
Name: "Name",
}
testDeepEqual(t, got, want)
}

func TestObjectKindUnmarshalYAML(t *testing.T) {

input := `kind: Dashboard
name: Name
`

got := new(GenericObject)
err := yaml.Unmarshal([]byte(input), got)
if err != nil {
t.Errorf("yaml.Unmarshal retunred error: %s", err)
}

want := &GenericObject{
Kind: DashboardKind,
Name: "Name",
}

testDeepEqual(t, got, want)
}
Loading

0 comments on commit 7336cc2

Please sign in to comment.