11use std:: collections:: HashMap ;
22
3- use serde:: { Deserialize , Deserializer } ;
3+ use serde:: { Deserialize , Deserializer , Serialize } ;
4+ use tracing:: warn;
45
56use crate :: cas_server:: WithInstanceName ;
67
@@ -11,23 +12,6 @@ enum WithInstanceNameBackCompat<T> {
1112 Vec ( Vec < WithInstanceName < T > > ) ,
1213}
1314
14- const DEPRECATION_MESSAGE : & str = r#"
15- WARNING: Using deprecated map format for services. Please migrate to the new array format:
16- // Old:
17- "cas": {
18- "main": {
19- "cas_store": "STORE_NAME"
20- }
21- }
22- // New:
23- "cas": [
24- {
25- "instance_name": "main",
26- "cas_store": "STORE_NAME"
27- }
28- ]
29- "# ;
30-
3115/// Use `#[serde(default, deserialize_with = "backcompat::opt_vec_named_config")]` for backwards
3216/// compatibility with map-based access. A deprecation warning will be written to stderr if the
3317/// old format is used.
@@ -36,22 +20,36 @@ pub(crate) fn opt_vec_with_instance_name<'de, D, T>(
3620) -> Result < Option < Vec < WithInstanceName < T > > > , D :: Error >
3721where
3822 D : Deserializer < ' de > ,
39- T : Deserialize < ' de > ,
23+ T : Deserialize < ' de > + Serialize ,
4024{
4125 let Some ( back_compat) = Option :: deserialize ( deserializer) ? else {
4226 return Ok ( None ) ;
4327 } ;
4428
4529 match back_compat {
4630 WithInstanceNameBackCompat :: Map ( map) => {
47- eprintln ! ( "{DEPRECATION_MESSAGE}" ) ;
48- let vec = map
31+ // TODO(palfrey): ideally this would be serde_json5::to_string_pretty but that doesn't exist
32+ // JSON is close enough to be workable for now
33+ let serde_map = serde_json:: to_string_pretty ( & map) . expect ( "valid map" ) ;
34+ let vec: Vec < WithInstanceName < T > > = map
4935 . into_iter ( )
5036 . map ( |( instance_name, config) | WithInstanceName {
5137 instance_name,
5238 config,
5339 } )
5440 . collect ( ) ;
41+ warn ! (
42+ r"WARNING: Using deprecated map format for services. Please migrate to the new array format:
43+ // Old:
44+ {}
45+ // New:
46+ {}
47+ " ,
48+ serde_map,
49+ // TODO(palfrey): ideally this would be serde_json5::to_string_pretty but that doesn't exist
50+ // JSON is close enough to be workable for now
51+ serde_json:: to_string_pretty( & vec) . expect( "valid new map" )
52+ ) ;
5553 Ok ( Some ( vec) )
5654 }
5755 WithInstanceNameBackCompat :: Vec ( vec) => Ok ( Some ( vec) ) ,
@@ -61,21 +59,23 @@ where
6159#[ cfg( test) ]
6260mod tests {
6361 use serde_json:: json;
62+ use tracing_test:: traced_test;
6463
6564 use super :: * ;
6665
67- #[ derive( Debug , Deserialize , PartialEq ) ]
66+ #[ derive( Debug , Deserialize , Serialize , PartialEq ) ]
6867 struct PartialConfig {
6968 store : String ,
7069 }
7170
72- #[ derive( Debug , Deserialize , PartialEq ) ]
71+ #[ derive( Debug , Deserialize , Serialize , PartialEq ) ]
7372 struct FullConfig {
7473 #[ serde( default , deserialize_with = "opt_vec_with_instance_name" ) ]
7574 cas : Option < Vec < WithInstanceName < PartialConfig > > > ,
7675 }
7776
7877 #[ test]
78+ #[ traced_test]
7979 fn test_configs_deserialization ( ) {
8080 let old_format = json ! ( {
8181 "cas" : {
@@ -109,6 +109,17 @@ mod tests {
109109 }
110110
111111 assert_eq ! ( old_format, new_format) ;
112+
113+ logs_assert ( |lines : & [ & str ] | {
114+ if lines. len ( ) != 1 {
115+ return Err ( format ! ( "Expected 1 log line, got: {lines:?}" ) ) ;
116+ }
117+ let line = lines[ 0 ] ;
118+ // TODO(palfrey): we should be checking the whole thing, but tracing-test is broken with multi-line items
119+ // See https://github.com/dbrgn/tracing-test/issues/48
120+ assert ! ( line. ends_with( "WARNING: Using deprecated map format for services. Please migrate to the new array format:" ) ) ;
121+ Ok ( ( ) )
122+ } ) ;
112123 }
113124
114125 #[ test]
0 commit comments