Skip to content

Remove redundant weight parameter from RuleBuilder.Dimension method - weights now auto-populated from dimension config#19

Merged
massiveio merged 7 commits into
masterfrom
copilot/fix-eb4db69f-b965-49a3-93ee-17b5a3c555d1
Aug 29, 2025
Merged

Remove redundant weight parameter from RuleBuilder.Dimension method - weights now auto-populated from dimension config#19
massiveio merged 7 commits into
masterfrom
copilot/fix-eb4db69f-b965-49a3-93ee-17b5a3c555d1

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 28, 2025

Problem

The RuleBuilder.Dimension() method required users to manually specify weights, even though dimension configurations (DimensionConfig) already contained weight values. This created redundancy and potential for inconsistency between configured weights and weights specified during rule creation.

// Before: Redundant weight specification
engine.AddDimension(&DimensionConfig{Name: "product", Weight: 15.0})
rule := NewRule("example").
    Dimension("product", "ProductA", MatchTypeEqual, 15.0). // Weight specified again!
    Build()

Solution

Modified the RuleBuilder.Dimension() method to automatically populate weights from dimension configurations during rule addition, eliminating the need for manual weight specification.

// After: Clean, simplified API
engine.AddDimension(&DimensionConfig{Name: "product", Weight: 15.0})
rule := NewRule("example").
    Dimension("product", "ProductA", MatchTypeEqual). // Weight auto-populated!
    Build()

Key Changes

API Simplification

  • Removed weight parameter from Dimension(name, value, matchType) method
  • Added automatic weight population during AddRule() and UpdateRule() operations
  • Implemented weight resolution logic:
    1. Use weight from DimensionConfig if configured
    2. Default to 1.0 for unconfigured dimensions (flexible mode compatibility)
    3. Preserve explicit weights from DimensionWithWeight() method

Backward Compatibility

  • Added DimensionWithWeight() method for cases requiring explicit weight overrides
  • Maintained existing behavior for edge cases and unconfigured dimensions
  • Updated convenience methods (AddSimpleRule, AddAnyRule) to use appropriate APIs

Weight Population Logic

func (m *InMemoryMatcher) populateDimensionWeights(rule *Rule) error {
    for _, dimValue := range rule.Dimensions {
        if dimValue.Weight != 0.0 { // Keep explicit weights
            continue
        }
        
        if config, exists := m.dimensionConfigs[dimValue.DimensionName]; exists {
            dimValue.Weight = config.Weight // Use configured weight
        } else {
            dimValue.Weight = 1.0 // Default weight
        }
    }
    return nil
}

Validation

Comprehensive Test Coverage

  • TestAutomaticWeightPopulation: Verifies weights are correctly populated from dimension configs
  • TestDefaultWeightWhenNoDimensionConfig: Ensures default weight behavior works
  • TestDimensionWithWeightBackwardCompatibility: Validates explicit weight override functionality

Working Demo

Created example/weight_removal_demo demonstrating:

  • Automatic weight population from dimension configurations
  • Mixed usage with both auto and explicit weights
  • Default weight behavior for unconfigured dimensions
  • Backward compatibility with DimensionWithWeight()

Benefits

  1. Eliminates Redundancy: No need to specify weights already defined in dimension configurations
  2. Prevents Inconsistency: Centralized weight management through dimension configs
  3. Simplifies API: Cleaner rule creation syntax with fewer parameters
  4. Maintains Flexibility: Still supports explicit weights when needed via DimensionWithWeight()
  5. Backward Compatible: Existing code using explicit weights continues to work

Migration Path

  • New code: Use Dimension(name, value, matchType) - weights auto-populated
  • Existing code: Continue using DimensionWithWeight(name, value, matchType, weight)
  • Mixed approach: Combine both methods as needed for maximum flexibility

This change addresses the core issue raised: "Weight in Dimension method of RuleBuilder is not necessary, cause dimension config contains weight already."


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits August 28, 2025 07:43
…omatic weight population

Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
Copilot AI changed the title [WIP] Weight in Dimension method of RuleBuilder is not necessary, cause dimension config contains weight already. Remove redundant weight parameter from RuleBuilder.Dimension method - weights now auto-populated from dimension config Aug 28, 2025
Copilot AI requested a review from cloorc August 28, 2025 07:55
cloorc added 3 commits August 28, 2025 08:21
Signed-off-by: Cloorc <wittcnezh@foxmail.com>
Signed-off-by: Cloorc <wittcnezh@foxmail.com>
Signed-off-by: Cloorc <wittcnezh@foxmail.com>
@massiveio massiveio marked this pull request as ready for review August 29, 2025 03:48
@massiveio massiveio merged commit 9425fdf into master Aug 29, 2025
2 checks passed
@massiveio massiveio deleted the copilot/fix-eb4db69f-b965-49a3-93ee-17b5a3c555d1 branch August 29, 2025 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants