Skip to content

Commit 8f2a8ce

Browse files
pedronassermartinpinto
authored andcommitted
Solving postgres marshal/unmarshal issue (iron-io#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
1 parent 99f8593 commit 8f2a8ce

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

api/datastore/postgres/postgres.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (ds *PostgresDatastore) UpdateApp(ctx context.Context, newapp *models.App)
121121
return err
122122
}
123123

124-
if config != "" {
124+
if len(config) > 0 {
125125
err := json.Unmarshal([]byte(config), &app.Config)
126126
if err != nil {
127127
return err
@@ -186,7 +186,10 @@ func (ds *PostgresDatastore) GetApp(ctx context.Context, name string) (*models.A
186186
}
187187

188188
if len(config) > 0 {
189-
json.Unmarshal([]byte(config), &res.Config)
189+
err := json.Unmarshal([]byte(config), &res.Config)
190+
if err != nil {
191+
return nil, err
192+
}
190193
}
191194

192195
return res, nil
@@ -203,7 +206,14 @@ func scanApp(scanner rowScanner, app *models.App) error {
203206
return err
204207
}
205208

206-
return json.Unmarshal([]byte(configStr), &app.Config)
209+
if len(configStr) > 0 {
210+
err = json.Unmarshal([]byte(configStr), &app.Config)
211+
if err != nil {
212+
return err
213+
}
214+
}
215+
216+
return nil
207217
}
208218

209219
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 {
410420
return err
411421
}
412422

413-
if headerStr == "" {
414-
return models.ErrRoutesNotFound
423+
if len(headerStr) > 0 {
424+
err = json.Unmarshal([]byte(headerStr), &route.Headers)
425+
if err != nil {
426+
return err
427+
}
415428
}
416429

417-
if err := json.Unmarshal([]byte(headerStr), &route.Headers); err != nil {
418-
return err
430+
if len(configStr) > 0 {
431+
err = json.Unmarshal([]byte(configStr), &route.Config)
432+
if err != nil {
433+
return err
434+
}
419435
}
420-
return json.Unmarshal([]byte(configStr), &route.Config)
436+
437+
return nil
421438
}
422439

423440
func (ds *PostgresDatastore) GetRoute(ctx context.Context, appName, routePath string) (*models.Route, error) {

0 commit comments

Comments
 (0)