Skip to content

Commit 9330bcb

Browse files
committed
feat: array merging
1 parent 29223f4 commit 9330bcb

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

src/Services/Profile/SessionProfileOpencodeConfigFile.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,38 @@ private static JsonObject MergeObjects(JsonObject destination, JsonObject source
123123
{
124124
foreach(var entry in source)
125125
{
126-
if(entry.Value is JsonObject sourceChildObject && destination[entry.Key] is JsonObject destinationChildObject)
127-
{
128-
destination[entry.Key] = MergeObjects(destinationChildObject, sourceChildObject);
129-
continue;
130-
}
126+
destination[entry.Key] = MergeNodes(destination[entry.Key], entry.Value);
127+
}
128+
129+
return destination;
130+
}
131+
132+
private static JsonNode? MergeNodes(JsonNode? destination, JsonNode? source)
133+
{
134+
if(source is null)
135+
{
136+
return null;
137+
}
131138

132-
destination[entry.Key] = entry.Value?.DeepClone();
139+
if(source is JsonObject sourceObject && destination is JsonObject destinationObject)
140+
{
141+
return MergeObjects(destinationObject, sourceObject);
142+
}
143+
144+
if(source is JsonArray sourceArray && destination is JsonArray destinationArray)
145+
{
146+
return MergeArrays(destinationArray, sourceArray);
147+
}
148+
149+
return source.DeepClone();
150+
}
151+
152+
private static JsonArray MergeArrays(JsonArray destination, JsonArray source)
153+
{
154+
// Preserve earlier entries and let later sources contribute additional items such as plugins.
155+
foreach(var item in source)
156+
{
157+
destination.Add(item?.DeepClone());
133158
}
134159

135160
return destination;

0 commit comments

Comments
 (0)