github.com/quay/claircore@v1.5.28/datastore/postgres/valuescan.go (about)

     1  package postgres
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/quay/claircore"
     9  )
    10  
    11  // this file implements any Valuer Scanner methods necessary to
    12  // insert custom types into the postgres database.
    13  
    14  // jsonbIndexReport is a type definition for claircore.IndexReport.
    15  // we are able to cast a claircore.IndexReport to jsonbIndexReport
    16  // and obtain the Value/Scan method set
    17  type jsonbIndexReport claircore.IndexReport
    18  
    19  func (sr jsonbIndexReport) Value() (driver.Value, error) {
    20  	return json.Marshal(sr)
    21  }
    22  
    23  func (sr *jsonbIndexReport) Scan(value interface{}) error {
    24  	b, ok := value.([]byte)
    25  	if !ok {
    26  		return fmt.Errorf("failed to type assert IndexReport to []bytes")
    27  	}
    28  
    29  	return json.Unmarshal(b, &sr)
    30  }