Skip to content

Commit

Permalink
toolkit: convert WFN.Value to value receiver
Browse files Browse the repository at this point in the history
When using the go sql driver WFN will not be recognized as a
driver.Valuer (only *WFN). As WFN is direct value in claircore's structs
this makes it fiddly, the change allows both pointer and value to
benefit from Value().

Signed-off-by: crozzy <[email protected]>
  • Loading branch information
crozzy committed Mar 6, 2024
1 parent 2a85dcb commit 3542340
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 1 deletion.
2 changes: 1 addition & 1 deletion toolkit/types/cpe/marshaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (w *WFN) Scan(src interface{}) (err error) {
}

// Value implements [driver.Valuer].
func (w *WFN) Value() (driver.Value, error) {
func (w WFN) Value() (driver.Value, error) {
switch err := w.Valid(); {
case err == nil:
case errors.Is(err, ErrUnset):
Expand Down
39 changes: 39 additions & 0 deletions toolkit/types/cpe/marshaling_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
package cpe

import (
"database/sql/driver"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestConvertValue(t *testing.T) {
w := MustUnbind(`cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`)
var testcases = []struct {
name string
wfn any
want string
wantErr error
}{
{
name: "test value",
wfn: w,
want: `cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`,
wantErr: nil,
},
{
name: "test pointer",
wfn: &w,
want: `cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`,
wantErr: nil,
},
}
dpc := driver.DefaultParameterConverter
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
val, err := dpc.ConvertValue(tt.wfn)
if err != tt.wantErr {
t.Fatal(err)
}
v := val.(string)
if v != tt.want {
t.Fatalf("wanted %q but got %q", tt.want, v)
}
fmt.Println()
})
}
}

func TestMarshal(t *testing.T) {
t.Parallel()
var names = []string{
Expand Down
Loading

0 comments on commit 3542340

Please sign in to comment.