-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.