aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/score.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 13:23:44 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 13:23:44 -0500
commita40213480929a93a78239053a40a6ee93547c063 (patch)
treeb848aac63f0695b18a2f58928c7ff4d2a7eb4184 /internal/feed/score.go
parent465f427a04c2f10bc5163f6bceef13693f7e99e6 (diff)
downloadcvez-a40213480929a93a78239053a40a6ee93547c063.tar.bz2
cvez-a40213480929a93a78239053a40a6ee93547c063.zip
internal/feed: add score, score tests
Diffstat (limited to 'internal/feed/score.go')
-rw-r--r--internal/feed/score.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/feed/score.go b/internal/feed/score.go
new file mode 100644
index 0000000..4e7da57
--- /dev/null
+++ b/internal/feed/score.go
@@ -0,0 +1,34 @@
+package feed
+
+import (
+ "encoding/json"
+ "fmt"
+ "math"
+ "strconv"
+)
+
+// CVSS score
+type Score float32
+
+// Unmarshal CVSS score from JSON.
+func (me *Score) UnmarshalJSON(b []byte) error {
+ // decode float, check for error
+ var v float32
+ if err := json.Unmarshal(b, &v); err != nil {
+ return err
+ }
+
+ // check score
+ if v < 0.0 || v > 10.0 {
+ return fmt.Errorf("CVSS score out of bounds: %2.1f", v)
+ }
+
+ // save result, return success
+ *me = Score(v)
+ return nil
+}
+
+func (me Score) String() string {
+ val := math.Trunc(10.0 * float64(me)) / 10.0
+ return strconv.FormatFloat(val, 'f', 1, 64)
+}