Skip to content

Use dict-dispatch pattern in get_hconfig_view() for consistency #187

@jtdub

Description

@jtdub

Description

get_hconfig_view() in constructors.py:55-73 uses an isinstance chain to select the correct view class:

def get_hconfig_view(config: HConfig) -> HConfigViewBase:
    driver = config.driver
    if isinstance(driver, HConfigDriverAristaEOS):
        return HConfigViewAristaEOS(config)
    if isinstance(driver, HConfigDriverCiscoIOS):
        return HConfigViewCiscoIOS(config)
    ...

Meanwhile, get_hconfig_driver() in the same file uses the cleaner dict-dispatch pattern:

platform_drivers: dict[Platform, type[HConfigDriverBase]] = {
    Platform.ARISTA_EOS: HConfigDriverAristaEOS,
    ...
}

Proposed Improvement

Refactor get_hconfig_view() to use a dict mapping from driver type to view class, consistent with get_hconfig_driver(). This would:

  • Make the code easier to extend (add a dict entry vs. a new if branch)
  • Be consistent with the pattern already established in the same file
  • Reduce cyclomatic complexity

Metadata

Metadata

Assignees

No one assigned

    Labels

    v4-enhancementEnhancement planned for hier_config v4.0.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions