go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/explorer/impact.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package explorer 5 6 import ( 7 "encoding/json" 8 "errors" 9 10 "go.mondoo.com/cnquery/checksums" 11 "gopkg.in/yaml.v3" 12 ) 13 14 func (v *Impact) AddBase(base *Impact) { 15 if base == nil { 16 return 17 } 18 19 if v.Scoring == ScoringSystem_SCORING_UNSPECIFIED { 20 v.Scoring = base.Scoring 21 } 22 if v.Value == nil { 23 v.Value = base.Value 24 } 25 if v.Weight < 1 { 26 v.Weight = base.Weight 27 } 28 if v.Action == Action_UNSPECIFIED { 29 v.Action = base.Action 30 } 31 } 32 33 func (v *Impact) Checksum() uint64 { 34 res := checksums.New 35 if v == nil { 36 return uint64(res) 37 } 38 39 res = res.AddUint(uint64(v.Scoring)). 40 AddUint(uint64(v.Weight)). 41 AddUint(uint64(v.Action)) 42 43 if v.Value != nil { 44 res = res.AddUint(uint64(v.Value.Value)) 45 } 46 47 return uint64(res) 48 } 49 50 func (v *Impact) UnmarshalJSON(data []byte) error { 51 var res int32 52 if err := json.Unmarshal(data, &res); err == nil { 53 v.Value = &ImpactValue{Value: res} 54 55 if v.Value.Value < 0 || v.Value.Value > 100 { 56 return errors.New("impact must be between 0 and 100") 57 } 58 return nil 59 } 60 61 type tmp Impact 62 return json.Unmarshal(data, (*tmp)(v)) 63 } 64 65 func (v *ImpactValue) MarshalJSON() ([]byte, error) { 66 if v == nil { 67 return []byte{}, nil 68 } 69 return json.Marshal(v.Value) 70 } 71 72 func (v *ImpactValue) UnmarshalJSON(data []byte) error { 73 var res int32 74 if err := json.Unmarshal(data, &res); err == nil { 75 v.Value = res 76 } else { 77 vInternal := &struct { 78 Value int32 `json:"value"` 79 }{} 80 if err := json.Unmarshal(data, &vInternal); err != nil { 81 return err 82 } 83 v.Value = vInternal.Value 84 } 85 86 if v.Value < 0 || v.Value > 100 { 87 return errors.New("impact must be between 0 and 100") 88 } 89 90 return nil 91 } 92 93 func (s *ScoringSystem) UnmarshalJSON(data []byte) error { 94 // check if we have a number 95 var code int32 96 err := json.Unmarshal(data, &code) 97 if err == nil { 98 *s = ScoringSystem(code) 99 } else { 100 var name string 101 _ = json.Unmarshal(data, &name) 102 103 switch name { 104 case "highest impact": 105 *s = ScoringSystem_WORST 106 case "weighted": 107 *s = ScoringSystem_WEIGHTED 108 case "average", "": 109 *s = ScoringSystem_AVERAGE 110 default: 111 return errors.New("unknown scoring system: " + string(data)) 112 } 113 } 114 return nil 115 } 116 117 func (s *ScoringSystem) UnmarshalYAML(node *yaml.Node) error { 118 // check if we have a number 119 var code int32 120 err := node.Decode(&code) 121 if err == nil { 122 *s = ScoringSystem(code) 123 } else { 124 var name string 125 _ = node.Decode(&name) 126 127 switch name { 128 case "highest impact": 129 *s = ScoringSystem_WORST 130 case "weighted": 131 *s = ScoringSystem_WEIGHTED 132 case "average", "": 133 *s = ScoringSystem_AVERAGE 134 default: 135 return errors.New("unknown scoring system: " + string(name)) 136 } 137 } 138 return nil 139 } 140 141 func (s *ScoringSystem) MarshalYAML() (interface{}, error) { 142 switch *s { 143 case ScoringSystem_WORST: 144 return "highest impact", nil 145 case ScoringSystem_WEIGHTED: 146 return "weighted", nil 147 case ScoringSystem_AVERAGE: 148 return "average", nil 149 default: 150 return *s, nil 151 } 152 }