@@ -45,72 +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- case IList :
62- {
63- if ( anotherDictionary . TryGetValue ( item . Key , out var value ) )
64- {
65- dictionary [ item . Key ] = value ;
66- }
67-
68- break ;
69- }
70- default :
71- {
72- if ( anotherDictionary . TryGetValue ( item . Key , out var value ) )
73- {
74- dictionary [ item . Key ] = value ;
75- }
76-
77- break ;
78- }
54+ Merge ( currentDictionary , sourceDictionary ) ;
55+ continue ;
7956 }
80- }
8157
82- foreach ( var item in anotherDictionary )
83- {
84- switch ( item . Value )
85- {
86- case IReadOnlyDictionary < object , object ? > when dictionary . ContainsKey ( item . Key ) :
87- continue ;
88- case IReadOnlyDictionary < object , object ? > dictionaryValue :
89- {
90- Dictionary < object , object ? > anotherDictionaryValue = [ ] ;
91- Merge ( anotherDictionaryValue , dictionaryValue ) ;
92- dictionary . Add ( item . Key , anotherDictionaryValue ) ;
93- break ;
94- }
95- case IList :
96- {
97- if ( ! dictionary . ContainsKey ( item . Key ) )
98- {
99- dictionary . Add ( item . Key , item . Value ) ;
100- }
58+ dictionary [ key ] = sourceValue is IReadOnlyDictionary < object , object ? > nestedDictionary
59+ ? CloneDictionary ( nestedDictionary )
60+ : sourceValue ;
61+ }
62+ }
10163
102- break ;
103- }
104- default :
105- {
106- if ( ! dictionary . ContainsKey ( item . Key ) )
107- {
108- dictionary . Add ( item . Key , item . Value ) ;
109- }
64+ private static Dictionary < object , object ? > CloneDictionary ( IReadOnlyDictionary < object , object ? > dictionary )
65+ {
66+ Dictionary < object , object ? > cloned = [ ] ;
11067
111- break ;
112- }
113- }
68+ foreach ( var ( key , value ) in dictionary )
69+ {
70+ cloned [ key ] = value is IReadOnlyDictionary < object , object ? > nestedDictionary
71+ ? CloneDictionary ( nestedDictionary )
72+ : value ;
11473 }
74+
75+ return cloned ;
11576 }
11677}
0 commit comments