Skip to content

Commit 2a7ffff

Browse files
Copilotcloorc
authored andcommitted
Fix major API discrepancies in README.md
Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
1 parent e3fad66 commit 2a7ffff

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

README.md

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ The rule matching engine now automatically populates dimension weights from dime
8989

9090
```go
9191
// Configure dimensions with weights for different match types
92-
engine.AddDimension(NewDimensionConfig("product", 0, true, 15.0))
93-
engine.AddDimension(NewDimensionConfig("environment", 1, false, 8.0))
92+
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true).
93+
SetWeight(matcher.MatchTypeEqual, 15.0))
94+
engine.AddDimension(matcher.NewDimensionConfig("environment", 1, false).
95+
SetWeight(matcher.MatchTypeEqual, 8.0))
9496

9597
// Or create with specific weights per match type
96-
productConfig := NewDimensionConfigWithWeights("product", 0, true, map[MatchType]float64{
97-
MatchTypeEqual: 15.0,
98-
MatchTypePrefix: 10.0,
99-
MatchTypeSuffix: 8.0,
100-
}, 5.0) // default weight for undefined match types
98+
productConfig := matcher.NewDimensionConfigWithWeights("product", 0, true, map[matcher.MatchType]float64{
99+
matcher.MatchTypeEqual: 15.0,
100+
matcher.MatchTypePrefix: 10.0,
101+
matcher.MatchTypeSuffix: 8.0,
102+
})
101103
engine.AddDimension(productConfig)
102104

103105
// Create rules without specifying weights - they're auto-populated!
@@ -109,20 +111,21 @@ rule := matcher.NewRule("auto-weight-rule").
109111

110112
### Backward Compatibility
111113

112-
For cases where you need explicit weight control, use `DimensionWithWeight()`:
114+
For cases where you need explicit weight control, use `ManualWeight()` to override the calculated weight:
113115

114116
```go
115117
rule := matcher.NewRule("explicit-weight-rule").
116-
Dimension("product", "ProductA", matcher.MatchTypeEqual). // Auto: 15.0 from config
117-
DimensionWithWeight("environment", "prod", matcher.MatchTypeEqual, 12.0). // Explicit: 12.0
118+
Dimension("product", "ProductA", matcher.MatchTypeEqual). // Weight: 15.0 from config
119+
Dimension("environment", "prod", matcher.MatchTypeEqual). // Weight: 8.0 from config
120+
ManualWeight(20.0). // Override total weight to 20.0 (instead of calculated 23.0)
118121
Build()
119122
```
120123

121124
### Weight Resolution
122125

123-
1. **Configured dimensions**: Use weight from `DimensionConfig.Weight`
124-
2. **Explicit weights**: Use weight from `DimensionWithWeight()` method
125-
3. **Unconfigured dimensions**: Default to weight `1.0`
126+
1. **Automatic weights**: Dimensions automatically get weights from `DimensionConfig` based on match type
127+
2. **Manual weight override**: Use `ManualWeight()` method to set total rule weight explicitly
128+
3. **Unconfigured dimensions**: Default to weight `0.0` when no dimension config exists
126129

127130
## Dimension Consistency Validation
128131

@@ -139,9 +142,12 @@ By default, the system enforces consistent rule structures once dimensions are c
139142
engine := matcher.NewMatcherEngineWithDefaults("./data")
140143

141144
// Configure dimensions first
142-
engine.AddDimension(NewDimensionConfig("product", 0, true, 10.0))
143-
engine.AddDimension(NewDimensionConfig("environment", 1, true, 8.0))
144-
engine.AddDimension(NewDimensionConfig("region", 2, false, 5.0))
145+
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true).
146+
SetWeight(matcher.MatchTypeEqual, 10.0))
147+
engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true).
148+
SetWeight(matcher.MatchTypeEqual, 8.0))
149+
engine.AddDimension(matcher.NewDimensionConfig("region", 2, false).
150+
SetWeight(matcher.MatchTypeEqual, 5.0))
145151
```
146152

147153
### Rule Validation
@@ -191,14 +197,16 @@ engine := matcher.NewMatcherEngineWithDefaults("./data")
191197

192198
// Default: duplicate weights are not allowed
193199
rule1 := matcher.NewRule("rule1").
194-
DimensionWithWeight("product", "ProductA", matcher.MatchTypeEqual, 10.0).
195-
DimensionWithWeight("environment", "production", matcher.MatchTypeEqual, 5.0).
196-
Build() // Total weight: 15.0
200+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
201+
Dimension("environment", "production", matcher.MatchTypeEqual).
202+
ManualWeight(15.0).
203+
Build()
197204

198205
rule2 := matcher.NewRule("rule2").
199-
DimensionWithWeight("product", "ProductB", matcher.MatchTypeEqual, 7.0).
200-
DimensionWithWeight("environment", "staging", matcher.MatchTypeEqual, 8.0).
201-
Build() // Total weight: 15.0 (same as rule1)
206+
Dimension("product", "ProductB", matcher.MatchTypeEqual).
207+
Dimension("environment", "staging", matcher.MatchTypeEqual).
208+
ManualWeight(15.0). // Same as rule1
209+
Build()
202210

203211
engine.AddRule(rule1) // ✅ Success
204212
engine.AddRule(rule2) // ❌ Error: weight conflict
@@ -213,15 +221,15 @@ engine.AddRule(rule2) // ✅ Success
213221
Weight conflicts are detected based on the total calculated weight:
214222

215223
```go
216-
// Calculated weight: sum of all dimension weights
224+
// Calculated weight: sum of all dimension weights from configs
217225
rule1 := matcher.NewRule("calculated").
218-
DimensionWithWeight("product", "ProductA", matcher.MatchTypeEqual, 10.0).
219-
DimensionWithWeight("route", "main", matcher.MatchTypeEqual, 5.0).
226+
Dimension("product", "ProductA", matcher.MatchTypeEqual). // 10.0 from config
227+
Dimension("route", "main", matcher.MatchTypeEqual). // 5.0 from config
220228
Build() // Total weight: 15.0
221229

222230
// Manual weight: overrides calculated weight
223231
rule2 := matcher.NewRule("manual").
224-
DimensionWithWeight("product", "ProductB", matcher.MatchTypeEqual, 20.0).
232+
Dimension("product", "ProductB", matcher.MatchTypeEqual). // Would be 10.0 from config
225233
ManualWeight(15.0). // Total weight: 15.0 (conflicts with rule1)
226234
Build()
227235

@@ -328,14 +336,19 @@ func main() {
328336
}
329337
defer engine.Close()
330338

331-
// Initialize default dimensions
332-
engine.InitializeDefaultDimensions()
339+
// Add dimensions with weights
340+
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true).
341+
SetWeight(matcher.MatchTypeEqual, 10.0))
342+
engine.AddDimension(matcher.NewDimensionConfig("route", 1, false).
343+
SetWeight(matcher.MatchTypeEqual, 5.0))
344+
engine.AddDimension(matcher.NewDimensionConfig("tool", 2, false).
345+
SetWeight(matcher.MatchTypeEqual, 8.0))
333346

334347
// Add a rule
335348
rule := matcher.NewRule("production_rule").
336-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
337-
Route("main", matcher.MatchTypeEqual, 5.0).
338-
Tool("laser", matcher.MatchTypeEqual, 8.0).
349+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
350+
Dimension("route", "main", matcher.MatchTypeEqual).
351+
Dimension("tool", "laser", matcher.MatchTypeEqual).
339352
Build()
340353

341354
engine.AddRule(rule)
@@ -370,13 +383,6 @@ func main() {
370383
log.Fatal(err)
371384
}
372385
}
373-
```
374-
375-
if result != nil {
376-
fmt.Printf("Best match: %s (weight: %.2f)\n",
377-
result.Rule.ID, result.TotalWeight)
378-
}
379-
}
380386
```
381387

382388
## 🎯 Match Types Examples
@@ -385,7 +391,7 @@ func main() {
385391

386392
```go
387393
rule := matcher.NewRule("exact_rule").
388-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
394+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
389395
Build()
390396
// Matches: "ProductA" exactly
391397
// Doesn't match: "ProductB", "ProductABC", "productA"
@@ -395,7 +401,7 @@ rule := matcher.NewRule("exact_rule").
395401

396402
```go
397403
rule := matcher.NewRule("prefix_rule").
398-
Product("Prod", matcher.MatchTypePrefix, 8.0).
404+
Dimension("product", "Prod", matcher.MatchTypePrefix).
399405
Build()
400406
// Matches: "Prod", "ProductA", "Production", "Produce"
401407
// Doesn't match: "MyProduct", "prod" (case sensitive)
@@ -405,7 +411,7 @@ rule := matcher.NewRule("prefix_rule").
405411

406412
```go
407413
rule := matcher.NewRule("suffix_rule").
408-
Tool("_beta", matcher.MatchTypeSuffix, 10.0).
414+
Dimension("tool", "_beta", matcher.MatchTypeSuffix).
409415
Build()
410416
// Matches: "tool_beta", "test_beta", "version_beta"
411417
// Doesn't match: "beta_test", "_beta_version"
@@ -415,8 +421,8 @@ rule := matcher.NewRule("suffix_rule").
415421

416422
```go
417423
rule := matcher.NewRule("fallback_rule").
418-
Product("", matcher.MatchTypeAny, 0.0). // Empty value for Any match
419-
Route("main", matcher.MatchTypeEqual, 5.0).
424+
Dimension("product", "", matcher.MatchTypeAny). // Empty value for Any match
425+
Dimension("route", "main", matcher.MatchTypeEqual).
420426
ManualWeight(5.0).
421427
Build()
422428
// Matches: any product value when route="main"
@@ -431,9 +437,9 @@ The engine supports partial queries where you don't specify all dimensions:
431437
```go
432438
// Rule with 3 dimensions
433439
rule := matcher.NewRule("three_dim_rule").
434-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
435-
Route("main", matcher.MatchTypeEqual, 5.0).
436-
Tool("", matcher.MatchTypeAny, 0.0). // Use MatchTypeAny for optional dimensions
440+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
441+
Dimension("route", "main", matcher.MatchTypeEqual).
442+
Dimension("tool", "", matcher.MatchTypeAny). // Use MatchTypeAny for optional dimensions
437443
Build()
438444

439445
// Partial query with only 2 dimensions
@@ -456,14 +462,14 @@ The engine supports excluding specific rules from query results, useful for A/B
456462
```go
457463
// Create multiple rules
458464
rule1 := matcher.NewRule("rule1").
459-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
460-
Route("main", matcher.MatchTypeEqual, 5.0).
465+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
466+
Dimension("route", "main", matcher.MatchTypeEqual).
461467
ManualWeight(15.0).
462468
Build()
463469

464470
rule2 := matcher.NewRule("rule2").
465-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
466-
Route("main", matcher.MatchTypeEqual, 5.0).
471+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
472+
Dimension("route", "main", matcher.MatchTypeEqual).
467473
ManualWeight(10.0).
468474
Build()
469475

@@ -498,12 +504,13 @@ allMatches, _ := engine.FindAllMatches(excludeQuery) // Returns only rule2
498504

499505
```go
500506
// Add custom dimension
501-
customDim := NewDimensionConfig("region", 5, false, 15.0)
507+
customDim := matcher.NewDimensionConfig("region", 5, false).
508+
SetWeight(matcher.MatchTypeEqual, 15.0)
502509
engine.AddDimension(customDim)
503510

504511
// Use in rules
505512
rule := matcher.NewRule("regional_rule").
506-
Product("ProductA", matcher.MatchTypeEqual, 10.0).
513+
Dimension("product", "ProductA", matcher.MatchTypeEqual).
507514
Dimension("region", "us-west", matcher.MatchTypeEqual).
508515
Build()
509516
```
@@ -527,8 +534,10 @@ longRule := matcher.NewRule("long").
527534
Build()
528535

529536
// With configured dimensions - consistent rule structure required
530-
engine.AddDimension(NewDimensionConfig("product", 0, true, 10.0))
531-
engine.AddDimension(NewDimensionConfig("route", 1, false, 5.0))
537+
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true).
538+
SetWeight(matcher.MatchTypeEqual, 10.0))
539+
engine.AddDimension(matcher.NewDimensionConfig("route", 1, false).
540+
SetWeight(matcher.MatchTypeEqual, 5.0))
532541

533542
// Now all rules must conform to these dimensions
534543
validRule := matcher.NewRule("valid").

0 commit comments

Comments
 (0)