Skip to content

Commit

Permalink
use type switch for sqlite v postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Dec 7, 2023
1 parent 43d8511 commit 9ab070d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion database/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
Expand Down Expand Up @@ -46,7 +47,14 @@ func (r DashReposJSON) Value() (driver.Value, error) {

// Scan - Implement the database/sql scanner interface for DashReposJSON.
func (r *DashReposJSON) Scan(value interface{}) error {
return json.Unmarshal(value.([]byte), &r)
switch v := value.(type) {
case []byte:
return json.Unmarshal(v, &r)
case string:
return json.Unmarshal([]byte(v), &r)
default:
return fmt.Errorf("wrong type for repos: %T", v)
}
}

// Nullify ensures the valid flag for
Expand Down
2 changes: 2 additions & 0 deletions database/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func TestDatabase_DashboardFromLibrary(t *testing.T) {
t.Errorf("DashboardFromLibrary() mismatch (-want +got):\n%s", diff)
}

// test empty uuid results in generated uuid
d.SetID("")

//nolint:staticcheck // linter is lying
Expand All @@ -177,6 +178,7 @@ func TestDatabase_DashboardFromLibrary(t *testing.T) {
t.Errorf("Length is %d", len(got.ID))
}

// test poorly formed uuid results in nil dashboard
d.SetID("123-abc")

got = DashboardFromLibrary(d)
Expand Down

0 comments on commit 9ab070d

Please sign in to comment.