@@ -45,54 +45,33 @@ public void Override(IReadOnlyDictionary<object, object?> value)
4545
4646 private static void Merge ( IDictionary < object , object ? > dictionary , IReadOnlyDictionary < object , object ? > anotherDictionary )
4747 {
48- foreach ( var item in dictionary )
48+ foreach ( var ( key , sourceValue ) in anotherDictionary )
4949 {
50- switch ( item . Value )
50+ if ( dictionary . TryGetValue ( key , out var currentValue )
51+ && currentValue is IDictionary < object , object ? > currentDictionary
52+ && sourceValue is IReadOnlyDictionary < object , object ? > sourceDictionary )
5153 {
52- case IDictionary < object , object ? > anotherDictionaryValue :
53- {
54- if ( anotherDictionary . TryGetValue ( item . Key , out var value ) && value is IReadOnlyDictionary < object , object ? > dictionaryValue )
55- {
56- Merge ( anotherDictionaryValue , dictionaryValue ) ;
57- }
58-
59- break ;
60- }
61- default :
62- {
63- if ( anotherDictionary . TryGetValue ( item . Key , out var value ) )
64- {
65- dictionary [ item . Key ] = value ;
66- }
67-
68- break ;
69- }
54+ Merge ( currentDictionary , sourceDictionary ) ;
55+ continue ;
7056 }
57+
58+ dictionary [ key ] = sourceValue is IReadOnlyDictionary < object , object ? > nestedDictionary
59+ ? CloneDictionary ( nestedDictionary )
60+ : sourceValue ;
7161 }
62+ }
7263
73- foreach ( var item in anotherDictionary )
74- {
75- switch ( item . Value )
76- {
77- case IReadOnlyDictionary < object , object ? > when dictionary . ContainsKey ( item . Key ) :
78- continue ;
79- case IReadOnlyDictionary < object , object ? > dictionaryValue :
80- {
81- Dictionary < object , object ? > anotherDictionaryValue = [ ] ;
82- Merge ( anotherDictionaryValue , dictionaryValue ) ;
83- dictionary . Add ( item . Key , anotherDictionaryValue ) ;
84- break ;
85- }
86- default :
87- {
88- if ( ! dictionary . ContainsKey ( item . Key ) )
89- {
90- dictionary . Add ( item . Key , item . Value ) ;
91- }
64+ private static Dictionary < object , object ? > CloneDictionary ( IReadOnlyDictionary < object , object ? > dictionary )
65+ {
66+ Dictionary < object , object ? > cloned = [ ] ;
9267
93- break ;
94- }
95- }
68+ foreach ( var ( key , value ) in dictionary )
69+ {
70+ cloned [ key ] = value is IReadOnlyDictionary < object , object ? > nestedDictionary
71+ ? CloneDictionary ( nestedDictionary )
72+ : value ;
9673 }
74+
75+ return cloned ;
9776 }
9877}
0 commit comments