Skip to content

Commit dabdc27

Browse files
committed
cpe: switch to new binding implementation
Signed-off-by: Hank Donnay <hdonnay@redhat.com> Change-Id: Icc05f1b5ebaae2f6ac6bc218dfd8a76b6a6a6964
1 parent b649e6e commit dabdc27

4 files changed

Lines changed: 30 additions & 15 deletions

File tree

toolkit/types/cpe/bind.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
// BindFS returns the WFN bound as CPE 2.3 formatted string.
11+
//
12+
// Deprecated: use [WFN.AppendText].
1113
func (w WFN) BindFS() string {
1214
b := strings.Builder{}
1315
b.WriteString(`cpe:2.3`)

toolkit/types/cpe/marshaling.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99

1010
// MarshalText implements [encoding.TextMarshaler].
1111
func (w *WFN) MarshalText() ([]byte, error) {
12-
switch err := w.Valid(); {
13-
case err == nil:
14-
case errors.Is(err, ErrUnset):
15-
return []byte{}, nil
16-
default:
17-
return nil, err
18-
}
19-
return []byte(w.BindFS()), nil
12+
// Guess at a good initial size. Calculated via finding the mean size across
13+
// the CPE Name dictionary and then rounding it up.
14+
//
15+
// zcat testdata/dictionary.list.gz | awk '/^#/{next}/^$/{next}{ct++;sum+=length($0)}END{print sum/ct}'
16+
// 55.9444 = 64
17+
b := make([]byte, 0, 64)
18+
return w.AppendText(b)
2019
}
2120

2221
// UnmarshalText implements [encoding.TextUnmarshaler].

toolkit/types/cpe/wfn.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ func (w WFN) String() string {
204204
case errors.Is(err, ErrUnset):
205205
return ""
206206
}
207-
return w.BindFS()
207+
b, _ := w.AppendText(make([]byte, 0, 64))
208+
if len(b) == 0 {
209+
return ""
210+
}
211+
return string(b)
208212
}
209213

210214
// GoString implements [fmt.GoStringer].

toolkit/types/cpe/wfn_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cpe
22

33
import (
44
"bufio"
5+
"bytes"
56
"compress/gzip"
67
"errors"
78
"fmt"
@@ -451,22 +452,31 @@ func TestDictionary(t *testing.T) {
451452
defer gz.Close()
452453

453454
s := bufio.NewScanner(gz)
455+
defer func() {
456+
if err := s.Err(); err != nil {
457+
t.Error(err)
458+
}
459+
}()
460+
back := make([]byte, 0, 128)
454461
for i := 1; s.Scan(); i++ {
455462
in := s.Text()
456463
if len(in) == 0 || strings.HasPrefix(in, "#") {
457464
continue
458465
}
459-
wfn, err := Unbind(in)
466+
467+
var wfn WFN
468+
if err := wfn.UnmarshalFS(in); err != nil {
469+
t.Fatalf("%v: %#q", err, in)
470+
}
471+
b, err := wfn.AppendText(back[:0])
460472
if err != nil {
461473
t.Fatalf("%v: %#q", err, in)
462474
}
463-
if got, want := wfn.BindFS(), s.Text(); got != want {
464-
t.Logf(fmt, i, in, got, want)
475+
476+
if got, want := b, s.Bytes(); !bytes.Equal(got, want) {
477+
t.Logf(fmt, i, in, string(got), string(want))
465478
t.Logf("wfn: %#v", wfn)
466479
t.Fail()
467480
}
468481
}
469-
if err := s.Err(); err != nil {
470-
t.Error(err)
471-
}
472482
}

0 commit comments

Comments
 (0)