17
17
18
18
import java .util .HashMap ;
19
19
import java .util .Map ;
20
+ import java .util .Set ;
21
+
20
22
import software .amazon .smithy .model .Model ;
21
- import software .amazon .smithy .model .shapes . CollectionShape ;
23
+ import software .amazon .smithy .model .selector . Selector ;
22
24
import software .amazon .smithy .model .shapes .MapShape ;
23
25
import software .amazon .smithy .model .shapes .MemberShape ;
24
26
import software .amazon .smithy .model .shapes .Shape ;
25
- import software .amazon .smithy .model .shapes .SimpleShape ;
26
27
import software .amazon .smithy .model .traits .SensitiveTrait ;
27
28
28
29
public class SensitiveDataFinder {
@@ -44,34 +45,20 @@ private boolean findRecursive(Shape shape, Model model) {
44
45
return true ;
45
46
}
46
47
47
- if (shape instanceof MemberShape ) {
48
- MemberShape memberShape = (MemberShape ) shape ;
49
- if (memberShape .getMemberTrait (model , SensitiveTrait .class ).isPresent ()) {
50
- cache .put (shape , true );
51
- return true ;
52
- }
53
- Shape memberTarget = model .expectShape (memberShape .getTarget ());
54
- return findRecursive (memberTarget , model );
48
+ if (shape .getMemberTrait (model , SensitiveTrait .class ).isPresent ()) {
49
+ cache .put (shape , true );
50
+ return true ;
55
51
}
56
52
57
- if (shape .getMemberTrait (model , SensitiveTrait .class ).isPresent ()) {
53
+ Selector selector = Selector .parse ("[id = '" + shape .getId () + "']" + " ~> [trait|sensitive]" );
54
+ Set <Shape > matches = selector .select (model );
55
+ boolean found = !matches .isEmpty ();
56
+ if (found ) {
58
57
cache .put (shape , true );
59
58
return true ;
60
- } else if (shape instanceof SimpleShape ) {
61
- cache .put (shape , false );
62
- return false ;
63
- } else if (shape .isStructureShape () || shape .isUnionShape ()) {
64
- boolean found = shape .getAllMembers ()
65
- .values ()
66
- .stream ()
67
- .anyMatch (m -> findRecursive (m , model ));
59
+ }
68
60
69
- cache .put (shape , found );
70
- return found ;
71
- } else if (shape instanceof CollectionShape ) {
72
- MemberShape collectionMember = ((CollectionShape ) shape ).getMember ();
73
- return findRecursive (collectionMember , model );
74
- } else if (shape instanceof MapShape ) {
61
+ if (shape instanceof MapShape ) {
75
62
MemberShape keyMember = ((MapShape ) shape ).getKey ();
76
63
MemberShape valMember = ((MapShape ) shape ).getValue ();
77
64
return findRecursive (keyMember , model ) || findRecursive (valMember , model );
0 commit comments