diff options
Diffstat (limited to 'internal/feed/score.go')
-rw-r--r-- | internal/feed/score.go | 34 |
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) +} |