github.com/goki/ki@v1.1.17/bools/bools.go (about) 1 // Copyright (c) 2022, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 package bools does conversion to / from booleans and other go standard types 7 */ 8 package bools 9 10 // A Booler is a type that can return 11 // its value as a boolean value 12 type Booler interface { 13 // Bool returns the boolean 14 // representation of the value 15 Bool() bool 16 } 17 18 // A BoolSetter is a Booler that can also 19 // set its value from a bool value 20 type BoolSetter interface { 21 Booler 22 // SetBool sets the value from the 23 // boolean representation of the value 24 SetBool(val bool) 25 } 26 27 // ToFloat32 converts a bool to a 1 (true) or 0 (false) 28 func ToFloat32(b bool) float32 { 29 if b { 30 return 1 31 } 32 return 0 33 } 34 35 // ToFloat64 converts a bool to a 1 (true) or 0 (false) 36 func ToFloat64(b bool) float64 { 37 if b { 38 return 1 39 } 40 return 0 41 } 42 43 // ToInt converts a bool to a 1 (true) or 0 (false) 44 func ToInt(b bool) int { 45 if b { 46 return 1 47 } 48 return 0 49 } 50 51 // ToInt32 converts a bool to a 1 (true) or 0 (false) 52 func ToInt32(b bool) int32 { 53 if b { 54 return 1 55 } 56 return 0 57 } 58 59 // ToInt64 converts a bool to a 1 (true) or 0 (false) 60 func ToInt64(b bool) int64 { 61 if b { 62 return 1 63 } 64 return 0 65 } 66 67 // ToString converts a bool to "true" or "false" string 68 func ToString(b bool) string { 69 if b { 70 return "true" 71 } 72 return "false" 73 } 74 75 ////////////////////////////////////////////// 76 77 // FromFloat32 converts value to a bool, 0 = false, else true 78 func FromFloat32(v float32) bool { 79 if v == 0 { 80 return false 81 } 82 return true 83 } 84 85 // FromFloat64 converts value to a bool, 0 = false, else true 86 func FromFloat64(v float64) bool { 87 if v == 0 { 88 return false 89 } 90 return true 91 } 92 93 // FromInt converts value to a bool, 0 = false, else true 94 func FromInt(v int) bool { 95 if v == 0 { 96 return false 97 } 98 return true 99 } 100 101 // FromInt32 converts value to a bool, 0 = false, else true 102 func FromInt32(v int32) bool { 103 if v == 0 { 104 return false 105 } 106 return true 107 } 108 109 // FromInt64 converts value to a bool, 0 = false, else true 110 func FromInt64(v int64) bool { 111 if v == 0 { 112 return false 113 } 114 return true 115 } 116 117 // FromString converts string to a bool, "true" = true, else false 118 func FromString(v string) bool { 119 if v == "true" { 120 return true 121 } 122 return false 123 }