mergeIfPossible method

void mergeIfPossible(
  1. Map<String, Object> source,
  2. Map<String, Object> resultingSource,
  3. String name
)

Attempts to merge the provided source map into an existing MapPropertySource identified by name, writing the result into resultingSource.

Merging Rules

  • The method retrieves the existing source via get.
  • If the existing source's underlying data is a Map, its entries are copied into resultingSource first.
  • The incoming source map is then applied on top, allowing the new values to override existing ones when key names collide.

Example merge behavior:

Existing source:

{ "host": "localhost", "port": "8080" }

Incoming update:

{ "port": "9090", "secure": "true" }

Result:

{ "host": "localhost", "port": "9090", "secure": "true" }

If the existing property source is not backed by a Map, the incoming source map is still copied on top, but no merging occurs from the underlying data.

This helper exists to keep addOrMerge clean while providing deterministic, override-oriented merge semantics consistent with layered configuration models.

Implementation

void mergeIfPossible(Map<String, Object> source, Map<String, Object> resultingSource, String name) {
  final existingSource = get(name);
  if (existingSource != null) {
    final underlyingSource = existingSource.getSource();
    if (underlyingSource is Map) {
      resultingSource.addAll(underlyingSource as Map<String, Object>);
    }
    resultingSource.addAll(source);
  }
}