Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve schema generator #1207

Merged
merged 42 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a724198
fix: json diff output
saikumarrs Dec 18, 2023
7d7a07f
feat: allow updating all files
saikumarrs Dec 18, 2023
307561e
feat: add directory check
saikumarrs Dec 18, 2023
48c073e
fix: avoid reading excluded files
saikumarrs Dec 18, 2023
7c84171
feat: add warning for missing fields in db-config.json
saikumarrs Dec 18, 2023
161a810
fix: avoid generating useNativeSDK schema always
saikumarrs Dec 18, 2023
332771b
chore: fix typos
saikumarrs Dec 18, 2023
887727d
feat: handle dynamicCustomForm and v2 ui-config
saikumarrs Dec 18, 2023
3533f29
fix: in-place schema file update
saikumarrs Dec 18, 2023
02465c8
fix: handle preRequisites for v2 ui config
saikumarrs Dec 18, 2023
65184e3
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Dec 18, 2023
b984edd
feat: generate allOf for new ui config
saikumarrs Dec 18, 2023
fcb9235
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Dec 19, 2023
f44c4b6
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Dec 19, 2023
b5d7b12
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Dec 19, 2023
24ec7bb
feat: keep the encoding of the file intact
saikumarrs Dec 20, 2023
4d16da5
chore: formatting changes
saikumarrs Dec 20, 2023
09da97f
fix: merge conditions
saikumarrs Dec 20, 2023
86b5eb5
Revert "chore: formatting changes"
saikumarrs Dec 20, 2023
d683eb3
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Dec 21, 2023
3f1c479
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 11, 2024
7d0cd7f
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 15, 2024
6dd833f
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 17, 2024
3319d9e
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 19, 2024
97ff123
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 23, 2024
908cafb
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 24, 2024
947f01f
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 29, 2024
a9e69b8
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 30, 2024
03b103c
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 30, 2024
607d813
Merge branch 'develop' into feat.improve-schema-generator-sdk-998
saikumarrs Jan 31, 2024
a0d09c9
Merge remote-tracking branch 'origin/develop' into feat.improve-schem…
saikumarrs Feb 5, 2024
a61c1cd
Merge remote-tracking branch 'origin/develop' into feat.improve-schem…
saikumarrs Feb 12, 2024
b2b36b1
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Feb 16, 2024
6fc3fb7
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Feb 21, 2024
93b3103
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Feb 23, 2024
12be63e
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Feb 27, 2024
ff9005e
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Apr 5, 2024
c60061c
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Apr 26, 2024
1448911
fix: revert unwanted changes
saikumarrs Apr 26, 2024
7d7556f
fix: remove undeleted code
saikumarrs Apr 26, 2024
8c92123
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Apr 29, 2024
6f07641
Merge branch 'develop' into feat.improve-schema-generator-int-1591
saikumarrs Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 72 additions & 21 deletions scripts/schemaGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ def is_dest_field_dependent_on_source(field, dbConfig, schema_field_name):
return False


def is_key_present_in_dest_config(dbConfig, key):
"""Checks if the given key is present in destConfig across all source types.

Args:
dbConfig (object): Destination configuration in db-config.json.
key (string): key to be searched in destConfig tree.

Returns:
boolean: True if the key is present in destConfig else, False.
"""
if not dbConfig:
return False

if "destConfig" in dbConfig:
for configSection in dbConfig["destConfig"]:
if key in dbConfig["destConfig"][configSection]:
return True
return False


def is_field_present_in_default_config(field, dbConfig, schema_field_name):
"""Checks if the given field is present in defaultConfig list present in dbConfig.

Expand Down Expand Up @@ -740,6 +760,7 @@ def generate_schema_for_allOf(uiConfig, dbConfig, schema_field_name):
- For each unique preRequisiteField, the properties are found by matching the current preRequisiteField.
- preRequisiteField becomes if block and corresponding properties become then block.


Args:
uiConfig (object): file content of ui-config.json.
dbConfig (object): Configurations of db-config.json.
Expand Down Expand Up @@ -971,17 +992,14 @@ def generate_connection_mode(dbConfig):
return connectionObj


def generate_schema_properties(
uiConfig, dbConfig, schemaObject, properties, name, selector
):
def generate_schema_properties(uiConfig, dbConfig, schemaObject, properties, selector):
"""Generates corresponding schema properties by iterating over each of the ui-config fields.

