Skip to content

Commit d3a74d0

Browse files
committed
refactor: thigthen structure type definition
1 parent 84265a2 commit d3a74d0

8 files changed

Lines changed: 45 additions & 68 deletions

File tree

src/adapter/etl-adapter-parquet/src/Flow/ETL/Adapter/Parquet/SchemaConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ private function flowToParquet(string $name, Type $type, bool $nullable): Column
170170
return NestedColumn::struct(
171171
$name,
172172
array_map(
173-
function (string $elementName, Type $elementType) {
173+
function (int|string $elementName, Type $elementType) {
174174
$elementOptional = $elementType instanceof OptionalType;
175175
$elementType = $elementType instanceof OptionalType ? $elementType->base() : $elementType;
176176

177-
return $this->flowToParquet($elementName, $elementType, $elementOptional);
177+
return $this->flowToParquet((string) $elementName, $elementType, $elementOptional);
178178
},
179179
array_keys($type->elements()),
180180
$type->elements(),

src/core/etl/src/Flow/ETL/DSL/functions.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,12 @@ function entries(Entry ...$entries): Entries
980980
}
981981

982982
/**
983-
* @param ?array<string, mixed> $value
984-
* @param Type<mixed> $type
983+
* @template TShape of array<array-key, mixed>
984+
*
985+
* @param ?TShape $value
986+
* @param StructureType<mixed>|Type<TShape> $type
985987
*
986-
* @return ($value is null ? Entry<null> : Entry<array<string, mixed>>)
988+
* @return ($value is null ? Entry<null> : Entry<TShape>)
987989
*/
988990
#[DocumentationDSL(module: Module::CORE, type: DSLType::ENTRY)]
989991
function struct_entry(string $name, ?array $value, Type $type, ?Metadata $metadata = null): Entry
@@ -1005,10 +1007,12 @@ function struct_entry(string $name, ?array $value, Type $type, ?Metadata $metada
10051007
}
10061008

10071009
/**
1008-
* @param ?array<string, mixed> $value
1009-
* @param Type<mixed> $type
1010+
* @template TShape of array<array-key, mixed>
1011+
*
1012+
* @param ?TShape $value
1013+
* @param StructureType<mixed>|Type<TShape> $type
10101014
*
1011-
* @return ($value is null ? Entry<null> : Entry<array<string, mixed>>)
1015+
* @return ($value is null ? Entry<null> : Entry<TShape>)
10121016
*/
10131017
#[DocumentationDSL(module: Module::CORE, type: DSLType::ENTRY)]
10141018
function structure_entry(string $name, ?array $value, Type $type, ?Metadata $metadata = null): Entry

src/core/etl/src/Flow/ETL/Row/Formatter/ASCIISchemaFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private function formatEntry(Definition $definition, array $buffer): array
114114
*
115115
* @return array<int, string>
116116
*/
117-
private function formatStructureElement(string $name, Type $structureType, array $buffer, int $level): array
117+
private function formatStructureElement(int|string $name, Type $structureType, array $buffer, int $level): array
118118
{
119119
$indention = str_repeat(' ', $level);
120120

src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StructureSelectTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function test_selecting_values_from_empty_structure(): void
8484
{
8585
$structure = struct_entry(
8686
'struct',
87+
// @mago-ignore analysis:possibly-invalid-argument
8788
[
8889
'id' => null,
8990
'email' => 'email@email.com',

src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/StructureEntryTest.php

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -176,52 +176,22 @@ public function test_serialization(): void
176176

177177
public function test_structure_element_names_as_numbers(): void
178178
{
179-
static::assertNotEquals(
180-
structure_entry(
181-
'name',
182-
// @mago-ignore analysis:possibly-invalid-argument
183-
['1' => 1, '2' => '2'],
184-
// @mago-ignore analysis:possibly-invalid-argument
185-
type_structure([
186-
'1' => type_integer(),
187-
'2' => type_string(),
188-
]),
189-
),
190-
structure_entry(
191-
'name',
192-
// @mago-ignore analysis:possibly-invalid-argument
193-
['1' => 1, '2' => '2', '3' => '3'],
194-
// @mago-ignore analysis:possibly-invalid-argument
195-
type_structure([
196-
'1' => type_integer(),
197-
'2' => type_string(),
198-
'3' => type_string(),
199-
]),
200-
),
201-
);
202-
static::assertEquals(
203-
structure_entry(
204-
'name',
205-
// @mago-ignore analysis:possibly-invalid-argument
206-
['1' => 1, '2' => 2, '3' => 3],
207-
// @mago-ignore analysis:possibly-invalid-argument
208-
type_structure([
209-
'1' => type_integer(),
210-
'2' => type_integer(),
211-
'3' => type_integer(),
212-
]),
213-
),
214-
structure_entry(
215-
'name',
216-
// @mago-ignore analysis:possibly-invalid-argument
217-
['1' => 1, '2' => 2, '3' => 3],
218-
// @mago-ignore analysis:possibly-invalid-argument
219-
type_structure([
220-
'1' => type_integer(),
221-
'2' => type_integer(),
222-
'3' => type_integer(),
223-
]),
224-
),
225-
);
179+
static::assertNotEquals(structure_entry('name', ['1' => 1, '2' => '2'], type_structure([
180+
'1' => type_integer(),
181+
'2' => type_string(),
182+
])), structure_entry('name', ['1' => 1, '2' => '2', '3' => '3'], type_structure([
183+
'1' => type_integer(),
184+
'2' => type_string(),
185+
'3' => type_string(),
186+
])));
187+
static::assertEquals(structure_entry('name', ['1' => 1, '2' => 2, '3' => 3], type_structure([
188+
'1' => type_integer(),
189+
'2' => type_integer(),
190+
'3' => type_integer(),
191+
])), structure_entry('name', ['1' => 1, '2' => 2, '3' => 3], type_structure([
192+
'1' => type_integer(),
193+
'2' => type_integer(),
194+
'3' => type_integer(),
195+
])));
226196
}
227197
}

src/lib/types/src/Flow/Types/DSL/functions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@
5555
use UnitEnum;
5656

5757
/**
58-
* @param array<string, Type<mixed>> $elements
59-
* @param array<string, Type<mixed>> $optional_elements
58+
* @template T
59+
*
60+
* @param array<array-key, Type<T>> $elements
61+
* @param array<array-key, Type<T>> $optional_elements
6062
*
61-
* @return Type<array<string, mixed>>
63+
* @return StructureType<T>
6264
*/
6365
#[DocumentationDSL(module: Module::TYPES, type: DSLType::TYPE)]
6466
function type_structure(array $elements = [], array $optional_elements = [], bool $allow_extra = false): Type

src/lib/types/src/Flow/Types/Type/Logical/StructureType.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@
3636
/**
3737
* @template T
3838
*
39-
* @implements Type<array<string, T>>
39+
* @implements Type<array<array-key, T>>
4040
*/
4141
final readonly class StructureType implements Type
4242
{
4343
/**
44-
* @var array<string, Type<T>>
44+
* @var array<array-key, Type<T>>
4545
*/
4646
private array $elements;
4747

4848
/**
49-
* @var array<string, Type<T>>
49+
* @var array<array-key, Type<T>>
5050
*/
5151
private array $optionalElements;
5252

5353
/**
54-
* @param array<string, Type<T>> $elements
55-
* @param array<string, Type<T>> $optionalElements
54+
* @param array<array-key, Type<T>> $elements
55+
* @param array<array-key, Type<T>> $optionalElements
5656
*
5757
* @throws InvalidArgumentException
5858
*/
@@ -158,7 +158,7 @@ public function cast(mixed $value): array
158158
}
159159

160160
/**
161-
* @return array<string, Type<mixed>>
161+
* @return array<array-key, Type<mixed>>
162162
*/
163163
public function elements(): array
164164
{
@@ -203,7 +203,7 @@ public function isValid(mixed $value): bool
203203
}
204204

205205
/**
206-
* @return array{type: 'structure', elements: array<string, array<string, mixed>>, optional_elements: array<string, array<string, mixed>>, allow_extra: bool}
206+
* @return array{type: 'structure', elements: array<array-key, array<string, mixed>>, optional_elements: array<array-key, array<string, mixed>>, allow_extra: bool}
207207
*/
208208
public function normalize(): array
209209
{
@@ -237,7 +237,7 @@ public function normalize(): array
237237
}
238238

239239
/**
240-
* @return array<string, Type<mixed>>
240+
* @return array<array-key, Type<mixed>>
241241
*/
242242
public function optionalElements(): array
243243
{

web/landing/resources/dsl.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)