HGet gets the string value of a hash field. It takes a string key 'k' and a field 'f', and returns the corresponding value and any possible error. Parameters: k: The key of the hash table. f: The field name. Returns: interface{}: The value corresponding to the field, or nil if the field doesn
(key string, field interface{})
| 157 | // - The retrieved byte data is converted back to the corresponding data type. |
| 158 | // - Returns the value corresponding to the field and any possible error. |
| 159 | func (hs *HashStructure) HGet(key string, field interface{}) (interface{}, error) { |
| 160 | // check the key ttl |
| 161 | ttl, _ := hs.TTL(key) |
| 162 | if ttl == -1 { |
| 163 | return nil, _const.ErrKeyIsExpired |
| 164 | } |
| 165 | |
| 166 | // Convert the parameters to bytes |
| 167 | k := stringToBytesWithKey(key) |
| 168 | |
| 169 | // Convert the parameters to bytes |
| 170 | f, err, _ := interfaceToBytes(field) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| 175 | // Check the parameters |
| 176 | if len(k) == 0 || len(f) == 0 { |
| 177 | return nil, _const.ErrKeyIsEmpty |
| 178 | } |
| 179 | |
| 180 | // Find the hash metadata by the given key |
| 181 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | |
| 186 | // If the counter is 0, return nil |
| 187 | if hashMeta.counter == 0 { |
| 188 | return nil, nil |
| 189 | } |
| 190 | |
| 191 | // Create a new HashField |
| 192 | hf := &HashField{ |
| 193 | field: f, |
| 194 | key: k, |
| 195 | version: hashMeta.version, |
| 196 | } |
| 197 | |
| 198 | // Encode the HashField |
| 199 | hfBuf := hf.encodeHashField() |
| 200 | |
| 201 | // Get the field from the database |
| 202 | value, err := hs.db.Get(hfBuf) |
| 203 | if err != nil { |
| 204 | return nil, err |
| 205 | } |
| 206 | |
| 207 | // Get the value type from the hashValueTypes |
| 208 | valueType := hs.hashValueType |
| 209 | |
| 210 | // Values of different types need to be converted to corresponding types |
| 211 | valueToInterface, err := byteToInterface(value, valueType) |
| 212 | if err != nil { |
| 213 | return nil, err |
| 214 | } |
| 215 | return valueToInterface, nil |
| 216 | } |
no test coverage detected