Skip to content

Commit

Permalink
CTOOLS-373: Added script fix circular reference issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Storey-Finbourne committed Oct 4, 2024
1 parent 03a0e84 commit 99267d5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
36 changes: 36 additions & 0 deletions generate/fix-imports-for-access.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

set -eETuo pipefail

failure() {
local lineno="$1"
local msg="$2"
echo "Failed at $lineno: $msg"
}
trap 'failure ${LINENO} "$BASH_COMMAND"' ERR

file_path="$1"
import_statement="$2"
update_forward_refs="$3"

echo "$file_path"
echo "Fixing imports for Access"


# Check if the import statement is already present
if ! grep -Fxq "$import_statement" "$file_path"; then
# Append the import statement to the bottom of the file
echo "$import_statement" >> "$file_path"
echo "Import statement added to $file_path"
else
echo "Import statement is already present in $file_path"
fi

# Append the additional line if it's not already present
if ! grep -Fxq "$update_forward_refs" "$file_path"; then
# Ensure the file ends with a new line, then append the additional line at the bottom
echo -e "\n$update_forward_refs" >> "$file_path"
echo "Import statement added to $file_path"
else
echo "Import statement is already present in $file_path"
fi
27 changes: 23 additions & 4 deletions generate/templates/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{>partial_header}}

from typing import List
from typing import List, Dict
import unittest
import json
from xeger import Xeger
Expand Down Expand Up @@ -55,7 +55,17 @@ class {{#operations}}Test{{classname}}(unittest.IsolatedAsyncioTestCase):
count = 0
while True:
count += 1
regex = r"{{{vendorExtensions.x-regex}}}"
regex_map = {
r'^(?=.*[a-zA-Z])[\w][\w +-]{2,100}$': "official-portfolios-read-only",
r'^[a-zA-Z0-9\\+/]*={0,3}$': "L=="
}

{{paramName}}: {{{dataType}}} = xegerObj.xeger(r"{{{vendorExtensions.x-regex}}}")

if regex in regex_map:
{{paramName}} = regex_map[regex]

if len({{paramName}}) >= minLength:
break
if count > 100:
Expand Down Expand Up @@ -92,9 +102,18 @@ class {{#operations}}Test{{classname}}(unittest.IsolatedAsyncioTestCase):
example = examples[numRuns - 1]
{{paramName}}: {{dataType}} = example["value"]
numExamples = len(examples)

{{paramName}} = {{dataType}}.from_dict({{paramName}})

tmp_{{paramName}} = {}
if isinstance({{paramName}}, dict):
if hasattr({{paramName}}, 'from_dict') and callable(getattr({{paramName}}, 'from_dict')):
for k, v in {{paramName}}.items():
if isinstance(v, dict):
tmp_{{paramName}}[k] = {{paramName}}.from_dict(v)
else:
break
{{paramName}} = tmp_{{paramName}}
else:
if '{{dataType}}' == '{{complexType}}':
{{paramName}}: {{dataType}} = {{complexType}}.from_dict({{paramName}})
{{/content}}
{{^content}}
{{#isArray}}
Expand Down
16 changes: 15 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ generate-local FLAG="":
finbourne/lusid-sdk-gen-python:latest -- ./generate/generate.sh ./generate ./generate/.output /tmp/swagger.json .config.json
rm -f generate/.output/.openapi-generator-ignore
rm generate/templates/description.mustache

# split the README into two, and move one up a level
bash generate/split-readme.sh

# make the necessary post-generation fixes to the sdks using the 'oneOf' openapi feature
# caused by a bug in the python generator
if [ "{{APPLICATION_NAME}}" = "notifications" ] || [ "{{APPLICATION_NAME}}" = "workflow" ]; then just make-fix-for-one-of; fi

echo "Application name: $APPLICATION_NAME"

if [ "{{APPLICATION_NAME}}" = "access" ]; then just make-import-fix; fi

add-tests:
mkdir -p {{justfile_directory()}}/generate/.output/sdk/test/
rm -rf {{justfile_directory()}}/generate/.output/sdk/test/*
Expand Down Expand Up @@ -150,6 +153,8 @@ generate-cicd TARGET_DIR FLAG="":
# caused by a bug in the python generator
if [ "{{APPLICATION_NAME}}" = "notifications" ] || [ "{{APPLICATION_NAME}}" = "workflow" ]; then just make-fix-for-one-of; fi

if [ "{{APPLICATION_NAME}}" = "access" ]; then just make-import-fix; fi

# need to remove the created content before copying over the top of it.
# this prevents deleted content from hanging around indefinitely.
rm -rf {{TARGET_DIR}}/sdk/${PACKAGE_NAME}
Expand Down Expand Up @@ -203,3 +208,12 @@ generate-and-publish-cicd OUT_DIR FLAG="":
make-fix-for-one-of:
bash {{justfile_directory()}}/generate/fix-files-for-one-of.sh {{justfile_directory()}} ${PACKAGE_NAME} ${APPLICATION_NAME}

make-import-fix:
bash {{justfile_directory()}}/generate/fix-imports-for-access.sh \
{{justfile_directory()}}/generate/.output/sdk/finbourne_access/models/policy_selector_definition.py \
"from finbourne_access.models.selector_definition import SelectorDefinition" \
"PolicySelectorDefinition.update_forward_refs()"
bash {{justfile_directory()}}/generate/fix-imports-for-access.sh \
{{justfile_directory()}}/generate/.output/sdk/finbourne_access/models/selector_definition.py \
"from finbourne_access.models.policy_selector_definition import PolicySelectorDefinition" \
"SelectorDefinition.update_forward_refs()"

0 comments on commit 99267d5

Please sign in to comment.