Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Shardingsphere YamlShadowRuleConfigurationSwapper SwapToObject

From Leeroopedia


Knowledge Sources
Domains Configuration_Management, Shadow_Testing
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete converter for transforming YAML shadow rule representation objects into domain configuration objects, provided by the ShardingSphere shadow module.

Description

YamlShadowRuleConfigurationSwapper is a final class that implements YamlRuleConfigurationSwapper<YamlShadowRuleConfiguration, ShadowRuleConfiguration>. Its swapToObject() method converts a deserialized YamlShadowRuleConfiguration into a ShadowRuleConfiguration that the rule builder and checker can consume.

The swapper delegates to three sub-swappers:

  • YamlShadowDataSourceConfigurationSwapper: Handles conversion of individual data source pairs. Note that the top-level swapper performs the YAML-map-to-collection conversion itself (line 90-96), converting each map entry into a ShadowDataSourceConfiguration with the map key as the name.
  • YamlShadowTableConfigurationSwapper: Converts each YamlShadowTableConfiguration into a ShadowTableConfiguration by copying the data source names and algorithm names.
  • YamlAlgorithmConfigurationSwapper: Converts each YamlAlgorithmConfiguration into an AlgorithmConfiguration.

After converting all three sections, the swapper applies two default-propagation steps:

  1. Default Data Source: If exactly one shadow data source mapping exists and a table has no explicit data source names, the single data source name is assigned to that table (lines 108-116).
  2. Default Algorithm: If a default shadow algorithm name is set and a table has no explicit algorithm names, the default algorithm name is assigned to that table (lines 118-125).

The swapper also implements swapToYamlConfiguration() for the reverse direction, getRuleTagName() returning "SHADOW", getOrder() returning ShadowOrder.ORDER, and getTypeClass() returning ShadowRuleConfiguration.class.

Usage

Use swapToObject() after YamlEngine.unmarshal() has produced a YamlShadowRuleConfiguration. The resulting ShadowRuleConfiguration is then passed to ShadowRuleConfigurationChecker.check() for validation and to ShadowRuleBuilder.build() (or directly to the ShadowRule constructor) for rule construction.

Code Reference

Source Location

  • Repository: Apache ShardingSphere
  • File: features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/YamlShadowRuleConfigurationSwapper.java
  • Lines: 79-88

Signature

@Override
public ShadowRuleConfiguration swapToObject(final YamlShadowRuleConfiguration yamlConfig) {
    ShadowRuleConfiguration result = new ShadowRuleConfiguration();
    result.setDataSources(swapToDataSources(yamlConfig.getDataSources()));
    result.setTables(swapToShadowTables(yamlConfig.getTables()));
    result.setShadowAlgorithms(swapToShadowAlgorithms(yamlConfig.getShadowAlgorithms()));
    setTableDefaultShadowDataSource(result.getTables(), result.getDataSources());
    setTableDefaultShadowAlgorithm(result.getTables(), result.getDefaultShadowAlgorithmName());
    result.setDefaultShadowAlgorithmName(yamlConfig.getDefaultShadowAlgorithmName());
    return result;
}

Import

import org.apache.shardingsphere.shadow.yaml.swapper.YamlShadowRuleConfigurationSwapper;

I/O Contract

Inputs

Name Type Required Description
yamlConfig YamlShadowRuleConfiguration Yes The deserialized YAML shadow rule configuration object containing dataSources, tables, shadowAlgorithms, and defaultShadowAlgorithmName

Outputs

Name Type Description
return ShadowRuleConfiguration Domain configuration object with dataSources as a Collection<ShadowDataSourceConfiguration>, tables as a Map<String, ShadowTableConfiguration>, shadowAlgorithms as a Map<String, AlgorithmConfiguration>, and defaultShadowAlgorithmName as a String. Tables may have default data source and algorithm values applied.

Usage Examples

// Step 1: Deserialize YAML to YAML configuration object
YamlShadowRuleConfiguration yamlConfig = YamlEngine.unmarshal(yamlContent, YamlShadowRuleConfiguration.class);

// Step 2: Convert to domain configuration object
YamlShadowRuleConfigurationSwapper swapper = new YamlShadowRuleConfigurationSwapper();
ShadowRuleConfiguration domainConfig = swapper.swapToObject(yamlConfig);

// The domain config is now ready for validation and rule building
// domainConfig.getDataSources() -> Collection<ShadowDataSourceConfiguration>
// domainConfig.getTables() -> Map<String, ShadowTableConfiguration>
// domainConfig.getShadowAlgorithms() -> Map<String, AlgorithmConfiguration>
// domainConfig.getDefaultShadowAlgorithmName() -> String (or null)

// Reverse direction: convert domain config back to YAML representation
YamlShadowRuleConfiguration yamlOutput = swapper.swapToYamlConfiguration(domainConfig);

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment