From 5e8340bc1c69de978b24d71de78030b6da8c992f Mon Sep 17 00:00:00 2001 From: Pedro Nasser Date: Tue, 28 Mar 2017 15:30:12 -0300 Subject: [PATCH] Solving postgres marshal/unmarshal issue (#610) * Solving postgres marshal/unmarshal issue Postgres datastore was not marshaling the App config during its insert, that behavior was resulting in issues when fetching the App and the datastore couldn't unmarshal the config. The same issue was probably happening with the Route's headers in some situations. This commit's idea is to always try to marshal configs and headers when inserting/updating Apps or Routes. But in Apps and Routes get methods, if the config/headers unmarshal fails, it returns an empty config/headers. * fix one more unmarshal case * returning error when unmarshaling non-empty --- api/datastore/postgres/postgres.go | 33 ++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/api/datastore/postgres/postgres.go b/api/datastore/postgres/postgres.go index 5f3ac0fb8..794b7436e 100644 --- a/api/datastore/postgres/postgres.go +++ b/api/datastore/postgres/postgres.go @@ -121,7 +121,7 @@ func (ds *PostgresDatastore) UpdateApp(ctx context.Context, newapp *models.App) return err } - if config != "" { + if len(config) > 0 { err := json.Unmarshal([]byte(config), &app.Config) if err != nil { return err @@ -186,7 +186,10 @@ func (ds *PostgresDatastore) GetApp(ctx context.Context, name string) (*models.A } if len(config) > 0 { - json.Unmarshal([]byte(config), &res.Config) + err := json.Unmarshal([]byte(config), &res.Config) + if err != nil { + return nil, err + } } return res, nil @@ -203,7 +206,14 @@ func scanApp(scanner rowScanner, app *models.App) error { return err } - return json.Unmarshal([]byte(configStr), &app.Config) + if len(configStr) > 0 { + err = json.Unmarshal([]byte(configStr), &app.Config) + if err != nil { + return err + } + } + + return nil } func (ds *PostgresDatastore) GetApps(ctx context.Context, filter *models.AppFilter) ([]*models.App, error) { @@ -410,14 +420,21 @@ func scanRoute(scanner rowScanner, route *models.Route) error { return err } - if headerStr == "" { - return models.ErrRoutesNotFound + if len(headerStr) > 0 { + err = json.Unmarshal([]byte(headerStr), &route.Headers) + if err != nil { + return err + } } - if err := json.Unmarshal([]byte(headerStr), &route.Headers); err != nil { - return err + if len(configStr) > 0 { + err = json.Unmarshal([]byte(configStr), &route.Config) + if err != nil { + return err + } } - return json.Unmarshal([]byte(configStr), &route.Config) + + return nil } func (ds *PostgresDatastore) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {