Skip to content

Commit 259ad9f

Browse files
committed
Add the route ID to uuid
Signed-off-by: Yijie Qin <[email protected]>
1 parent f67d03f commit 259ad9f

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

dispatch/route.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ func (r *Route) Key() string {
180180
return b.String()
181181
}
182182

183+
// ID returns a key for the route. It should uniquely identify the route in general.
184+
func (r *Route) ID() string {
185+
b := strings.Builder{}
186+
187+
var position *int
188+
if r.parent != nil {
189+
// Find the position in the same level leaf.
190+
for i, cr := range r.parent.Routes {
191+
if cr == r {
192+
position = &i
193+
break
194+
}
195+
}
196+
}
197+
b.WriteString(r.Key())
198+
199+
if position != nil {
200+
b.WriteRune('/')
201+
b.WriteString(fmt.Sprint(*position))
202+
}
203+
return b.String()
204+
}
205+
183206
// Walk traverses the route tree in depth-first order.
184207
func (r *Route) Walk(visit func(*Route)) {
185208
visit(r)

dispatch/route_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,3 +853,80 @@ routes:
853853
}
854854
}
855855
}
856+
857+
func TestRouteID(t *testing.T) {
858+
in := `
859+
receiver: 'notify-def'
860+
861+
routes:
862+
- matchers: ['{owner="team-A"}', '{level!="critical"}']
863+
864+
receiver: 'notify-A'
865+
866+
routes:
867+
- matchers: ['{env="testing"}', '{baz!~".*quux"}']
868+
869+
receiver: 'notify-testing'
870+
group_by: [...]
871+
872+
- match:
873+
env: "production"
874+
875+
receiver: 'notify-productionA'
876+
group_wait: 1m
877+
878+
continue: true
879+
880+
- matchers: [ env=~"produ.*", job=~".*"]
881+
882+
receiver: 'notify-productionB'
883+
group_wait: 30s
884+
group_interval: 5m
885+
repeat_interval: 1h
886+
group_by: ['job']
887+
888+
- match_re:
889+
owner: 'team-(B|C)'
890+
891+
group_by: ['foo', 'bar']
892+
group_wait: 2m
893+
receiver: 'notify-BC'
894+
895+
- matchers: [group_by="role"]
896+
group_by: ['role']
897+
898+
routes:
899+
- matchers: ['{env="testing"}']
900+
receiver: 'notify-testing'
901+
routes:
902+
- matchers: [wait="long"]
903+
group_wait: 2m
904+
`
905+
906+
var ctree config.Route
907+
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
908+
t.Fatal(err)
909+
}
910+
var (
911+
tree = NewRoute(&ctree, nil)
912+
)
913+
914+
tests := []struct {
915+
id string
916+
}{
917+
{
918+
id: "{}/{level!=\"critical\",owner=\"team-A\"}/0",
919+
},
920+
{
921+
id: "{}/{owner=~\"^(?:team-(B|C))$\"}/1",
922+
},
923+
{
924+
id: "{}/{group_by=\"role\"}/2",
925+
},
926+
}
927+
928+
for i, test := range tests {
929+
id := tree.Routes[i].ID()
930+
require.Equal(t, test.id, id)
931+
}
932+
}

0 commit comments

Comments
 (0)