3838//! and a `KvsValue` as second parameter. Either `KvsValue::Number(123.0)` or `123.0` can be
3939//! used as there will be an auto-Into performed when calling the function.
4040//!
41- //! To read a value call [`Kvs::get_value::<T> `](Kvs::get_value) with the `key` as first
42- //! parameter. `T` represents the type to read and can be `f64`, `bool`, `String`, `()`,
43- //! `Vec<KvsValue>`, `HashMap<String, KvsValue` or `KvsValue`. Also `let value: f64 =
44- //! kvs.get_value ()` can be used.
41+ //! To read a value call [`Kvs::get_value`](Kvs::get_value) or [`Kvs::get_value_as::<T>`](Kvs::get_value_as)
42+ //! with the `key` as first parameter. `T` represents the type to read and can be `f64`, `bool`, `String`, `()`,
43+ //! `Vec<KvsValue>`, `HashMap<String, KvsValue> ` or `KvsValue`.
44+ //! Also `let value: f64 = kvs.get_value_as ()` can be used.
4545//!
4646//! If a `key` isn't available in the KVS a lookup into the defaults storage will be performed and
4747//! if the `value` is found the default will be returned. The default value isn't stored when
@@ -462,7 +462,8 @@ pub trait KvsApi {
462462 fn reset ( & self ) -> Result < ( ) , ErrorCode > ;
463463 fn get_all_keys ( & self ) -> Result < Vec < String > , ErrorCode > ;
464464 fn key_exists ( & self , key : & str ) -> Result < bool , ErrorCode > ;
465- fn get_value < T > ( & self , key : & str ) -> Result < T , ErrorCode >
465+ fn get_value ( & self , key : & str ) -> Result < KvsValue , ErrorCode > ;
466+ fn get_value_as < T > ( & self , key : & str ) -> Result < T , ErrorCode >
466467 where
467468 for < ' a > T : TryFrom < & ' a KvsValue > + Clone ,
468469 for < ' a > <T as TryFrom < & ' a KvsValue > >:: Error : std:: fmt:: Debug ;
@@ -781,6 +782,31 @@ impl KvsApi for Kvs {
781782 Ok ( self . kvs . lock ( ) ?. contains_key ( key) )
782783 }
783784
785+ /// Get the assigned value for a given key
786+ ///
787+ /// # Features
788+ /// * `FEAT_REQ__KVS__default_values`
789+ ///
790+ /// # Parameters
791+ /// * `key`: Key to retrieve the value from
792+ ///
793+ /// # Return Value
794+ /// * Ok: Type specific value if key was found
795+ /// * `ErrorCode::MutexLockFailed`: Mutex locking failed
796+ /// * `ErrorCode::KeyNotFound`: Key wasn't found in KVS nor in defaults
797+ fn get_value ( & self , key : & str ) -> Result < KvsValue , ErrorCode > {
798+ let kvs = self . kvs . lock ( ) ?;
799+
800+ if let Some ( value) = kvs. get ( key) {
801+ Ok ( value. clone ( ) )
802+ } else if let Some ( value) = self . default . get ( key) {
803+ Ok ( value. clone ( ) )
804+ } else {
805+ eprintln ! ( "error: get_value could not find key: {key}" ) ;
806+ Err ( ErrorCode :: KeyNotFound )
807+ }
808+ }
809+
784810 /// Get the assigned value for a given key
785811 ///
786812 /// See [Variants](https://docs.rs/tinyjson/latest/tinyjson/enum.JsonValue.html#variants) for
@@ -797,7 +823,7 @@ impl KvsApi for Kvs {
797823 /// * `ErrorCode::MutexLockFailed`: Mutex locking failed
798824 /// * `ErrorCode::ConversionFailed`: Type conversion failed
799825 /// * `ErrorCode::KeyNotFound`: Key wasn't found in KVS nor in defaults
800- fn get_value < T > ( & self , key : & str ) -> Result < T , ErrorCode >
826+ fn get_value_as < T > ( & self , key : & str ) -> Result < T , ErrorCode >
801827 where
802828 for < ' a > T : TryFrom < & ' a KvsValue > + std:: clone:: Clone ,
803829 for < ' a > <T as TryFrom < & ' a KvsValue > >:: Error : std:: fmt:: Debug ,
@@ -1431,7 +1457,28 @@ mod tests {
14311457 . unwrap ( ) ,
14321458 ) ;
14331459 let _ = kvs. set_value ( "test" , KvsValue :: Number ( 123.0 ) ) ;
1434- let value = kvs. get_value :: < f64 > ( "test" ) ;
1460+ let value = kvs. get_value ( "test" ) . unwrap ( ) ;
1461+ assert_eq ! (
1462+ * value. get:: <f64 >( ) . unwrap( ) ,
1463+ 123.0 ,
1464+ "Expected to retrieve the inserted value"
1465+ ) ;
1466+ }
1467+
1468+ #[ test]
1469+ fn test_get_value_as ( ) {
1470+ let dir = tempdir ( ) . unwrap ( ) ;
1471+ let dir_path = dir. path ( ) . to_string_lossy ( ) . to_string ( ) ;
1472+
1473+ let instance_id = InstanceId :: new ( 0 ) ;
1474+ let kvs = Arc :: new (
1475+ KvsBuilder :: < Kvs > :: new ( instance_id. clone ( ) )
1476+ . dir ( dir_path. clone ( ) )
1477+ . build ( )
1478+ . unwrap ( ) ,
1479+ ) ;
1480+ let _ = kvs. set_value ( "test" , KvsValue :: Number ( 123.0 ) ) ;
1481+ let value = kvs. get_value_as :: < f64 > ( "test" ) ;
14351482 assert_eq ! (
14361483 value. unwrap( ) ,
14371484 123.0 ,
@@ -1499,14 +1546,14 @@ mod tests {
14991546 let _ = kvs. set_value ( "test" , KvsValue :: Number ( 123.0f64 ) ) ;
15001547
15011548 // stored value: should return ConversionFailed
1502- let result = kvs. get_value :: < u64 > ( "test" ) ;
1549+ let result = kvs. get_value_as :: < u64 > ( "test" ) ;
15031550 assert ! (
15041551 matches!( result, Err ( ErrorCode :: ConversionFailed ) ) ,
15051552 "Expected ConversionFailed for stored value"
15061553 ) ;
15071554
15081555 // default value: should return ConversionFailed
1509- let result = kvs. get_value :: < u64 > ( "bool1" ) ;
1556+ let result = kvs. get_value_as :: < u64 > ( "bool1" ) ;
15101557 assert ! (
15111558 matches!( result, Err ( ErrorCode :: ConversionFailed ) ) ,
15121559 "Expected ConversionFailed for default value"
@@ -1527,8 +1574,8 @@ mod tests {
15271574 )
15281575 . unwrap ( ) ;
15291576 let _ = kvs. set_value ( "direct" , KvsValue :: String ( "abc" . to_string ( ) ) ) ;
1530- let value = kvs. get_value :: < String > ( "direct" ) ;
1531- assert_eq ! ( value. unwrap( ) , "abc" ) ;
1577+ let value = kvs. get_value ( "direct" ) . unwrap ( ) ;
1578+ assert_eq ! ( value. get :: < String > ( ) . unwrap( ) , "abc" ) ;
15321579 }
15331580
15341581 #[ test]
@@ -1545,10 +1592,10 @@ mod tests {
15451592 )
15461593 . unwrap ( ) ;
15471594 let _ = kvs. set_value ( "reset" , KvsValue :: Number ( 1.0 ) ) ;
1548- assert ! ( kvs. get_value:: < f64 > ( "reset" ) . is_ok( ) ) ;
1595+ assert ! ( kvs. get_value( "reset" ) . is_ok( ) ) ;
15491596 kvs. reset ( ) . unwrap ( ) ;
15501597 assert ! ( matches!(
1551- kvs. get_value:: < f64 > ( "reset" ) ,
1598+ kvs. get_value( "reset" ) ,
15521599 Err ( ErrorCode :: KeyNotFound )
15531600 ) ) ;
15541601 }
0 commit comments