gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/KeyValuePair.go (about) 1 package corestr 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "gitlab.com/evatix-go/core/constants" 9 "gitlab.com/evatix-go/core/constants/bitsize" 10 "gitlab.com/evatix-go/core/coredata/corejson" 11 ) 12 13 type KeyValuePair struct { 14 Key, Value string 15 } 16 17 func (it KeyValuePair) KeyName() string { 18 return it.Key 19 } 20 21 func (it KeyValuePair) VariableName() string { 22 return it.Key 23 } 24 25 func (it KeyValuePair) ValueString() string { 26 return it.Value 27 } 28 29 func (it KeyValuePair) IsVariableNameEqual(name string) bool { 30 return it.Key == name 31 } 32 33 func (it KeyValuePair) IsValueEqual(valueString string) bool { 34 return it.Value == valueString 35 } 36 37 func (it KeyValuePair) Json() corejson.Result { 38 return corejson.New(it) 39 } 40 41 func (it KeyValuePair) JsonPtr() *corejson.Result { 42 return corejson.NewPtr(it) 43 } 44 45 func (it KeyValuePair) Serialize() ([]byte, error) { 46 return corejson.NewPtr(it).Raw() 47 } 48 49 func (it KeyValuePair) SerializeMust() (jsonBytes []byte) { 50 return corejson.NewPtr(it).RawMust() 51 } 52 53 func (it KeyValuePair) Compile() string { 54 return it.String() 55 } 56 57 func (it *KeyValuePair) IsKeyEmpty() bool { 58 return it.Key == "" 59 } 60 61 func (it *KeyValuePair) IsValueEmpty() bool { 62 return it.Value == "" 63 } 64 65 func (it *KeyValuePair) HasKey() bool { 66 return it.Key != "" 67 } 68 69 func (it *KeyValuePair) HasValue() bool { 70 return it.Value != "" 71 } 72 73 func (it *KeyValuePair) IsKeyValueEmpty() bool { 74 return it.Key == "" && it.Value == "" 75 } 76 77 func (it *KeyValuePair) TrimKey() string { 78 return strings.TrimSpace(it.Key) 79 } 80 81 func (it *KeyValuePair) TrimValue() string { 82 return strings.TrimSpace(it.Value) 83 } 84 85 func (it *KeyValuePair) ValueBool() bool { 86 if it.Value == "" { 87 return false 88 } 89 90 toBool, err := strconv.ParseBool(it.Value) 91 92 if err != nil { 93 return false 94 } 95 96 return toBool 97 } 98 99 func (it *KeyValuePair) ValueInt(defaultInteger int) int { 100 toInt, err := strconv.Atoi(it.Value) 101 102 if err != nil { 103 return defaultInteger 104 } 105 106 return toInt 107 } 108 109 func (it *KeyValuePair) ValueDefInt() int { 110 toInt, err := strconv.Atoi(it.Value) 111 112 if err != nil { 113 return constants.Zero 114 } 115 116 return toInt 117 } 118 119 func (it *KeyValuePair) ValueByte(defVal byte) byte { 120 toInt, err := strconv.Atoi(it.Value) 121 122 if err != nil || toInt > constants.MaxUnit8AsInt { 123 return defVal 124 } 125 126 return byte(toInt) 127 } 128 129 func (it *KeyValuePair) ValueDefByte() byte { 130 toInt, err := strconv.Atoi(it.Value) 131 132 if err != nil || toInt > constants.MaxUnit8AsInt { 133 return constants.Zero 134 } 135 136 return byte(toInt) 137 } 138 139 func (it *KeyValuePair) ValueFloat64(defVal float64) float64 { 140 toFloat, err := strconv.ParseFloat(it.Value, bitsize.Of64) 141 142 if err != nil { 143 return defVal 144 } 145 146 return toFloat 147 } 148 149 func (it *KeyValuePair) ValueDefFloat64() float64 { 150 return it.ValueFloat64(constants.Zero) 151 } 152 153 func (it *KeyValuePair) ValueValid() ValidValue { 154 return ValidValue{ 155 Value: it.Value, 156 IsValid: true, 157 Message: constants.EmptyString, 158 } 159 } 160 161 func (it *KeyValuePair) ValueValidOptions( 162 isValid bool, 163 message string, 164 ) ValidValue { 165 return ValidValue{ 166 Value: it.Value, 167 IsValid: isValid, 168 Message: message, 169 } 170 } 171 172 func (it *KeyValuePair) Is(key, val string) bool { 173 return it != nil && it.Key == key && it.Value == val 174 } 175 176 func (it *KeyValuePair) IsKey(key string) bool { 177 return it != nil && it.Key == key 178 } 179 180 func (it *KeyValuePair) IsVal(val string) bool { 181 return it != nil && it.Value == val 182 } 183 184 func (it *KeyValuePair) IsKeyValueAnyEmpty() bool { 185 return it == nil || it.Key == "" || it.Value == "" 186 } 187 188 // FormatString 189 // 190 // First %v is key and next one is value 191 func (it *KeyValuePair) FormatString(format string) string { 192 return fmt.Sprintf( 193 format, 194 it.Key, 195 it.Value) 196 } 197 198 func (it *KeyValuePair) String() string { 199 return fmt.Sprintf( 200 keyValuePrintFormat, 201 it.Key, 202 it.Value) 203 } 204 205 func (it *KeyValuePair) Clear() { 206 if it == nil { 207 return 208 } 209 210 it.Key = "" 211 it.Value = "" 212 } 213 214 func (it *KeyValuePair) Dispose() { 215 if it == nil { 216 return 217 } 218 219 it.Clear() 220 }