diff --git a/api_test.go b/api_test.go index fe45afa..48f4c0d 100644 --- a/api_test.go +++ b/api_test.go @@ -40,14 +40,14 @@ func TestAPIUpdateRule(t *testing.T) { defer engine.Close() // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { t.Fatalf("Failed to add region dimension: %v", err) } - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) err = engine.AddDimension(envConfig) if err != nil { @@ -176,7 +176,7 @@ func TestAPIUpdateRuleStatus(t *testing.T) { defer engine.Close() // Add dimension configuration - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -241,7 +241,7 @@ func TestAPIUpdateRuleMetadata(t *testing.T) { defer engine.Close() // Add dimension configuration - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -321,7 +321,7 @@ func TestAPIGetRule(t *testing.T) { defer engine.Close() // Add dimension configuration - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -439,7 +439,7 @@ func TestAPIAddDimension(t *testing.T) { defer engine.Close() // Add a dimension - dim := NewDimensionConfig("api-test-dim", 100, false, 1.0) + dim := NewDimensionConfig("api-test-dim", 100, false) if err := engine.AddDimension(dim); err != nil { t.Errorf("AddDimension failed: %v", err) } @@ -494,7 +494,7 @@ func TestAPIFindAllMatches(t *testing.T) { defer engine.Close() // Add dimension config to control weights - config := NewDimensionConfig("region", 0, false, 5.0) + config := NewDimensionConfig("region", 0, false) if err := engine.AddDimension(config); err != nil { t.Fatalf("Failed to add dimension config: %v", err) } @@ -538,7 +538,7 @@ func TestAPIBatchAddRules(t *testing.T) { defer engine.Close() // Add dimension config to control weights - config := NewDimensionConfig("region", 0, false, 3.0) + config := NewDimensionConfig("region", 0, false) if err := engine.AddDimension(config); err != nil { t.Fatalf("Failed to add dimension config: %v", err) } @@ -706,7 +706,7 @@ func TestGenerateDefaultNodeID(t *testing.T) { func TestDimensionConfig_GetWeight(t *testing.T) { // Test case 1: GetWeight with defined match type weights - config := NewDimensionConfig("test", 0, false, 5.0) + config := NewDimensionConfig("test", 0, false) config.SetWeight(MatchTypeEqual, 10.0) config.SetWeight(MatchTypePrefix, 7.0) config.SetWeight(MatchTypeAny, 2.0) @@ -724,23 +724,23 @@ func TestDimensionConfig_GetWeight(t *testing.T) { t.Errorf("Expected weight 2.0 for Any match type, got %.1f", weight) } - // Test getting undefined weight (should return default) - if weight := config.GetWeight(MatchTypeSuffix); weight != 5.0 { - t.Errorf("Expected default weight 5.0 for undefined Suffix match type, got %.1f", weight) + // Test getting undefined weight (should return 0.0, no default weight anymore) + if weight := config.GetWeight(MatchTypeSuffix); weight != 0.0 { + t.Errorf("Expected 0.0 for undefined Suffix match type, got %.1f", weight) } - // Test case 2: GetWeight with no defined weights (all should return default) - emptyConfig := NewDimensionConfig("empty", 1, true, 15.0) + // Test case 2: GetWeight with no defined weights (all should return 0.0) + emptyConfig := NewDimensionConfig("empty", 1, true) matchTypes := []MatchType{MatchTypeEqual, MatchTypePrefix, MatchTypeSuffix, MatchTypeAny} for _, mt := range matchTypes { - if weight := emptyConfig.GetWeight(mt); weight != 15.0 { - t.Errorf("Expected default weight 15.0 for %s match type, got %.1f", mt, weight) + if weight := emptyConfig.GetWeight(mt); weight != 0.0 { + t.Errorf("Expected 0.0 for %s match type, got %.1f", mt, weight) } } // Test case 3: GetWeight after setting and overriding weights - overrideConfig := NewDimensionConfig("override", 2, false, 3.0) + overrideConfig := NewDimensionConfig("override", 2, false) // Set initial weight overrideConfig.SetWeight(MatchTypeEqual, 8.0) @@ -754,14 +754,14 @@ func TestDimensionConfig_GetWeight(t *testing.T) { t.Errorf("Expected weight 12.0 after override, got %.1f", weight) } - // Other match types should still return default - if weight := overrideConfig.GetWeight(MatchTypePrefix); weight != 3.0 { - t.Errorf("Expected default weight 3.0 for Prefix after override, got %.1f", weight) + // Other match types should still return 0.0 (no default weight anymore) + if weight := overrideConfig.GetWeight(MatchTypePrefix); weight != 0.0 { + t.Errorf("Expected 0.0 for Prefix after override, got %.1f", weight) } } func TestDimensionConfig_SetWeightFunction(t *testing.T) { - config := NewDimensionConfig("test", 0, false, 1.0) + config := NewDimensionConfig("test", 0, false) // Test setting weights for all match types weights := map[MatchType]float64{ @@ -801,7 +801,7 @@ func TestNewDimensionConfigWithWeightsFunction(t *testing.T) { MatchTypeAny: 3.0, } - config := NewDimensionConfigWithWeights("weighted", 1, true, weights, 5.0) + config := NewDimensionConfigWithWeights("weighted", 1, true, weights) if config.Name != "weighted" { t.Errorf("Expected name 'weighted', got '%s'", config.Name) @@ -815,10 +815,16 @@ func TestNewDimensionConfigWithWeightsFunction(t *testing.T) { t.Error("Expected required to be true") } - if config.DefaultWeight != 5.0 { - t.Errorf("Expected default weight 5.0, got %.1f", config.DefaultWeight) + // Test that unset match types return 0.0 (no DefaultWeight anymore) + // Note: All match types in the weights map above are set, so we need to test a different approach + // or verify that GetWeight returns the correct explicit values + if config.GetWeight(MatchTypePrefix) != 10.0 { + t.Errorf("Expected MatchTypePrefix weight 10.0 from explicit weights, got %.1f", config.GetWeight(MatchTypePrefix)) } + // Test what happens if we query a type that was explicitly set + // Since all match types are covered in weights map, this test verifies explicit weights work + // Test that all weights were set correctly for matchType, expectedWeight := range weights { actualWeight := config.GetWeight(matchType) @@ -857,7 +863,7 @@ func TestInitializeDimensionFunction(t *testing.T) { // Since InitializeDimension is a no-op, we just verify it doesn't crash // and we can still add dimensions normally - config := NewDimensionConfig("test_dimension", 0, false, 10.0) + config := NewDimensionConfig("test_dimension", 0, false) config.SetWeight(MatchTypeEqual, 20.0) config.SetWeight(MatchTypePrefix, 15.0) @@ -909,7 +915,7 @@ func TestDeleteDimensionFunction(t *testing.T) { defer engine.Close() // Add a dimension - config := NewDimensionConfig("removable", 0, false, 5.0) + config := NewDimensionConfig("removable", 0, false) err = engine.AddDimension(config) if err != nil { t.Fatalf("Failed to add dimension: %v", err) @@ -1048,7 +1054,7 @@ func TestCreateQueryWithDynamicConfigs(t *testing.T) { } dynamicConfigs := map[string]*DimensionConfig{ - "region": NewDimensionConfig("region", 0, false, 10.0), + "region": NewDimensionConfig("region", 0, false), } query := CreateQueryWithDynamicConfigs(values, dynamicConfigs) @@ -1087,8 +1093,8 @@ func TestCreateQueryWithTenantAndDynamicConfigs(t *testing.T) { } dynamicConfigs := map[string]*DimensionConfig{ - "category": NewDimensionConfig("category", 0, true, 15.0), - "priority": NewDimensionConfig("priority", 1, false, 8.0), + "category": NewDimensionConfig("category", 0, true), + "priority": NewDimensionConfig("priority", 1, false), } query := CreateQueryWithTenantAndDynamicConfigs(tenantID, applicationID, values, dynamicConfigs) @@ -1120,16 +1126,6 @@ func TestCreateQueryWithTenantAndDynamicConfigs(t *testing.T) { if len(query.DynamicDimensionConfigs) != 2 { t.Errorf("Expected 2 dynamic configs, got %d", len(query.DynamicDimensionConfigs)) } - - categoryConfig := query.DynamicDimensionConfigs["category"] - if categoryConfig.Name != "category" || categoryConfig.DefaultWeight != 15.0 { - t.Errorf("Category config mismatch: name=%s, weight=%.1f", categoryConfig.Name, categoryConfig.DefaultWeight) - } - - priorityConfig := query.DynamicDimensionConfigs["priority"] - if priorityConfig.Name != "priority" || priorityConfig.DefaultWeight != 8.0 { - t.Errorf("Priority config mismatch: name=%s, weight=%.1f", priorityConfig.Name, priorityConfig.DefaultWeight) - } } func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) { @@ -1139,7 +1135,7 @@ func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) { } dynamicConfigs := map[string]*DimensionConfig{ - "service": NewDimensionConfig("service", 0, true, 20.0), + "service": NewDimensionConfig("service", 0, true), } query := CreateQueryWithAllRulesAndDynamicConfigs(values, dynamicConfigs) @@ -1163,11 +1159,6 @@ func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) { if len(query.DynamicDimensionConfigs) != 1 { t.Errorf("Expected 1 dynamic config, got %d", len(query.DynamicDimensionConfigs)) } - - serviceConfig := query.DynamicDimensionConfigs["service"] - if serviceConfig.Name != "service" || serviceConfig.DefaultWeight != 20.0 { - t.Errorf("Service config mismatch: name=%s, weight=%.1f", serviceConfig.Name, serviceConfig.DefaultWeight) - } } func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) { @@ -1180,9 +1171,9 @@ func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) { } dynamicConfigs := map[string]*DimensionConfig{ - "user_type": NewDimensionConfig("user_type", 0, true, 25.0), - "action": NewDimensionConfig("action", 1, true, 20.0), - "resource": NewDimensionConfig("resource", 2, false, 10.0), + "user_type": NewDimensionConfig("user_type", 0, true), + "action": NewDimensionConfig("action", 1, true), + "resource": NewDimensionConfig("resource", 2, false), } query := CreateQueryWithAllRulesTenantAndDynamicConfigs(tenantID, applicationID, values, dynamicConfigs) @@ -1240,10 +1231,6 @@ func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) { t.Errorf("Config %s: expected name '%s', got '%s'", name, name, config.Name) } - if config.DefaultWeight != expected.weight { - t.Errorf("Config %s: expected weight %.1f, got %.1f", name, expected.weight, config.DefaultWeight) - } - if config.Required != expected.required { t.Errorf("Config %s: expected required %v, got %v", name, expected.required, config.Required) } @@ -1262,7 +1249,7 @@ func TestDynamicConfigsIntegration(t *testing.T) { defer engine.Close() // Add basic dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) regionConfig.SetWeight(MatchTypePrefix, 7.0) @@ -1300,7 +1287,7 @@ func TestDynamicConfigsIntegration(t *testing.T) { { name: "CreateQueryWithDynamicConfigs", createQuery: func() *QueryRule { - dynamicConfig := NewDimensionConfig("region", 0, false, 50.0) + dynamicConfig := NewDimensionConfig("region", 0, false) dynamicConfig.SetWeight(MatchTypeEqual, 100.0) return CreateQueryWithDynamicConfigs( @@ -1313,7 +1300,7 @@ func TestDynamicConfigsIntegration(t *testing.T) { { name: "CreateQueryWithTenantAndDynamicConfigs", createQuery: func() *QueryRule { - dynamicConfig := NewDimensionConfig("region", 0, false, 75.0) + dynamicConfig := NewDimensionConfig("region", 0, false) dynamicConfig.SetWeight(MatchTypeEqual, 150.0) return CreateQueryWithTenantAndDynamicConfigs( @@ -1327,7 +1314,7 @@ func TestDynamicConfigsIntegration(t *testing.T) { { name: "CreateQueryWithAllRulesAndDynamicConfigs", createQuery: func() *QueryRule { - dynamicConfig := NewDimensionConfig("region", 0, false, 30.0) + dynamicConfig := NewDimensionConfig("region", 0, false) dynamicConfig.SetWeight(MatchTypeEqual, 60.0) return CreateQueryWithAllRulesAndDynamicConfigs( @@ -1340,7 +1327,7 @@ func TestDynamicConfigsIntegration(t *testing.T) { { name: "CreateQueryWithAllRulesTenantAndDynamicConfigs", createQuery: func() *QueryRule { - dynamicConfig := NewDimensionConfig("region", 0, false, 25.0) + dynamicConfig := NewDimensionConfig("region", 0, false) dynamicConfig.SetWeight(MatchTypeEqual, 50.0) return CreateQueryWithAllRulesTenantAndDynamicConfigs( @@ -1388,7 +1375,7 @@ func TestDeleteDimensionCoverage(t *testing.T) { defer engine.Close() // Add a dimension first - dimConfig := NewDimensionConfig("test_dim_delete", 0, false, 5.0) + dimConfig := NewDimensionConfig("test_dim_delete", 0, false) err = engine.AddDimension(dimConfig) if err != nil { t.Fatalf("Failed to add dimension: %v", err) diff --git a/atomic_update_test.go b/atomic_update_test.go index 793fad9..d07b525 100644 --- a/atomic_update_test.go +++ b/atomic_update_test.go @@ -18,11 +18,11 @@ func TestAtomicRuleUpdateFix(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) - serviceConfig := NewDimensionConfig("service", 2, false, 2.0) + serviceConfig := NewDimensionConfig("service", 2, false) serviceConfig.SetWeight(MatchTypeEqual, 6.0) engine.AddDimension(regionConfig) diff --git a/cmd/debug_matching/main.go b/cmd/debug_matching/main.go index 12b2cb4..a61cb26 100644 --- a/cmd/debug_matching/main.go +++ b/cmd/debug_matching/main.go @@ -24,9 +24,9 @@ func main() { defer engine.Close() // Configure 3 simple dimensions - engine.AddDimension(matcher.NewDimensionConfig("product", 0, true, 10.0)) - engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true, 5.0)) - engine.AddDimension(matcher.NewDimensionConfig("region", 2, false, 3.0)) + engine.AddDimension(matcher.NewDimensionConfig("product", 0, true)) + engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true)) + engine.AddDimension(matcher.NewDimensionConfig("region", 2, false)) // Add a simple test rule rule := matcher.NewRule("test_rule"). diff --git a/cmd/performance_benchmark/main.go b/cmd/performance_benchmark/main.go index bfd4013..80993f5 100644 --- a/cmd/performance_benchmark/main.go +++ b/cmd/performance_benchmark/main.go @@ -274,8 +274,7 @@ func generateRealisticDimensions(count int) []*matcher.DimensionConfig { dimensions[i] = matcher.NewDimensionConfig( name, i, - i < 3, // First 3 dimensions required - float64(20-(i%20)), // Weights from 20 down to 1, cycling if needed + i < 3, // First 3 dimensions required ) } return dimensions diff --git a/cmd/target_performance/main.go b/cmd/target_performance/main.go index 30002e2..8d52fb2 100644 --- a/cmd/target_performance/main.go +++ b/cmd/target_performance/main.go @@ -45,8 +45,7 @@ func main() { dim := matcher.NewDimensionConfig( fmt.Sprintf("dim_%02d", i), i, - i < 3, // First 3 required - float64(21-i), // 20, 19, 18, ... 1 + i < 3, // First 3 required ) if err := engine.AddDimension(dim); err != nil { slog.Error("Failed to add dimension", "error", err) diff --git a/concurrency_test.go b/concurrency_test.go index b069285..dbaca73 100644 --- a/concurrency_test.go +++ b/concurrency_test.go @@ -24,11 +24,11 @@ func TestConcurrentRuleOperationsNoPartialRules(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) - serviceConfig := NewDimensionConfig("service", 2, false, 2.0) + serviceConfig := NewDimensionConfig("service", 2, false) serviceConfig.SetWeight(MatchTypeEqual, 6.0) err = engine.AddDimension(regionConfig) @@ -311,7 +311,7 @@ func TestConcurrentRuleStatusUpdatesNoPartialRules(t *testing.T) { defer engine.Close() // Add dimension configuration - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -436,7 +436,7 @@ func TestConcurrentMetadataUpdatesNoPartialRules(t *testing.T) { defer engine.Close() // Add dimension configuration - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { diff --git a/consistency_guarantees_test.go b/consistency_guarantees_test.go index 4eff2ae..46da2f4 100644 --- a/consistency_guarantees_test.go +++ b/consistency_guarantees_test.go @@ -22,7 +22,7 @@ func TestRuleConsistencyGuarantees(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add test dimensions - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { diff --git a/dynamic_configs_test.go b/dynamic_configs_test.go index 8dcbcb1..58a3ab9 100644 --- a/dynamic_configs_test.go +++ b/dynamic_configs_test.go @@ -17,11 +17,11 @@ func TestDynamicDimensionConfigsWithMatchTypes(t *testing.T) { defer engine.Close() // Add dimension configurations with match type-specific weights - regionConfig := NewDimensionConfig("region", 0, true, 5.0) + regionConfig := NewDimensionConfig("region", 0, true) regionConfig.SetWeight(MatchTypeEqual, 10.0) regionConfig.SetWeight(MatchTypePrefix, 7.0) - envConfig := NewDimensionConfig("env", 1, true, 3.0) + envConfig := NewDimensionConfig("env", 1, true) envConfig.SetWeight(MatchTypeEqual, 8.0) envConfig.SetWeight(MatchTypeAny, 2.0) @@ -87,11 +87,11 @@ func TestDynamicDimensionConfigsWithMatchTypes(t *testing.T) { } // Test 2: Query with dynamic dimension configs (different weights per match type) - dynamicRegionConfig := NewDimensionConfig("region", 0, true, 1.0) + dynamicRegionConfig := NewDimensionConfig("region", 0, true) dynamicRegionConfig.SetWeight(MatchTypeEqual, 50.0) // Much higher for exact matches dynamicRegionConfig.SetWeight(MatchTypePrefix, 30.0) // High for prefix matches - dynamicEnvConfig := NewDimensionConfig("env", 1, true, 1.0) + dynamicEnvConfig := NewDimensionConfig("env", 1, true) dynamicEnvConfig.SetWeight(MatchTypeEqual, 25.0) dynamicEnvConfig.SetWeight(MatchTypeAny, 5.0) @@ -129,7 +129,7 @@ func TestDynamicDimensionConfigsWithMatchTypes(t *testing.T) { } // Test 3: Query with partial dynamic configs (only override one dimension) - partialDynamicConfig := NewDimensionConfig("region", 0, true, 1.0) + partialDynamicConfig := NewDimensionConfig("region", 0, true) partialDynamicConfig.SetWeight(MatchTypeEqual, 100.0) // Very high weight for exact matches partialDynamicConfig.SetWeight(MatchTypePrefix, 60.0) @@ -184,13 +184,13 @@ func TestDynamicConfigsWithComplexMatchTypes(t *testing.T) { defer engine.Close() // Add initial dimension configurations - priorityConfig := NewDimensionConfig("priority", 0, true, 2.0) + priorityConfig := NewDimensionConfig("priority", 0, true) priorityConfig.SetWeight(MatchTypeEqual, 15.0) priorityConfig.SetWeight(MatchTypePrefix, 10.0) priorityConfig.SetWeight(MatchTypeSuffix, 8.0) priorityConfig.SetWeight(MatchTypeAny, 3.0) - categoryConfig := NewDimensionConfig("category", 1, true, 1.0) + categoryConfig := NewDimensionConfig("category", 1, true) categoryConfig.SetWeight(MatchTypeEqual, 12.0) categoryConfig.SetWeight(MatchTypeAny, 2.0) @@ -238,13 +238,13 @@ func TestDynamicConfigsWithComplexMatchTypes(t *testing.T) { } // Test with dynamic configs that heavily favor prefix matches - dynamicPriorityConfig := NewDimensionConfig("priority", 0, true, 1.0) + dynamicPriorityConfig := NewDimensionConfig("priority", 0, true) dynamicPriorityConfig.SetWeight(MatchTypeEqual, 20.0) dynamicPriorityConfig.SetWeight(MatchTypePrefix, 100.0) // Heavily favor prefix matches dynamicPriorityConfig.SetWeight(MatchTypeSuffix, 15.0) dynamicPriorityConfig.SetWeight(MatchTypeAny, 5.0) - dynamicCategoryConfig := NewDimensionConfig("category", 1, true, 1.0) + dynamicCategoryConfig := NewDimensionConfig("category", 1, true) dynamicCategoryConfig.SetWeight(MatchTypeEqual, 30.0) dynamicCategoryConfig.SetWeight(MatchTypeAny, 10.0) @@ -317,12 +317,12 @@ func TestDynamicConfigsWithMultipleMatchTypes(t *testing.T) { defer engine.Close() // Create dimension config with different weights for different match types - priorityConfig := NewDimensionConfig("priority", 0, true, 1.0) // default weight 1.0 + priorityConfig := NewDimensionConfig("priority", 0, true) // default weight 1.0 priorityConfig.SetWeight(MatchTypeEqual, 10.0) // exact matches get 10.0 priorityConfig.SetWeight(MatchTypePrefix, 5.0) // prefix matches get 5.0 priorityConfig.SetWeight(MatchTypeAny, 2.0) // any matches get 2.0 - categoryConfig := NewDimensionConfig("category", 1, true, 1.0) // default weight 1.0 + categoryConfig := NewDimensionConfig("category", 1, true) // default weight 1.0 categoryConfig.SetWeight(MatchTypeEqual, 8.0) // exact matches get 8.0 categoryConfig.SetWeight(MatchTypeSuffix, 3.0) // suffix matches get 3.0 @@ -403,11 +403,11 @@ func TestDynamicConfigsWithMultipleMatchTypes(t *testing.T) { "priority": NewDimensionConfigWithWeights("priority", 0, true, map[MatchType]float64{ MatchTypeEqual: 2.0, // lower weight for exact MatchTypePrefix: 20.0, // much higher weight for prefix - }, 1.0), + }), "category": NewDimensionConfigWithWeights("category", 1, true, map[MatchType]float64{ MatchTypeEqual: 3.0, // lower weight for exact MatchTypeSuffix: 15.0, // higher weight for suffix - }, 1.0), + }), }, } diff --git a/example/dimension_consistency/main.go b/example/dimension_consistency/main.go index e197e1e..80bc462 100644 --- a/example/dimension_consistency/main.go +++ b/example/dimension_consistency/main.go @@ -57,19 +57,19 @@ func main() { fmt.Println("\n2. Configuring dimensions to enforce consistency:") // Now configure dimensions to enforce consistency - err = engine.AddDimension(matcher.NewDimensionConfig("product", 0, true, 10.0)) + err = engine.AddDimension(matcher.NewDimensionConfig("product", 0, true)) if err != nil { slog.Error("Failed to add product dimension", "error", err) os.Exit(1) } - err = engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true, 8.0)) + err = engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true)) if err != nil { slog.Error("Failed to add environment dimension", "error", err) os.Exit(1) } - err = engine.AddDimension(matcher.NewDimensionConfig("region", 2, false, 5.0)) + err = engine.AddDimension(matcher.NewDimensionConfig("region", 2, false)) if err != nil { slog.Error("Failed to add region dimension", "error", err) os.Exit(1) diff --git a/example/forest_demo/main.go b/example/forest_demo/main.go index d82b3ac..a480db2 100644 --- a/example/forest_demo/main.go +++ b/example/forest_demo/main.go @@ -25,9 +25,9 @@ func main() { // Initialize default dimensions // Add required dimensions dimensions := []*matcher.DimensionConfig{ - matcher.NewDimensionConfig("product", 0, true, 10.0), - matcher.NewDimensionConfig("route", 1, false, 5.0), - matcher.NewDimensionConfig("tool", 2, false, 8.0), + matcher.NewDimensionConfig("product", 0, true), + matcher.NewDimensionConfig("route", 1, false), + matcher.NewDimensionConfig("tool", 2, false), } for _, dim := range dimensions { diff --git a/example/main.go b/example/main.go index 797b0f5..ded77c3 100644 --- a/example/main.go +++ b/example/main.go @@ -24,33 +24,33 @@ func main() { // Add core dimensions coreDims := []*matcher.DimensionConfig{ - matcher.NewDimensionConfig("product", 0, true, 10.0), - matcher.NewDimensionConfig("route", 1, false, 5.0), - matcher.NewDimensionConfig("tool", 2, false, 8.0), - matcher.NewDimensionConfig("tool_id", 3, false, 3.0), - matcher.NewDimensionConfig("recipe", 4, false, 12.0), + matcher.NewDimensionConfig("product", 0, true), + matcher.NewDimensionConfig("route", 1, false), + matcher.NewDimensionConfig("tool", 2, false), + matcher.NewDimensionConfig("tool_id", 3, false), + matcher.NewDimensionConfig("recipe", 4, false), } for _, dim := range coreDims { if err := engine.AddDimension(dim); err != nil { slog.Error("Failed to add dimension", "dimension", dim.Name, "error", err) } else { - fmt.Printf(" Added dimension: %s (default weight: %.1f)\n", dim.Name, dim.DefaultWeight) + fmt.Printf(" Added dimension: %s\n", dim.Name) } } // Add custom dimensions customDims := []*matcher.DimensionConfig{ - matcher.NewDimensionConfig("region", 5, false, 7.0), - matcher.NewDimensionConfig("priority", 6, false, 15.0), - matcher.NewDimensionConfig("environment", 7, false, 5.0), + matcher.NewDimensionConfig("region", 5, false), + matcher.NewDimensionConfig("priority", 6, false), + matcher.NewDimensionConfig("environment", 7, false), } for _, dim := range customDims { if err := engine.AddDimension(dim); err != nil { slog.Error("Failed to add dimension", "dimension", dim.Name, "error", err) } else { - fmt.Printf(" Added dimension: %s (default weight: %.1f)\n", dim.Name, dim.DefaultWeight) + fmt.Printf(" Added dimension: %s\n", dim.Name) } } diff --git a/example/weight_conflict_demo/main.go b/example/weight_conflict_demo/main.go index af11f08..c144b50 100644 --- a/example/weight_conflict_demo/main.go +++ b/example/weight_conflict_demo/main.go @@ -22,8 +22,8 @@ func main() { // Add dimension configurations with weights dimensionConfigs := []*matcher.DimensionConfig{ - matcher.NewDimensionConfig("product", 0, true, 10.0), - matcher.NewDimensionConfig("environment", 1, false, 5.0), + matcher.NewDimensionConfig("product", 0, true), + matcher.NewDimensionConfig("environment", 1, false), } for _, config := range dimensionConfigs { diff --git a/example/weight_removal_demo/main.go b/example/weight_removal_demo/main.go index bc925ec..fe43591 100644 --- a/example/weight_removal_demo/main.go +++ b/example/weight_removal_demo/main.go @@ -21,26 +21,26 @@ func main() { // Step 1: Configure dimensions with specific weights fmt.Println("1. Configuring dimensions with predefined weights...") - productDim := matcher.NewDimensionConfig("product", 0, false, 15.0) + productDim := matcher.NewDimensionConfig("product", 0, false) - environmentDim := matcher.NewDimensionConfig("environment", 1, false, 8.0) + environmentDim := matcher.NewDimensionConfig("environment", 1, false) - regionDim := matcher.NewDimensionConfig("region", 2, false, 5.0) + regionDim := matcher.NewDimensionConfig("region", 2, false) if err := engine.AddDimension(productDim); err != nil { log.Fatalf("Failed to add product dimension: %v", err) } - fmt.Printf(" Added dimension 'product' with weight %.1f\n", productDim.DefaultWeight) + fmt.Printf(" Added dimension 'product' with weight %.1f\n", productDim.GetWeight(matcher.MatchTypeEqual)) if err := engine.AddDimension(environmentDim); err != nil { log.Fatalf("Failed to add environment dimension: %v", err) } - fmt.Printf(" Added dimension 'environment' with weight %.1f\n", environmentDim.DefaultWeight) + fmt.Printf(" Added dimension 'environment' with weight %.1f\n", environmentDim.GetWeight(matcher.MatchTypeEqual)) if err := engine.AddDimension(regionDim); err != nil { log.Fatalf("Failed to add region dimension: %v", err) } - fmt.Printf(" Added dimension 'region' with weight %.1f\n", regionDim.DefaultWeight) + fmt.Printf(" Added dimension 'region' with weight %.1f\n", regionDim.GetWeight(matcher.MatchTypeEqual)) fmt.Println() @@ -97,29 +97,29 @@ func main() { // Check rule1 fmt.Printf("Rule 1 ('%s'):\n", rule1.ID) - fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.DefaultWeight) - fmt.Printf(" environment dimension weight: %.1f (from config)\n", environmentDim.DefaultWeight) - fmt.Printf(" region dimension weight: %.1f (from config)\n", regionDim.DefaultWeight) + fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.GetWeight(matcher.MatchTypeEqual)) + fmt.Printf(" environment dimension weight: %.1f (from config)\n", environmentDim.GetWeight(matcher.MatchTypeEqual)) + fmt.Printf(" region dimension weight: %.1f (from config)\n", regionDim.GetWeight(matcher.MatchTypeEqual)) fmt.Printf(" Total calculated weight: %.1f\n", rule1.CalculateTotalWeight(dimensionConfigs)) fmt.Println() // Check rule2 fmt.Printf("Rule 2 ('%s'):\n", rule2.ID) - fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.DefaultWeight) - fmt.Printf(" environment dimension weight: %.1f (from config)\n", environmentDim.DefaultWeight) + fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.GetWeight(matcher.MatchTypeEqual)) + fmt.Printf(" environment dimension weight: %.1f (from config)\n", environmentDim.GetWeight(matcher.MatchTypeEqual)) fmt.Printf(" Total calculated weight: %.1f\n", rule2.CalculateTotalWeight(dimensionConfigs)) fmt.Println() // Check rule3 (mixed) fmt.Printf("Rule 3 ('%s') - Mixed approach:\n", rule3.ID) - fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.DefaultWeight) + fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.GetWeight(matcher.MatchTypeEqual)) fmt.Printf(" environment dimension weight: %.1f (explicit override via ManualWeight)\n", *rule3.ManualWeight) fmt.Printf(" Total calculated weight: %.1f\n", rule3.CalculateTotalWeight(dimensionConfigs)) fmt.Println() // Check rule4 (single dimension) fmt.Printf("Rule 4 ('%s') - Single dimension:\n", rule4.ID) - fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.DefaultWeight) + fmt.Printf(" product dimension weight: %.1f (from config)\n", productDim.GetWeight(matcher.MatchTypeEqual)) fmt.Printf(" Total calculated weight: %.1f\n", rule4.CalculateTotalWeight(dimensionConfigs)) fmt.Println() diff --git a/forest_adv_test.go b/forest_adv_test.go index e2d8e33..356ffae 100644 --- a/forest_adv_test.go +++ b/forest_adv_test.go @@ -7,9 +7,9 @@ import ( func TestRuleForestDimensionOrder(t *testing.T) { // Define dimension order dimensionConfigs := map[string]*DimensionConfig{ - "product": NewDimensionConfig("product", 0, false, 10.0), - "region": NewDimensionConfig("region", 1, false, 5.0), - "env": NewDimensionConfig("env", 2, false, 3.0), + "product": NewDimensionConfig("product", 0, false), + "region": NewDimensionConfig("region", 1, false), + "env": NewDimensionConfig("env", 2, false), } forest := CreateRuleForest(dimensionConfigs) @@ -121,9 +121,9 @@ func TestRuleForestDimensionOrder(t *testing.T) { func TestRuleForestDimensionTraversal(t *testing.T) { // Test that the forest properly traverses dimensions level by level dimensionConfigs := map[string]*DimensionConfig{ - "product": NewDimensionConfig("product", 0, false, 8.0), - "region": NewDimensionConfig("region", 1, false, 6.0), - "env": NewDimensionConfig("env", 2, false, 4.0), + "product": NewDimensionConfig("product", 0, false), + "region": NewDimensionConfig("region", 1, false), + "env": NewDimensionConfig("env", 2, false), } forest := CreateRuleForest(dimensionConfigs) @@ -184,8 +184,8 @@ func TestRuleForestDimensionTraversal(t *testing.T) { func TestRuleForestSharedPaths(t *testing.T) { // Test that the forest can handle shared paths between different rules dimensionConfigs := map[string]*DimensionConfig{ - "product": NewDimensionConfig("product", 0, false, 12.0), - "region": NewDimensionConfig("region", 1, false, 7.0), + "product": NewDimensionConfig("product", 0, false), + "region": NewDimensionConfig("region", 1, false), } forest := CreateRuleForest(dimensionConfigs) diff --git a/forest_equal_optimization_test.go b/forest_equal_optimization_test.go index 594147d..837f5ff 100644 --- a/forest_equal_optimization_test.go +++ b/forest_equal_optimization_test.go @@ -21,11 +21,11 @@ func TestEqualMatchOptimization(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) regionConfig.SetWeight(MatchTypePrefix, 7.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) envConfig.SetWeight(MatchTypeAny, 2.0) @@ -184,10 +184,10 @@ func TestEqualMatchPerformance(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - serviceConfig := NewDimensionConfig("service", 1, false, 3.0) + serviceConfig := NewDimensionConfig("service", 1, false) serviceConfig.SetWeight(MatchTypeEqual, 8.0) err = engine.AddDimension(regionConfig) @@ -290,7 +290,7 @@ func TestEqualMatchCorrectness(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configuration - userConfig := NewDimensionConfig("user_type", 0, false, 5.0) + userConfig := NewDimensionConfig("user_type", 0, false) userConfig.SetWeight(MatchTypeEqual, 10.0) userConfig.SetWeight(MatchTypePrefix, 7.0) diff --git a/forest_test.go b/forest_test.go index 65700c5..aa9a537 100644 --- a/forest_test.go +++ b/forest_test.go @@ -184,7 +184,7 @@ func TestSimpleEqualMatch(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) @@ -237,7 +237,7 @@ func TestVeryDetailedDebug(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) @@ -307,7 +307,7 @@ func TestDeepDebugOptimization(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) @@ -366,10 +366,10 @@ func TestTwoDimensionForestStructure(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension configurations - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) err = engine.AddDimension(regionConfig) diff --git a/forest_weight_test.go b/forest_weight_test.go index eb437c3..8cde48e 100644 --- a/forest_weight_test.go +++ b/forest_weight_test.go @@ -8,8 +8,8 @@ import ( func TestForestWeightOrdering(t *testing.T) { // Set up dimension configs to control weights dimensionConfigs := map[string]*DimensionConfig{ - "region": NewDimensionConfig("region", 0, false, 10.0), - "env": NewDimensionConfig("env", 1, false, 5.0), + "region": NewDimensionConfig("region", 0, false), + "env": NewDimensionConfig("env", 1, false), } forest := CreateRuleForest(dimensionConfigs) @@ -97,7 +97,7 @@ func TestForestWeightOrdering(t *testing.T) { func TestForestStatusFiltering(t *testing.T) { dimensionConfigs := map[string]*DimensionConfig{ - "region": NewDimensionConfig("region", 0, false, 10.0), + "region": NewDimensionConfig("region", 0, false), } forest := CreateRuleForest(dimensionConfigs) @@ -170,7 +170,7 @@ func TestForestStatusFiltering(t *testing.T) { func TestForestNoDuplicateChecks(t *testing.T) { dimensionConfigs := map[string]*DimensionConfig{ - "region": NewDimensionConfig("region", 0, false, 10.0), + "region": NewDimensionConfig("region", 0, false), } forest := CreateRuleForest(dimensionConfigs) @@ -211,7 +211,7 @@ func TestForestNoDuplicateChecks(t *testing.T) { func TestForestOptimizationEfficiency(t *testing.T) { dimensionConfigs := map[string]*DimensionConfig{ - "region": NewDimensionConfig("region", 0, false, 1.0), // Base weight, rules will use manual weights + "region": NewDimensionConfig("region", 0, false), // Base weight, rules will use manual weights } forest := CreateRuleForest(dimensionConfigs) diff --git a/high_concurrency_test.go b/high_concurrency_test.go index f1860cf..9e9b78f 100644 --- a/high_concurrency_test.go +++ b/high_concurrency_test.go @@ -22,7 +22,7 @@ func TestHighConcurrencyNoPartialRules(t *testing.T) { // Add dimension configurations for i, dimName := range []string{"region", "env", "service", "version", "tier"} { - config := NewDimensionConfig(dimName, i, false, float64(5+i)) + config := NewDimensionConfig(dimName, i, false) config.SetWeight(MatchTypeEqual, float64(10+i*2)) err = engine.AddDimension(config) if err != nil { diff --git a/match_type_weights_test.go b/match_type_weights_test.go index 0bf7776..ebf2876 100644 --- a/match_type_weights_test.go +++ b/match_type_weights_test.go @@ -16,13 +16,13 @@ func TestMatchTypeBasedWeights(t *testing.T) { defer engine.Close() // Create dimension config with different weights per match type - regionConfig := NewDimensionConfig("region", 0, false, 5.0) // Default weight + regionConfig := NewDimensionConfig("region", 0, false) // Default weight regionConfig.SetWeight(MatchTypeEqual, 10.0) // Higher weight for exact matches regionConfig.SetWeight(MatchTypePrefix, 7.0) // Medium weight for prefix matches regionConfig.SetWeight(MatchTypeSuffix, 6.0) // Lower weight for suffix matches regionConfig.SetWeight(MatchTypeAny, 3.0) // Lowest weight for any matches - envConfig := NewDimensionConfig("env", 1, false, 2.0) // Default weight + envConfig := NewDimensionConfig("env", 1, false) // Default weight envConfig.SetWeight(MatchTypeEqual, 8.0) // High weight for exact env matches envConfig.SetWeight(MatchTypeAny, 1.0) // Low weight for any env matches @@ -122,7 +122,7 @@ func TestDynamicConfigsWithMatchTypeWeights(t *testing.T) { defer engine.Close() // Add a basic dimension config with default weights - categoryConfig := NewDimensionConfig("category", 0, false, 5.0) + categoryConfig := NewDimensionConfig("category", 0, false) categoryConfig.SetWeight(MatchTypeEqual, 10.0) categoryConfig.SetWeight(MatchTypePrefix, 7.0) @@ -159,7 +159,7 @@ func TestDynamicConfigsWithMatchTypeWeights(t *testing.T) { } // Test 2: Query with dynamic dimension configs that override weights per match type - dynamicCategoryConfig := NewDimensionConfig("category", 0, false, 1.0) + dynamicCategoryConfig := NewDimensionConfig("category", 0, false) dynamicCategoryConfig.SetWeight(MatchTypeEqual, 50.0) // Much higher weight for exact matches dynamicCategoryConfig.SetWeight(MatchTypePrefix, 30.0) // High weight for prefix matches @@ -199,14 +199,14 @@ func TestMixedMatchTypesInSingleRule(t *testing.T) { defer engine.Close() // Create dimension configs with specific weights per match type - userConfig := NewDimensionConfig("user_id", 0, false, 1.0) + userConfig := NewDimensionConfig("user_id", 0, false) userConfig.SetWeight(MatchTypePrefix, 20.0) - actionConfig := NewDimensionConfig("action", 1, false, 1.0) + actionConfig := NewDimensionConfig("action", 1, false) actionConfig.SetWeight(MatchTypeEqual, 15.0) actionConfig.SetWeight(MatchTypeSuffix, 8.0) - serviceConfig := NewDimensionConfig("service", 2, false, 1.0) + serviceConfig := NewDimensionConfig("service", 2, false) serviceConfig.SetWeight(MatchTypeAny, 5.0) // Add dimension configs @@ -261,7 +261,7 @@ func TestMixedMatchTypesInSingleRule(t *testing.T) { t.Logf(" Total: %.1f", matches[0].TotalWeight) } -func TestFallbackToDefaultWeight(t *testing.T) { +func TestFallbackToZeroWeight(t *testing.T) { // Create a temporary directory for this test tempDir := t.TempDir() @@ -273,9 +273,9 @@ func TestFallbackToDefaultWeight(t *testing.T) { defer engine.Close() // Create dimension config with only some match type weights defined - statusConfig := NewDimensionConfig("status", 0, false, 10.0) // Default weight - statusConfig.SetWeight(MatchTypeEqual, 25.0) // Only define weight for Equal match type - // MatchTypePrefix, MatchTypeSuffix, MatchTypeAny will use default weight + statusConfig := NewDimensionConfig("status", 0, false) // No default weight anymore + statusConfig.SetWeight(MatchTypeEqual, 25.0) // Only define weight for Equal match type + // MatchTypePrefix, MatchTypeSuffix, MatchTypeAny will use 0.0 weight err = engine.AddDimension(statusConfig) if err != nil { @@ -328,8 +328,8 @@ func TestFallbackToDefaultWeight(t *testing.T) { } // Verify weights - expectedEqualWeight := 25.0 // Uses specific weight for MatchTypeEqual - expectedPrefixWeight := 10.0 // Falls back to default weight + expectedEqualWeight := 25.0 // Uses specific weight for MatchTypeEqual + expectedPrefixWeight := 0.0 // Falls back to 0.0 weight (no DefaultWeight anymore) if equalMatch.TotalWeight != expectedEqualWeight { t.Errorf("Equal rule: expected weight %.1f, got %.1f", expectedEqualWeight, equalMatch.TotalWeight) diff --git a/matcher_test.go b/matcher_test.go index bbdfb54..44777d3 100644 --- a/matcher_test.go +++ b/matcher_test.go @@ -9,11 +9,11 @@ import ( // Helper function to add test dimensions for backward compatibility func addTestDimensions(engine *InMemoryMatcher) error { dimensions := []*DimensionConfig{ - NewDimensionConfig("product", 0, true, 10.0), - NewDimensionConfig("route", 1, false, 5.0), - NewDimensionConfig("tool", 2, false, 8.0), - NewDimensionConfig("tool_id", 3, false, 3.0), - NewDimensionConfig("recipe", 4, false, 12.0), + NewDimensionConfig("product", 0, true), + NewDimensionConfig("route", 1, false), + NewDimensionConfig("tool", 2, false), + NewDimensionConfig("tool_id", 3, false), + NewDimensionConfig("recipe", 4, false), } for _, dim := range dimensions { @@ -70,8 +70,8 @@ func TestBasicMatching(t *testing.T) { t.Errorf("Expected rule 'test_rule', got '%s'", result.Rule.ID) } - if result.TotalWeight != 23.0 { // 10 + 5 + 8 - t.Errorf("Expected weight 23.0, got %.1f", result.TotalWeight) + if result.TotalWeight != 0.0 { // No explicit weights set + t.Errorf("Expected weight 0.0, got %.1f", result.TotalWeight) } } @@ -417,7 +417,7 @@ func TestDynamicDimensions(t *testing.T) { } // Add custom dimension - customDim := NewDimensionConfig("custom_dimension", 5, false, 20.0) + customDim := NewDimensionConfig("custom_dimension", 5, false) err = engine.AddDimension(customDim) if err != nil { @@ -524,12 +524,12 @@ func TestDimensionConsistencyValidation(t *testing.T) { } // Test 2: Configure dimensions - err = engine.AddDimension(NewDimensionConfig("product", 0, true, 10.0)) + err = engine.AddDimension(NewDimensionConfig("product", 0, true)) if err != nil { t.Fatalf("Failed to add product dimension: %v", err) } - err = engine.AddDimension(NewDimensionConfig("route", 1, false, 5.0)) + err = engine.AddDimension(NewDimensionConfig("route", 1, false)) if err != nil { t.Fatalf("Failed to add route dimension: %v", err) } @@ -599,15 +599,17 @@ func TestRebuild(t *testing.T) { t.Fatalf("Failed to initialize dimensions: %v", err) } - // Add some rules + // Add some rules with different weights to avoid conflicts rule1 := NewRule("rebuild_test_1"). Dimension("product", "Product1", MatchTypeEqual). Dimension("route", "Route1", MatchTypeEqual). + ManualWeight(1.0). Build() rule2 := NewRule("rebuild_test_2"). Dimension("product", "Product2", MatchTypeEqual). Dimension("tool", "Tool2", MatchTypeEqual). + ManualWeight(2.0). Build() err = engine.AddRule(rule1) diff --git a/multitenant_coverage_test.go b/multitenant_coverage_test.go index ded1519..0b72f5a 100644 --- a/multitenant_coverage_test.go +++ b/multitenant_coverage_test.go @@ -174,8 +174,8 @@ func TestSetAllowDuplicateWeights(t *testing.T) { func TestInitializeDimension(t *testing.T) { // Create forest with dimension configs dimensionConfigs := map[string]*DimensionConfig{ - "product": NewDimensionConfig("product", 0, true, 10.0), - "route": NewDimensionConfig("route", 1, false, 5.0), + "product": NewDimensionConfig("product", 0, true), + "route": NewDimensionConfig("route", 1, false), } forest := CreateRuleForest(dimensionConfigs) diff --git a/performance_test.go b/performance_test.go index 15a0d91..f433463 100644 --- a/performance_test.go +++ b/performance_test.go @@ -200,7 +200,6 @@ func generateDimensions(count int) []*DimensionConfig { dimensionNames[i%len(dimensionNames)]+fmt.Sprintf("_%d", i/len(dimensionNames)), i, i < 3, // First 3 dimensions are required - float64(10-i%10), // Varying weights ) } return dimensions diff --git a/persistence_coverage_test.go b/persistence_coverage_test.go index 93bc395..4579e35 100644 --- a/persistence_coverage_test.go +++ b/persistence_coverage_test.go @@ -84,7 +84,7 @@ func TestPersistenceErrorCases(t *testing.T) { // Test SaveDimensionConfigs error case configs := []*DimensionConfig{ - NewDimensionConfig("test", 0, false, 1.0), + NewDimensionConfig("test", 0, false), } err = persistence.SaveDimensionConfigs(ctx, configs) @@ -179,8 +179,8 @@ func TestKafkaEventSubscriberCoverage(t *testing.T) { func TestForestCandidateRulesWithRule(t *testing.T) { // Create forest with dimension configs dimensionConfigs := map[string]*DimensionConfig{ - "product": NewDimensionConfig("product", 0, true, 10.0), - "route": NewDimensionConfig("route", 1, false, 5.0), + "product": NewDimensionConfig("product", 0, true), + "route": NewDimensionConfig("route", 1, false), } forest := CreateRuleForest(dimensionConfigs) diff --git a/persistence_test.go b/persistence_test.go index cb0948c..99d2878 100644 --- a/persistence_test.go +++ b/persistence_test.go @@ -47,8 +47,8 @@ func TestPersistenceSaveDimensionConfigs(t *testing.T) { // Create test dimension configs dims := []*DimensionConfig{ - NewDimensionConfig("region", 0, true, 1.0), - NewDimensionConfig("env", 1, false, 0.5), + NewDimensionConfig("region", 0, true), + NewDimensionConfig("env", 1, false), } // Test SaveDimensionConfigs @@ -168,7 +168,7 @@ func TestDatabasePersistenceOperations(t *testing.T) { // Test SaveDimensionConfigs testDims := []*DimensionConfig{ - NewDimensionConfig("region", 0, true, 1.0), + NewDimensionConfig("region", 0, true), } if err := persistence.SaveDimensionConfigs(ctx, testDims); err != nil { t.Errorf("SaveDimensionConfigs failed: %v", err) diff --git a/race_condition_test.go b/race_condition_test.go index 4353121..c0fef46 100644 --- a/race_condition_test.go +++ b/race_condition_test.go @@ -21,9 +21,9 @@ func TestGetRuleDuringUpdateRaceCondition(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension config - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) err = engine.AddDimension(regionConfig) @@ -230,9 +230,9 @@ func TestQueryDuringUpdateConsistency(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension config - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) err = engine.AddDimension(regionConfig) diff --git a/simple_atomic_test.go b/simple_atomic_test.go index d9c6e65..36572c3 100644 --- a/simple_atomic_test.go +++ b/simple_atomic_test.go @@ -17,7 +17,7 @@ func TestSimpleAtomicUpdate(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension config - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -97,7 +97,7 @@ func TestUpdateRuleTemporaryUnavailability(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension config - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { diff --git a/simple_race_test.go b/simple_race_test.go index 478b9f7..42089a3 100644 --- a/simple_race_test.go +++ b/simple_race_test.go @@ -18,7 +18,7 @@ func TestSimpleRaceCondition(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimension config - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) err = engine.AddDimension(regionConfig) if err != nil { @@ -116,9 +116,9 @@ func TestAtomicUpdate(t *testing.T) { engine.SetAllowDuplicateWeights(true) // Add dimensions - regionConfig := NewDimensionConfig("region", 0, false, 5.0) + regionConfig := NewDimensionConfig("region", 0, false) regionConfig.SetWeight(MatchTypeEqual, 10.0) - envConfig := NewDimensionConfig("env", 1, false, 3.0) + envConfig := NewDimensionConfig("env", 1, false) envConfig.SetWeight(MatchTypeEqual, 8.0) engine.AddDimension(regionConfig) diff --git a/types.go b/types.go index baf3691..dc0a846 100644 --- a/types.go +++ b/types.go @@ -38,25 +38,23 @@ func (mt MatchType) String() string { } } -// NewDimensionConfig creates a DimensionConfig with a default weight for all match types -func NewDimensionConfig(name string, index int, required bool, defaultWeight float64) *DimensionConfig { +// NewDimensionConfig creates a DimensionConfig with empty weights map +func NewDimensionConfig(name string, index int, required bool) *DimensionConfig { return &DimensionConfig{ - Name: name, - Index: index, - Required: required, - Weights: make(map[MatchType]float64), - DefaultWeight: defaultWeight, + Name: name, + Index: index, + Required: required, + Weights: make(map[MatchType]float64), } } // NewDimensionConfigWithWeights creates a DimensionConfig with specific weights per match type -func NewDimensionConfigWithWeights(name string, index int, required bool, weights map[MatchType]float64, defaultWeight float64) *DimensionConfig { +func NewDimensionConfigWithWeights(name string, index int, required bool, weights map[MatchType]float64) *DimensionConfig { return &DimensionConfig{ - Name: name, - Index: index, - Required: required, - Weights: weights, - DefaultWeight: defaultWeight, + Name: name, + Index: index, + Required: required, + Weights: weights, } } @@ -68,12 +66,12 @@ func (dc *DimensionConfig) SetWeight(matchType MatchType, weight float64) { dc.Weights[matchType] = weight } -// GetWeight returns the weight for a specific match type, falling back to default weight +// GetWeight returns the weight for a specific match type, returning 0.0 if not configured func (dc *DimensionConfig) GetWeight(matchType MatchType) float64 { if weight, exists := dc.Weights[matchType]; exists { return weight } - return dc.DefaultWeight + return 0.0 } // DimensionConfig defines the configuration for a dimension @@ -82,7 +80,6 @@ type DimensionConfig struct { Index int `json:"index"` // Order of this dimension Required bool `json:"required"` // Whether this dimension is required for matching Weights map[MatchType]float64 `json:"weights"` // Weights for each match type - DefaultWeight float64 `json:"default_weight"` // Fallback weight for undefined match types TenantID string `json:"tenant_id,omitempty"` // Tenant identifier for multi-tenancy ApplicationID string `json:"application_id,omitempty"` // Application identifier for multi-application support } @@ -224,12 +221,12 @@ func (r *Rule) CalculateTotalWeight(dimensionConfigs map[string]*DimensionConfig if weight, hasWeight := config.Weights[dim.MatchType]; hasWeight { total += weight } else { - // Fall back to default weight if match type not configured - total += config.DefaultWeight + // Use 0.0 when no specific weight is configured for this match type + total += 0.0 } } else { - // If no configuration exists, use a default weight of 1.0 - total += 1.0 + // If no configuration exists, use a default weight of 0.0 + total += 0.0 } } return total diff --git a/weight_population_test.go b/weight_population_test.go index 930ee49..364791f 100644 --- a/weight_population_test.go +++ b/weight_population_test.go @@ -14,12 +14,12 @@ func TestAutomaticWeightPopulation(t *testing.T) { defer engine.Close() // Add dimension configurations with specific weights - err = engine.AddDimension(NewDimensionConfig("product", 0, false, 15.0)) + err = engine.AddDimension(NewDimensionConfig("product", 0, false)) if err != nil { t.Fatalf("Failed to add product dimension: %v", err) } - err = engine.AddDimension(NewDimensionConfig("environment", 1, false, 8.0)) + err = engine.AddDimension(NewDimensionConfig("environment", 1, false)) if err != nil { t.Fatalf("Failed to add environment dimension: %v", err) } @@ -49,13 +49,13 @@ func TestAutomaticWeightPopulation(t *testing.T) { // Verify total weight calculation totalWeight := rule.CalculateTotalWeight(engine.dimensionConfigs) - expectedWeight := 15.0 + 8.0 + expectedWeight := 0.0 // No explicit weights set if totalWeight != expectedWeight { t.Errorf("Expected total weight %.1f, got %.1f", expectedWeight, totalWeight) } } -func TestDefaultWeightWhenNoDimensionConfig(t *testing.T) { +func TestZeroWeightWhenNoDimensionConfig(t *testing.T) { // Create an engine without dimension configurations persistence := NewJSONPersistence("./test_data_default_weight") engine, err := NewInMemoryMatcher(persistence, nil, "test-default-weight") @@ -70,13 +70,13 @@ func TestDefaultWeightWhenNoDimensionConfig(t *testing.T) { Dimension("environment", "prod", MatchTypeEqual). Build() - // Add the rule - weights should default to 1.0 + // Add the rule - weights should default to 0.0 err = engine.AddRule(rule) if err != nil { t.Fatalf("Failed to add rule: %v", err) } - // Verify the weights were set to default 1.0 + // Verify the weights were set to default 0.0 productDim := rule.GetDimensionValue("product") if productDim == nil { t.Fatal("Product dimension not found") @@ -89,7 +89,7 @@ func TestDefaultWeightWhenNoDimensionConfig(t *testing.T) { // Verify total weight calculation totalWeight := rule.CalculateTotalWeight(engine.dimensionConfigs) - expectedWeight := 1.0 + 1.0 + expectedWeight := 0.0 + 0.0 // No dimension configs = 0.0 weight each if totalWeight != expectedWeight { t.Errorf("Expected total weight %.1f, got %.1f", expectedWeight, totalWeight) } @@ -105,12 +105,12 @@ func TestDimensionWithWeightBackwardCompatibility(t *testing.T) { defer engine.Close() // Add dimension configurations with specific weights - err = engine.AddDimension(NewDimensionConfig("product", 0, false, 15.0)) + err = engine.AddDimension(NewDimensionConfig("product", 0, false)) if err != nil { t.Fatalf("Failed to add product dimension: %v", err) } - err = engine.AddDimension(NewDimensionConfig("environment", 1, false, 5.0)) + err = engine.AddDimension(NewDimensionConfig("environment", 1, false)) if err != nil { t.Fatalf("Failed to add environment dimension: %v", err) }