Skip to content

Commit

Permalink
Fix match statements, remove package name from endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
cerrussell committed Jan 15, 2024
1 parent 4603b7f commit a30b8e8
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions atom_tools/lib/slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def import_slice(filename):
return content.get('objectSlices', []), content.get(
'userDefinedTypes', []
)
except [json.decoder.JSONDecodeError, UnicodeDecodeError]:
logging.warning(
f'Failed to load usages slice: {filename}\nPlease check '
f'that you specified a valid json file.'
)
# except [json.decoder.JSONDecodeError, UnicodeDecodeError]:
# logging.warning(
# f'Failed to load usages slice: {filename}\nPlease check '
# f'that you specified a valid json file.'
# )
except FileNotFoundError:
logging.warning(
f'Failed to locate the usages slice file in the location '
Expand All @@ -91,20 +91,13 @@ def generate_endpoints(self):
"""
endpoints = []
for object_slice in self.object_slices:
parent = (
'/' + object_slice.get('fullName', '')
.split(':')[0]
# .split('.')[-1]
.rstrip('/')
)
endpoints.extend(
self.extract_endpoints_from_usages(
object_slice.get('usages', []), parent)
endpoints.extend(self.extract_endpoints_from_usages(
object_slice.get('usages', []))
)
endpoints.extend(self.extract_endpoints_from_udts())
return list(set(endpoints))

def extract_endpoints(self, code, pkg):
def extract_endpoints(self, code):
"""
Extracts endpoints from the given code based on the specified language.
Expand All @@ -120,22 +113,25 @@ def extract_endpoints(self, code, pkg):
return endpoints
matches = re.findall(UsageSlice.ENDPOINTS_REGEX, code) or []
match self.language:
case 'java', 'jar':
case 'java' | 'jar':
if code.startswith('@') and (
'Mapping' in code or 'Path' in code) and '(' in code:
endpoints.extend(
[
pkg + v.replace('"', '').replace("'", "")
f'/{v.replace('"', '')
.replace("'", "")
.lstrip('/')}'
for v in matches
if v and not v.startswith(".")
and "/" in v and not v.startswith("@")
]
)
case 'js', 'ts', 'javascript', 'typescript':
case 'js' | 'ts' | 'javascript' | 'typescript':
if 'app.' in code or 'route' in code:
endpoints.extend(
[
pkg + v.replace('"', '').replace("'", "")
f'/{v.replace('"', '')
.replace("'", "").lstrip('/')}'
for v in matches
if v and not v.startswith(".")
and '/' in v and not v.startswith('@')
Expand All @@ -145,12 +141,15 @@ def extract_endpoints(self, code, pkg):
)
case _:
endpoints.extend([
pkg + v.replace('"', '').replace("'", "").replace('\n', '')
f'/{v.replace('"', '')
.replace("'", "")
.replace('\n', '')
.lstrip('/')}'
for v in matches or [] if len(v) > 2 and '/' in v
])
return endpoints

def extract_endpoints_from_usages(self, usages, pkg):
def extract_endpoints_from_usages(self, usages):
"""
Extracts endpoints from the given list of usages.
Expand All @@ -167,14 +166,14 @@ def extract_endpoints_from_usages(self, usages, pkg):
defined_by = usage.get('definedBy', {})
invoked_calls = usage.get('invokedCalls', [])
if resolved_method := target_obj.get('resolvedMethod'):
endpoints.extend(self.extract_endpoints(resolved_method, pkg))
endpoints.extend(self.extract_endpoints(resolved_method))
elif resolved_method := defined_by.get('resolvedMethod'):
endpoints.extend(self.extract_endpoints(resolved_method, pkg))
endpoints.extend(self.extract_endpoints(resolved_method))
if invoked_calls:
for call in invoked_calls:
if resolved_method := call.get('resolvedMethod'):
endpoints.extend(
self.extract_endpoints(resolved_method, pkg))
self.extract_endpoints(resolved_method))
return endpoints

def extract_endpoints_from_udts(self):
Expand All @@ -186,11 +185,9 @@ def extract_endpoints_from_udts(self):
"""
endpoints = []
for udt in self.user_defined_types:
pkg = udt.get('name')
if fields := udt.get('fields'):
for f in fields:
endpoints.extend(
self.extract_endpoints(f.get('name'), pkg))
endpoints.extend(self.extract_endpoints(f.get('name')))
return endpoints


Expand Down

0 comments on commit a30b8e8

Please sign in to comment.