Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
51 changes: 32 additions & 19 deletions glassgen/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,48 @@ def _schema_dict_to_fields(
# Handle choice generator specially
if generator_name == GeneratorType.CHOICE:
# Split by comma but preserve quoted strings
params = [p.strip().strip("\"'") for p in params_str.split(",")]
params = [
p.strip().strip("\"'") for p in params_str.split(",")
]
elif generator_name == GeneratorType.ARRAY:
# Handle array generator: format is "generator_name, count,
# param1, param2, ..."
param_parts = [p.strip() for p in params_str.split(",")]
if len(param_parts) < 2:
# Handle array: "generator_name(params), count" or
# "generator_name, count, param1, param2, ..."
array_match = re.match(
r"(\w+)(?:\((.*?)\))?,\s*(\d+)(?:,\s*(.*))?", params_str
Comment thread
aditypan marked this conversation as resolved.
Resolved
)
if not array_match:
raise ValueError(
f"Array generator requires at least generator name and \
count: {value}"
f"Invalid array format: {params_str}"
)

# First parameter is the generator name (without $)
generator_name_param = param_parts[0].strip("$")
# Second parameter is the count
generator_name_param = array_match.group(1)

try:
count = int(param_parts[1])
count = int(array_match.group(3))
except ValueError as e:
raise ValueError(
f"Array count must be an integer: {param_parts[1]}"
f"Array count must be an integer: "
f"{array_match.group(3)}"
) from e

# Remaining parameters are for the nested generator
# Get nested params from either format
nested_params = []
for p in param_parts[2:]:
# Convert numeric parameters
if p.isdigit():
nested_params.append(int(p))
else:
nested_params.append(p)
nested_params_str = array_match.group(2) or array_match.group(4)
if nested_params_str:
# Format 1: generator_name(params), count
# e.g., "$array(intrange(1, 100), 5)"
# Format 2: generator_name, count, param1, param2, ...
# e.g., "$array(intrange, 5, 1, 100)"
nested_schema = ConfigSchema._schema_dict_to_fields(
{
name: "$"
+ generator_name_param
+ "("
+ nested_params_str
+ ")"
}
)
nested_params = nested_schema[name].params
Comment thread
PabloPardoGarcia marked this conversation as resolved.
Resolved

params = [generator_name_param, count] + nested_params
else:
Expand Down
60 changes: 58 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ def test_array_generator():
# Test with generator that has parameters
schema_with_params = ConfigSchema.from_dict(
{
"numbers": "$array(intrange, 5, 1, 100)",
"choices": "$array(choice, 3, apple, banana, cherry)",
"numbers": "$array(intrange(1, 100), 5)",
"choices": "$array(choice(apple, banana, cherry), 3)",
Comment thread
aditypan marked this conversation as resolved.
Resolved
"prices": "$array(price(1.0, 10.0), 4)",
}
)
record_with_params = schema_with_params._generate_record()
Expand All @@ -215,6 +216,14 @@ def test_array_generator():
for choice in record_with_params["choices"]:
assert choice in ["apple", "banana", "cherry"]

# Check prices array
assert "prices" in record_with_params
assert isinstance(record_with_params["prices"], list)
assert len(record_with_params["prices"]) == 4
for price in record_with_params["prices"]:
assert isinstance(price, float)
assert 1.0 <= price <= 10.0


def test_array_generator_registry():
"""Test that ARRAY generator is properly registered"""
Expand Down Expand Up @@ -244,6 +253,53 @@ def test_array_generator_registry():
assert 10 <= num <= 20


def test_array_generator_both_formats():
"""Test that both array formats work:
1. Nested params format: "$array(intrange(1, 100), 5)"
2. Flat params format: "$array(intrange, 5, 1, 100)"
"""
# Test nested params format
schema_nested = ConfigSchema.from_dict(
{"numbers_nested": "$array(intrange(1, 100), 5)"}
)
record_nested = schema_nested._generate_record()

assert "numbers_nested" in record_nested
assert isinstance(record_nested["numbers_nested"], list)
assert len(record_nested["numbers_nested"]) == 5
for num in record_nested["numbers_nested"]:
assert isinstance(num, int)
assert 1 <= num <= 100

# Test flat params format
schema_flat = ConfigSchema.from_dict(
{"numbers_flat": "$array(choice, 3, apple, banana, cherry)"}
)
record_flat = schema_flat._generate_record()

assert "numbers_flat" in record_flat
assert isinstance(record_flat["numbers_flat"], list)
assert len(record_flat["numbers_flat"]) == 3
for num in record_flat["numbers_flat"]:
assert isinstance(num, str)

# Test both formats in the same schema
schema_both = ConfigSchema.from_dict(
{
"nested_format": "$array(price(1.0, 5.0), 3)",
"flat_format": "$array(price, 3, 1.0, 5.0)",
}
)
record_both = schema_both._generate_record()

assert len(record_both["nested_format"]) == 3
assert len(record_both["flat_format"]) == 3

for num in record_both["nested_format"]:
assert 1.0 <= num <= 5.0
for price in record_both["flat_format"]:
assert 1.0 <= price <= 5.0

def test_predefined_schema():
"""Test using predefined UserSchema"""
schema = UserSchema()
Expand Down
Loading