1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use smol_str::SmolStr;
pub fn object_ref_matches_table(
possible_references: &[Vec<SmolStr>],
targets: &[Vec<SmolStr>],
) -> bool {
// Simple case: If there are no references, assume okay.
if possible_references.is_empty() {
return true;
}
// Simple case: Reference exactly matches a target.
for pr in possible_references {
if targets.contains(pr) {
return true;
}
}
// Tricky case: If one is shorter than the other, check for a suffix match.
for pr in possible_references {
for t in targets {
if (pr.len() < t.len() && pr == &t[t.len() - pr.len()..])
|| (t.len() < pr.len() && t == &pr[pr.len() - t.len()..])
{
return true;
}
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_object_ref_matches_table() {
let test_cases = vec![
// Empty list of references is always true
(vec![], vec![vec!["abc".into()]], true),
// Simple cases: one reference, one target
(
vec![vec!["agent1".into()]],
vec![vec!["agent1".into()]],
true,
),
(
vec![vec!["agent1".into()]],
vec![vec!["customer".into()]],
false,
),
// Multiple references. If any match, good.
(
vec![vec!["bar".into()], vec!["user_id".into()]],
vec![vec!["bar".into()]],
true,
),
(
vec![vec!["foo".into()], vec!["user_id".into()]],
vec![vec!["bar".into()]],
false,
),
// Multiple targets. If any reference matches, good.
(
vec![vec!["table1".into()]],
vec![
vec!["table1".into()],
vec!["table2".into()],
vec!["table3".into()],
],
true,
),
(
vec![vec!["tbl2".into()]],
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
false,
),
(
vec![vec!["tbl2".into()]],
vec![vec!["db".into(), "sc".into(), "tbl2".into()]],
true,
),
// Multipart references and targets. Checks for a suffix match.
(
vec![vec!["Arc".into(), "tbl1".into()]],
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
false,
),
(
vec![vec!["sc".into(), "tbl1".into()]],
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
true,
),
(
vec![vec!["cb".into(), "sc".into(), "tbl1".into()]],
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
false,
),
(
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
true,
),
(
vec![vec!["public".into(), "agent1".into()]],
vec![vec!["agent1".into()]],
true,
),
(
vec![vec!["public".into(), "agent1".into()]],
vec![vec!["public".into()]],
false,
),
];
for (possible_references, targets, expected) in test_cases {
assert_eq!(
object_ref_matches_table(&possible_references, &targets),
expected
);
}
}
}