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

View File

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