diff options
Diffstat (limited to 'dbstore/dbstore.go')
-rw-r--r-- | dbstore/dbstore.go | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/dbstore/dbstore.go b/dbstore/dbstore.go index 4df9fb9..00b8fe2 100644 --- a/dbstore/dbstore.go +++ b/dbstore/dbstore.go @@ -189,6 +189,31 @@ type CpeSearchRow struct { Rank float32 `json:"rank"` } +// Unmarshal CPE search row from row set. +func unmarshalCpeSearchRow(rows *db_sql.Rows) (CpeSearchRow, error) { + var r CpeSearchRow + var titles string + var refs string + + // get row values + if err := rows.Scan(&r.CpeId, &r.Cpe23, &titles, &refs, &r.Rank); err != nil { + return r, err + } + + // unmarshal titles + if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil { + return r, err + } + + // unmarshal refs + if err := json.Unmarshal([]byte(refs), &r.Refs); err != nil { + return r, err + } + + // return sccess + return r, nil +} + // search CPEs func (me DbStore) CpeSearch( ctx context.Context, @@ -216,28 +241,13 @@ func (me DbStore) CpeSearch( // walk results for rows.Next() { - var sr CpeSearchRow - var titles string - var refs string - - // get row values - err = rows.Scan(&sr.CpeId, &sr.Cpe23, &titles, &refs, &sr.Rank) - if err != nil { - return r, err - } - - // unmarshal titles - if err = json.Unmarshal([]byte(titles), &sr.Titles); err != nil { - return r, err - } - - // unmarshal refs - if err = json.Unmarshal([]byte(refs), &sr.Refs); err != nil { + if sr, err := unmarshalCpeSearchRow(rows); err != nil { + // return error return r, err + } else { + // append to results + r = append(r, sr) } - - // append to results - r = append(r, sr) } // close rows |