Skip to content

Commit e381870

Browse files
committed
Implement flagged path checks
1 parent 9331cc1 commit e381870

22 files changed

Lines changed: 281 additions & 229 deletions

examples/pipesbot

tools/FileRefExtractor/ActionOp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ interface ActionOp
2222
{
2323
public function build(TLContext $tl): void;
2424

25-
public function normalize(array $stack, string $current): ?self;
25+
public function normalize(array $stack, string $current, bool $ignoreFlagged): ?self;
2626
}

tools/FileRefExtractor/FieldExtractorOp.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818

1919
namespace danog\MadelineProto\FileRefExtractor;
2020

21-
interface FieldExtractorOp extends TypedOp
21+
abstract readonly class FieldExtractorOp implements TypedOp
2222
{
23+
public function __construct(
24+
/** @var list<list{0: string, 1: string, 2?: TypedOp|null}> */
25+
public array $path,
26+
) {
27+
foreach ($path as $elem) {
28+
if (\count($elem) !== 2 && \count($elem) !== 3) {
29+
throw new \InvalidArgumentException('Invalid path part: ' . json_encode($path));
30+
}
31+
if (isset($elem[2]) && !$elem[2] instanceof TypedOp && $elem[2] !== true) {
32+
throw new \InvalidArgumentException('Invalid path part: ' . json_encode($path));
33+
}
34+
}
35+
}
36+
37+
public function getType(TLContext $tl): string
38+
{
39+
return $tl->getTypeAtPosition($this);
40+
}
41+
2342
}

tools/FileRefExtractor/Ops/ArrayOp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public function __construct(TypedOp ...$values)
2929
{
3030
$this->values = $values;
3131
}
32-
public function normalize(array $stack, string $current): ?\danog\MadelineProto\FileRefExtractor\TypedOp
32+
public function normalize(array $stack, string $current, bool $ignoreFlag): ?\danog\MadelineProto\FileRefExtractor\TypedOp
3333
{
3434
$final = [];
3535
$isDifferent = false;
3636
foreach ($this->values as $value) {
37-
$normalized = $value->normalize($stack, $current);
37+
$normalized = $value->normalize($stack, $current, $ignoreFlag);
3838
if ($normalized === null) {
3939
return null;
4040
}

tools/FileRefExtractor/Ops/CallOp.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public function __construct(
3434
) {
3535
Assert::allIsInstanceOf($args, TypedOp::class);
3636
}
37-
public function normalize(array $stack, string $current): ?ActionOp
37+
public function normalize(array $stack, string $current, bool $ignoreFlag): ?ActionOp
3838
{
3939
$final = [];
4040
$isDifferent = false;
4141
foreach ($this->args as $from => $to) {
42-
$normalized = $to->normalize($stack, $current);
42+
$normalized = $to->normalize($stack, $current, $ignoreFlag);
4343
if ($normalized === null) {
4444
return null;
4545
}
@@ -59,7 +59,7 @@ public static function simple(string $method, string $constructor, array $args):
5959
$final = [];
6060
foreach ($args as $from => $to) {
6161
if (!$to instanceof TypedOp) {
62-
$to = new ExtractFromHereOp([$constructor, $to]);
62+
$to = new ExtractFromHereOp([[$constructor, $to]]);
6363
}
6464
$final[$from] = $to;
6565
}
@@ -117,12 +117,12 @@ public function build(TLContext $tl): void
117117
];
118118
}
119119
} else {
120-
assert($out instanceof Ast);
120+
\assert($out instanceof Ast);
121121
$out->output[$tl->position][] = [
122122
'op' => 'call',
123123
'method' => $this->method,
124124
'args' => $final,
125-
'needsMethod' => $out->needsMethod
125+
'needsMethod' => $out->needsMethod,
126126
];
127127
}
128128
}

tools/FileRefExtractor/Ops/ConstructorOp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public function __construct(
3232
Assert::allIsInstanceOf($args, TypedOp::class);
3333
}
3434