Args:
uiConfig (object): file content of ui-config.json.
dbConfig (object): Configurations of db-config.json.
schemaObject (object): schema being generated
properties (object): properties of schema
name (string): name of the source or destination.
selector (string): either 'source' or 'destination'
"""
if is_old_format(uiConfig):
Expand All @@ -992,9 +1010,16 @@ def generate_schema_properties(
continue
generateFunction = uiTypetoSchemaFn.get(field["type"], None)
if generateFunction:
properties[field["value"]] = generateFunction(
field, dbConfig, "value"
)
# Generate schema for the field if it is defined in the destination config
if is_key_present_in_dest_config(dbConfig, field["value"]):
properties[field["value"]] = generateFunction(
field, dbConfig, "value"
)
else:
warnings.warn(
f'The field {field["value"]} is defined in ui-config.json but not in db-config.json\n',
UserWarning,
)
if field.get(
"required", False
) == True and is_field_present_in_default_config(
Expand All @@ -1012,9 +1037,18 @@ def generate_schema_properties(
for field in group.get("fields", []):
generateFunction = uiTypetoSchemaFn.get(field["type"], None)
if generateFunction:
properties[field["configKey"]] = generateFunction(
field, dbConfig, "configKey"
)
# Generate schema for the field if it is defined in the destination config
if is_key_present_in_dest_config(
dbConfig, field["configKey"]
):
properties[field["configKey"]] = generateFunction(
field, dbConfig, "configKey"
)
else:
warnings.warn(
f'The field {field["configKey"]} is defined in ui-config.json but not in db-config.json\n',
UserWarning,
)
if (
template.get("title", "") == "Initial setup"
and is_field_present_in_default_config(
Expand All @@ -1027,9 +1061,17 @@ def generate_schema_properties(
for field in sdkTemplate.get("fields", []):
generateFunction = uiTypetoSchemaFn.get(field["type"], None)
if generateFunction:
properties[field["configKey"]] = generateFunction(
field, dbConfig, "configKey"
)
# Generate schema for the field if it is defined in the destination config
if is_key_present_in_dest_config(dbConfig, field["configKey"]):
properties[field["configKey"]] = generateFunction(
field, dbConfig, "configKey"
)
else:
warnings.warn(
f'The field {field["configKey"]} is defined in ui-config.json but not in db-config.json\n',
UserWarning,
)

if field.get(
"required", False
) == True and is_field_present_in_default_config(
Expand All @@ -1051,9 +1093,12 @@ def generate_schema_properties(
schemaObject["required"].append(field["configKey"])

# default properties in new ui-config based schemas.
schemaObject["properties"]["useNativeSDK"] = generate_schema_for_checkbox(
{"type": "checkbox", "value": "useNativeSDK"}, dbConfig, "value"
)
if is_key_present_in_dest_config(dbConfig, "useNativeSDK"):
schemaObject["properties"]["useNativeSDK"] = (
generate_schema_for_checkbox(
{"type": "checkbox", "value": "useNativeSDK"}, dbConfig, "value"
)
)
schemaObject["properties"]["connectionMode"] = generate_connection_mode(
dbConfig
)
Expand Down Expand Up @@ -1099,6 +1144,7 @@ def generate_schema(uiConfig, dbConfig, name, selector):
schemaObject["type"] = "object"
schemaObject["properties"] = {}
allOfSchemaObj = {}
print(f"Generating schema for {name} {selector}")
if is_old_format(uiConfig):
allOfSchemaObj = generate_schema_for_allOf(uiConfig, dbConfig, "value")
if allOfSchemaObj:
Expand All @@ -1110,8 +1156,9 @@ def generate_schema(uiConfig, dbConfig, name, selector):
schemaObject["anyOf"] = allOfSchemaObj
else:
schemaObject["allOf"] = allOfSchemaObj

generate_schema_properties(
uiConfig, dbConfig, schemaObject, schemaObject["properties"], name, selector
uiConfig, dbConfig, schemaObject, schemaObject["properties"], selector
)
newSchema["configSchema"] = schemaObject

Expand Down Expand Up @@ -1154,7 +1201,10 @@ def generate_warnings_for_each_type(uiConfig, dbConfig, schema, curUiType):
)
if (
curUiType == "textInput"
and field["value"] in schema["required"]
and (
schema.get("required", False) == True
and field["value"] in schema["required"]
)
and "regex" not in field
):
warnings.warn(
Expand Down Expand Up @@ -1204,7 +1254,10 @@ def generate_warnings_for_each_type(uiConfig, dbConfig, schema, curUiType):
)
if (
curUiType == "textInput"
and field["configKey"] in schema["required"]
and (
schema.get("required", False) == True
and field["configKey"] in schema["required"]
)
and "regex" not in field
):
warnings.warn(
Expand Down Expand Up @@ -1423,11 +1476,9 @@ def get_schema_diff(name, selector, shouldUpdateSchema=False):
selector (string): either 'source' or 'destination'.
shouldUpdateSchema (boolean): if it should update the existing schema with generated one
"""

file_selectors = ["db-config.json", "ui-config.json", "schema.json"]
directory = f"./{CONFIG_DIR}/{selector}s/{name}"
if not os.path.isdir(directory):
print(f"No {selector}s directory found for {name}")
return

if name not in EXCLUDED_DEST:
Expand Down
4 changes: 0 additions & 4 deletions src/configurations/destinations/snap_pixel/ui-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@
"name": "LOGIN",
"value": "LOGIN"
},
{
"name": "ADD_BILLING",
"value": "ADD_BILLING"
},
{
"name": "SHARE",
"value": "SHARE"
Expand Down
Loading