package feed import ( "encoding/json" "fmt" "math" "strconv" ) // CVSS score type Score uint8 // // create new score from floating point value. // func NewScore(v float64) (Score, error) { // // check score // if v < 0.0 || v > 10.0 { // return Score(0), fmt.Errorf("CVSS score out of bounds: %2.1f", v) // } else { // // convert to score, return success // return Score(uint8(math.Trunc(10.0 * v))), nil // } // } // Unmarshal CVSS score from JSON. func (me *Score) UnmarshalJSON(b []byte) error { // decode float, check for error var v float64 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(uint8(math.Trunc(10.0 * v))) return nil } // Convert to string. func (me Score) String() string { val := float64(me) / 10.0 return strconv.FormatFloat(val, 'f', 1, 64) } // Return floating point representation of score. func (s Score) Float() float32 { return float32(s) / 10.0 } // Marshal score as JSON. func (s Score) MarshalJSON() ([]byte, error) { return json.Marshal(s.Float()) }