Skip to content

Commit 3542340

Browse files
committed
toolkit: convert WFN.Value to value receiver
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]>
1 parent 2a85dcb commit 3542340

File tree

3 files changed

+403
-1
lines changed

3 files changed

+403
-1
lines changed

toolkit/types/cpe/marshaling.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (w *WFN) Scan(src interface{}) (err error) {
5050
}
5151

5252
// Value implements [driver.Valuer].
53-
func (w *WFN) Value() (driver.Value, error) {
53+
func (w WFN) Value() (driver.Value, error) {
5454
switch err := w.Valid(); {
5555
case err == nil:
5656
case errors.Is(err, ErrUnset):

toolkit/types/cpe/marshaling_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
package cpe
22

33
import (
4+
"database/sql/driver"
5+
"fmt"
46
"testing"
57

68
"github.com/google/go-cmp/cmp"
79
)
810

11+
func TestConvertValue(t *testing.T) {
12+
w := MustUnbind(`cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`)
13+
var testcases = []struct {
14+
name string
15+
wfn any
16+
want string
17+
wantErr error
18+
}{
19+
{
20+
name: "test value",
21+
wfn: w,
22+
want: `cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`,
23+
wantErr: nil,
24+
},
25+
{
26+
name: "test pointer",
27+
wfn: &w,
28+
want: `cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*`,
29+
wantErr: nil,
30+
},
31+
}
32+
dpc := driver.DefaultParameterConverter
33+
for _, tt := range testcases {
34+
t.Run(tt.name, func(t *testing.T) {
35+
val, err := dpc.ConvertValue(tt.wfn)
36+
if err != tt.wantErr {
37+
t.Fatal(err)
38+
}
39+
v := val.(string)
40+
if v != tt.want {
41+
t.Fatalf("wanted %q but got %q", tt.want, v)
42+
}
43+
fmt.Println()
44+
})
45+
}
46+
}
47+
948
func TestMarshal(t *testing.T) {
1049
t.Parallel()
1150
var names = []string{

0 commit comments

Comments
 (0)