17
17
18
18
import java .util .HashMap ;
19
19
import java .util .Map ;
20
+ import java .util .Set ;
20
21
import software .amazon .smithy .model .Model ;
21
- import software .amazon .smithy .model .shapes . CollectionShape ;
22
+ import software .amazon .smithy .model .selector . Selector ;
22
23
import software .amazon .smithy .model .shapes .MapShape ;
23
24
import software .amazon .smithy .model .shapes .MemberShape ;
24
25
import software .amazon .smithy .model .shapes .Shape ;
25
- import software .amazon .smithy .model .shapes .SimpleShape ;
26
26
import software .amazon .smithy .model .traits .SensitiveTrait ;
27
27
28
28
public class SensitiveDataFinder {
@@ -44,34 +44,20 @@ private boolean findRecursive(Shape shape, Model model) {
44
44
return true ;
45
45
}
46
46
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 );
47
+ if (shape .getMemberTrait (model , SensitiveTrait .class ).isPresent ()) {
48
+ cache .put (shape , true );
49
+ return true ;
55
50
}
56
51
57
- if (shape .getMemberTrait (model , SensitiveTrait .class ).isPresent ()) {
52
+ Selector selector = Selector .parse ("[id = '" + shape .getId () + "']" + " ~> [trait|sensitive]" );
53
+ Set <Shape > matches = selector .select (model );
54
+ boolean found = !matches .isEmpty ();
55
+ if (found ) {
58
56
cache .put (shape , true );
59
57
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 ));
58
+ }
68
59
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 ) {
60
+ if (shape instanceof MapShape ) {
75
61
MemberShape keyMember = ((MapShape ) shape ).getKey ();
76
62
MemberShape valMember = ((MapShape ) shape ).getValue ();
77
63
return findRecursive (keyMember , model ) || findRecursive (valMember , model );
0 commit comments