35-
public function normalize(array $stack, string $current): ?\danog\MadelineProto\FileRefExtractor\TypedOp
35+
public function normalize(array $stack, string $current, bool $ignoreFlag): ?\danog\MadelineProto\FileRefExtractor\TypedOp
3636
{
3737
$final = [];
3838
$isDifferent = false;
3939
foreach ($this->args as $from => $to) {
40-
$normalized = $to->normalize($stack, $current);
40+
$normalized = $to->normalize($stack, $current, $ignoreFlag);
4141
if ($normalized === null) {
4242
return null;
4343
}

tools/FileRefExtractor/Ops/CopyMethodCallOp.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
namespace danog\MadelineProto\FileRefExtractor\Ops;
2020

2121
use danog\MadelineProto\FileRefExtractor\ActionOp;
22-
use danog\MadelineProto\FileRefExtractor\BuildMode;
2322
use danog\MadelineProto\FileRefExtractor\BuildMode\Ast;
2423
use danog\MadelineProto\FileRefExtractor\TLContext;
2524
use Webmozart\Assert\Assert;
@@ -30,7 +29,7 @@ public function __construct(private readonly string $method)
3029
{
3130
}
3231

33-
public function normalize(array $stack, string $current): ?\danog\MadelineProto\FileRefExtractor\ActionOp
32+
public function normalize(array $stack, string $current, bool $ignoreFlag): ?\danog\MadelineProto\FileRefExtractor\ActionOp
3433
{
3534
Assert::eq($current, $this->method);
3635
Assert::isEmpty($stack);
@@ -43,12 +42,12 @@ public function build(TLContext $tl): void
4342
$tl->tl->tl->getMethods()->findByMethod($this->method)['type']; // Validate type
4443
$out = $tl->buildMode;
4544
if ($out instanceof Ast) {
46-
$out->output[$this->method][] = [
45+
/*$out->output[$this->method][] = [
4746
'op' => 'copyMethodCall',
4847
'ctx' => $out->contextName,
4948
'method' => $this->method,
50-
];
51-
} else {
49+
];*/
5250
}
51+
5352
}
5453
}

tools/FileRefExtractor/Ops/ExtractFromHereOp.php

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,48 @@
2020

2121
use danog\MadelineProto\FileRefExtractor\FieldExtractorOp;
2222
use danog\MadelineProto\FileRefExtractor\TLContext;
23+
use danog\MadelineProto\FileRefExtractor\TypedOp;
2324
use Webmozart\Assert\Assert;
2425

25-
final readonly class ExtractFromHereOp implements FieldExtractorOp
26+
final readonly class ExtractFromHereOp extends FieldExtractorOp
2627
{
27-
public function __construct(
28-
/** @var string[] */
29-
public readonly array $path,
30-
public readonly bool $isFlag = false,
31-
public readonly ?FieldExtractorOp $ifEmptyFlag = null,
32-
) {
33-
if ($ifEmptyFlag !== null) {
34-
Assert::true($isFlag);
35-
}
36-
}
37-
38-
public function normalize(array $stack, string $current): ?\danog\MadelineProto\FileRefExtractor\TypedOp
28+
public function normalize(array $stack, string $current, bool $ignoreFlag): ?\danog\MadelineProto\FileRefExtractor\TypedOp
3929
{
40-
$if = $this->ifEmptyFlag?->normalize($stack, $current);
41-
if ($if === null && $this->ifEmptyFlag !== null) {
42-
return null;
30+
$new = [];
31+
foreach ($this->path as $i => $part) {
32+
if ($ignoreFlag && \array_key_exists(2, $part) && $part[2] === null) {
33+
return null;
34+
}
35+
if (isset($part[2]) && $part[2] instanceof TypedOp) {
36+
$n = $part[2]->normalize($stack, $current, $ignoreFlag);
37+
if ($n === null) {
38+
return null;
39+
}
40+
$part[2] = $n;
41+
}
42+
$new[$i] = $part;
4343
}
44-
Assert::eq($current, $this->path[0]);
44+
Assert::eq($current, $this->path[0][0]);
4545
return new self(
46-
[...$stack, ...$this->path],
47-
$this->isFlag,
48-
$if
46+
[...$stack, ...$new],
4947
);
5048
}
5149

52-
public function getType(TLContext $tl): string
53-
{
54-
$t = $tl->getTypeAtPosition($this);
55-
if ($this->ifEmptyFlag !== null) {
56-
Assert::eq($this->ifEmptyFlag->getType($tl), $t);
57-
}
58-
return $t;
59-
}
60-
61-
public function extend(string ...$path): self
62-
{
63-
return new self(...$this->path, ...$path);
64-
}
65-
6650
public function build(TLContext $tl): array
6751
{
6852
// Validate
6953
$this->getType($tl);
7054

55+
$new = [];
56+
foreach ($this->path as $part) {
57+
if (isset($part[2]) && $part[2] !== true) {
58+
$part[2] = $part[2]->build($tl);
59+
}
60+
$new[] = $part;
61+
}
7162
return [
7263
'op' => 'extractFromHere',
73-
'isFlag' => $this->isFlag,
74-
'ifFlagEmptyUse' => $this->ifEmptyFlag?->build($tl),
75-
'path' => $this->path,
64+
'path' => $new,
7665
];
7766
}
7867
}

0 commit comments

Comments
 (0)