use json.unmarshal for unmarshalling PostgresStatus

This commit is contained in:
Felix Kunde 2019-04-15 16:32:01 +02:00
parent 15b087ea65
commit 487c16a30e
2 changed files with 17 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import (
)
type postgresqlCopy Postgresql
type postgresStatusCopy PostgresStatus
// MarshalJSON converts a maintenance window definition to JSON.
func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) {
@ -71,25 +72,20 @@ func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error {
// UnmarshalJSON converts a JSON to the status subresource definition.
func (ps *PostgresStatus) UnmarshalJSON(data []byte) error {
var got PostgresStatus
var (
tmp postgresStatusCopy
status string
)
str := string(data)
str = strings.Replace(str, "{", "", -1)
str = strings.Replace(str, "}", "", -1)
str = strings.Replace(str, "\"", "", -1)
parts := strings.Split(str, ":")
if len(parts) == 2 || len(parts) == 3 {
if parts[1] != "" {
got.PostgresClusterStatus = parts[len(parts)-1]
} else {
got.PostgresClusterStatus = ClusterStatusUnknown
err := json.Unmarshal(data, &tmp)
if err != nil {
metaErr := json.Unmarshal(data, &status)
if metaErr != nil {
return fmt.Errorf("Could not parse status: %v; err %v", string(data), metaErr)
}
} else {
return fmt.Errorf("incorrect status field of CR")
tmp.PostgresClusterStatus = status
}
*ps = got
*ps = PostgresStatus(tmp)
return nil
}

View File

@ -116,15 +116,13 @@ var postgresStatus = []struct {
out PostgresStatus
err error
}{
{[]byte(`"status":"Running"`),
{[]byte(`{"PostgresClusterStatus":"Running"}`),
PostgresStatus{PostgresClusterStatus: ClusterStatusRunning}, nil},
{[]byte(`"status":{"PostgresClusterStatus":"Running"}`),
{[]byte(`{"PostgresClusterStatus":""}`),
PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil},
{[]byte(`"Running"`),
PostgresStatus{PostgresClusterStatus: ClusterStatusRunning}, nil},
{[]byte(`"status":""`),
PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil},
{[]byte(`"status":{}`),
PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil},
{[]byte(`"status":`),
{[]byte(`""`),
PostgresStatus{PostgresClusterStatus: ClusterStatusUnknown}, nil}}
var unmarshalCluster = []struct {