github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/types/bool.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 ) 6 7 type BoolValue struct { 8 BaseAttribute 9 value bool 10 } 11 12 func (b BoolValue) MarshalJSON() ([]byte, error) { 13 return json.Marshal(map[string]interface{}{ 14 "value": b.value, 15 "metadata": b.metadata, 16 }) 17 } 18 19 func (b *BoolValue) UnmarshalJSON(data []byte) error { 20 var keys map[string]interface{} 21 if err := json.Unmarshal(data, &keys); err != nil { 22 return err 23 } 24 if keys["value"] != nil { 25 b.value = keys["value"].(bool) 26 } 27 if keys["metadata"] != nil { 28 raw, err := json.Marshal(keys["metadata"]) 29 if err != nil { 30 return err 31 } 32 var m Metadata 33 if err := json.Unmarshal(raw, &m); err != nil { 34 return err 35 } 36 b.metadata = m 37 } 38 return nil 39 } 40 41 func Bool(value bool, metadata Metadata) BoolValue { 42 return BoolValue{ 43 value: value, 44 BaseAttribute: BaseAttribute{metadata: metadata}, 45 } 46 } 47 48 func BoolDefault(value bool, metadata Metadata) BoolValue { 49 b := Bool(value, metadata) 50 b.BaseAttribute.metadata.isDefault = true 51 return b 52 } 53 54 func BoolUnresolvable(m Metadata) BoolValue { 55 b := Bool(false, m) 56 b.BaseAttribute.metadata.isUnresolvable = true 57 return b 58 } 59 60 func BoolExplicit(value bool, metadata Metadata) BoolValue { 61 b := Bool(value, metadata) 62 b.BaseAttribute.metadata.isExplicit = true 63 return b 64 } 65 66 func (b BoolValue) Value() bool { 67 return b.value 68 } 69 70 func (b BoolValue) GetRawValue() interface{} { 71 return b.value 72 } 73 74 func (b BoolValue) IsTrue() bool { 75 if b.metadata.isUnresolvable { 76 return false 77 } 78 return b.Value() 79 } 80 81 func (b BoolValue) IsFalse() bool { 82 if b.metadata.isUnresolvable { 83 return false 84 } 85 return !b.Value() 86 } 87 88 func (s BoolValue) ToRego() interface{} { 89 m := s.metadata.ToRego().(map[string]interface{}) 90 m["value"] = s.Value() 91 return m 92 }