diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-01 13:36:20 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-01 13:36:20 -0500 |
commit | ee4857cdd5416686e36624621b3460f1bc334c54 (patch) | |
tree | cf40832f85dccc666278fe4839486ec2e6b30f8a /internal | |
parent | a40213480929a93a78239053a40a6ee93547c063 (diff) | |
download | cvez-ee4857cdd5416686e36624621b3460f1bc334c54.tar.bz2 cvez-ee4857cdd5416686e36624621b3460f1bc334c54.zip |
internal/feed: represent scores as uint8
Diffstat (limited to 'internal')
-rw-r--r-- | internal/feed/score.go | 8 | ||||
-rw-r--r-- | internal/feed/score_test.go | 34 |
2 files changed, 21 insertions, 21 deletions
diff --git a/internal/feed/score.go b/internal/feed/score.go index 4e7da57..051522f 100644 --- a/internal/feed/score.go +++ b/internal/feed/score.go @@ -8,12 +8,12 @@ import ( ) // CVSS score -type Score float32 +type Score uint8 // Unmarshal CVSS score from JSON. func (me *Score) UnmarshalJSON(b []byte) error { // decode float, check for error - var v float32 + var v float64 if err := json.Unmarshal(b, &v); err != nil { return err } @@ -24,11 +24,11 @@ func (me *Score) UnmarshalJSON(b []byte) error { } // save result, return success - *me = Score(v) + *me = Score(uint8(math.Trunc(10.0 * v))) return nil } func (me Score) String() string { - val := math.Trunc(10.0 * float64(me)) / 10.0 + val := float64(me) / 10.0 return strconv.FormatFloat(val, 'f', 1, 64) } diff --git a/internal/feed/score_test.go b/internal/feed/score_test.go index 0bda1b2..2baa7ab 100644 --- a/internal/feed/score_test.go +++ b/internal/feed/score_test.go @@ -43,14 +43,14 @@ func TestScoreUnmarshalInvalidValues(t *testing.T) { func TestScoreUnmarshalValidValues(t *testing.T) { tests := []struct { val string - exp float32 + exp uint8 } { - { `0.0`, 0.0 }, - { `0.1`, 0.1 }, - { `1.2`, 1.2 }, - { `5.9`, 5.9 }, - { `9.9`, 9.9 }, - { `10.0`, 10.0 }, + { `0.0`, 0 }, + { `0.1`, 1 }, + { `1.2`, 12 }, + { `5.9`, 59 }, + { `9.9`, 99 }, + { `10.0`, 100 }, } for _, test := range(tests) { @@ -60,8 +60,8 @@ func TestScoreUnmarshalValidValues(t *testing.T) { if err := json.Unmarshal([]byte(test.val), &got); err != nil { t.Error(err) return - } else if float32(got) != test.exp { - t.Errorf("got \"%f\", exp \"%f\"", float32(got), test.exp) + } else if uint8(got) != test.exp { + t.Errorf("got \"%d\", exp \"%d\"", uint8(got), test.exp) } }) } @@ -69,16 +69,16 @@ func TestScoreUnmarshalValidValues(t *testing.T) { func TestScoreString(t *testing.T) { tests := []struct { - val float32 + val uint8 exp string } { - { 0.0, "0.0" }, - { 0.01, "0.0" }, - { 0.09, "0.0" }, - { 1.2222, "1.2" }, - { 5.9, "5.9" }, - { 9.99, "9.9" }, - { 10.0, "10.0" }, + { 0, "0.0" }, + { 1, "0.1" }, + { 9, "0.9" }, + { 12, "1.2" }, + { 59, "5.9" }, + { 99, "9.9" }, + { 100, "10.0" }, } for _, test := range(tests) { |