Skip to content

Commit

Permalink
deleting extra comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mhli1260 committed Oct 2, 2024
1 parent 3ede84d commit bcb8861
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
24 changes: 4 additions & 20 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,34 @@
)

def generate_class(class_name: str, class_schema: Dict[str, Any]) -> str:
#Hard coded this for now...?
imports = "from typing import Any, Union\n"


# Define a list of primitive types
# primitive_types = ['string', 'number', 'integer', 'boolean']
imports = "from typing import Any, Union\n"

# Check if the schema defines a simple type (like string, number) without properties
if 'type' in class_schema and 'properties' not in class_schema:
return f"class {class_name}:\n def __init__(self):\n pass\n"




# Check for '$ref' and handle it
if '$ref' in class_schema:
ref_class_name = class_schema['$ref'].split('/')[-1]
return f"{imports}\nclass {class_name}:\n pass # This is a reference to {ref_class_name}\n"

if 'anyOf' in class_schema:
return generate_any_of_class(class_name, class_schema['anyOf'])

# Extract properties and required fields
properties = class_schema.get('properties', {})
required = class_schema.get('required', [])

class_def = f"{imports}class {class_name}:\n"
class_def += " def __init__(self"

# Generate __init__ method parameters
for prop, prop_schema in properties.items():
type_hint = get_type_hint(prop_schema)
if prop in required:
# Required parameters should not have default values
class_def += f", {prop}: {type_hint}"
else:
# Optional parameters should have a default value of None
class_def += f", {prop}: {type_hint} = None"

class_def += "):\n"

# Generate attribute assignments in __init__
for prop in properties:
class_def += f" self.{prop} = {prop}\n"

Expand All @@ -76,7 +62,7 @@ def generate_class(class_name: str, class_schema: Dict[str, Any]) -> str:

def generate_any_of_class(class_name: str, any_of_schemas: List[Dict[str, Any]]) -> str:
types = [get_type_hint(schema) for schema in any_of_schemas]
type_union = "Union[" + ", ".join(f'"{t}"' for t in types) + "]" # Add quotes
type_union = "Union[" + ", ".join(f'"{t}"' for t in types) + "]"

class_def = f"class {class_name}:\n"
class_def += f" def __init__(self, value: {type_union}):\n"
Expand All @@ -99,7 +85,7 @@ def get_type_hint(prop_schema: Dict[str, Any]) -> str:
types = [get_type_hint(option) for option in prop_schema['anyOf']]
return f'Union[{", ".join(types)}]'
elif '$ref' in prop_schema:
return prop_schema['$ref'].split('/')[-1] # Get the definition name
return prop_schema['$ref'].split('/')[-1]
return 'Any'

def load_schema(schema_path: Path) -> dict:
Expand All @@ -113,7 +99,6 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:

definitions: Dict[str, str] = {}

# Loop through the definitions and generate classes
for name, schema in rootschema.get("definitions", {}).items():
class_code = generate_class(name, schema)
definitions[name] = class_code
Expand All @@ -123,8 +108,7 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:
with open(output_file, 'w') as f:
f.write(generated_classes)

# Main execution
if __name__ == "__main__":
schema_file = "tools/testingSchema.json" # Update this path as needed
schema_file = "tools/testingSchema.json"
output_file = Path("tools/generated_classes.py")
generate_schema_wrapper(Path(schema_file), output_file)
3 changes: 2 additions & 1 deletion tools/generated_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ class AggregateExpression:
def __init__(self, agg: str, label: str = None):
self.agg = agg
self.label = label

class ParamRef:
def __init__(self):
pass

class TransformField:
def __init__(self, value: Union["str", "ParamRef"]):
self.value = value
Expand Down

0 comments on commit bcb8861

Please sign in to comment.