Skip to content

Commit 8d14796

Browse files
committed
Ensure there are no conflicts between schemas
1 parent de3802d commit 8d14796

6 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/TL_file_ref_map_schema.tl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
boolFalse#bc799737 = Bool;
22
boolTrue#997275b5 = Bool;
3+
true#3fedd339 = True;
4+
5+
vector#1cb5c415 {t:Type} # [ t ] = Vector t;
36

47
// Root
58
fileReferenceOrigins#ed01a4f1 db_schema:string db_schema_json:string locations:Vector<Location> origins:Vector<Origin> skipped:Vector<SkippedOrigin> actions:Vector<Action> = FileReferenceOrigins;

src/file_ref_map.dat

396 Bytes
Binary file not shown.

src/file_ref_map.json

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

tools/FileRefExtractor/BuildMode/Ast.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class Ast implements BuildMode
4444
private ?string $needsParent = null;
4545

4646
public function __construct(
47+
private readonly array $blacklistedPredicates,
4748
public readonly bool $allowUnpacking,
4849
private array $outputSchema = []
4950
) {
@@ -82,14 +83,14 @@ public function finalize(array $outgoingCons, array $incomingCons, string $refMa
8283
'stored_constructor' => $cons,
8384
];
8485
}
85-
$dbSchema = '';
86+
$dbSchema = "boolFalse#bc799737 = Bool;\nboolTrue#997275b5 = Bool;\ntrue#3fedd339 = True;\nvector#1cb5c415 {t:Type} # [ t ] = Vector t;\n\n";
8687
foreach ($fileIdCons as $cons => $_) {
87-
$dbSchema .= self::stringifySchema($cons, ['id' => 'long'], "FileId")."\n";
88+
$dbSchema .= $this->stringifySchema($cons, ['id' => 'long'], "FileId")."\n";
8889
}
8990
$dbSchema .= "\n";
9091

9192
foreach ($this->outputSchema as $constructor => $params) {
92-
$dbSchema .= self::stringifySchema($constructor, $params, "FileSource")."\n";
93+
$dbSchema .= $this->stringifySchema($constructor, $params, "FileSource")."\n";
9394
}
9495
$dbSchemaJSON = (new TL(null))->toJson($dbSchema);
9596

@@ -115,15 +116,25 @@ public function finalize(array $outgoingCons, array $incomingCons, string $refMa
115116
$s = $s->setOther(['filerefs' => __DIR__ . '/../../../src/TL_file_ref_map_schema.tl']);
116117
$TL = new TL((new ReflectionClass(MTProto::class))->newInstanceWithoutConstructor());
117118
$TL->init($s);
119+
120+
$json = $TL->toJson(__DIR__ . '/../../../src/TL_file_ref_map_schema.tl');
121+
foreach ($json['constructors'] as $constructor) {
122+
Assert::keyNotExists($this->blacklistedPredicates, $constructor['predicate'], "{$constructor['predicate']} is blacklisted and cannot be used in the schema");
123+
}
124+
foreach ($json['methods'] as $method) {
125+
Assert::keyNotExists($this->blacklistedPredicates, $method['method'], "{$method['method']} is blacklisted and cannot be used in the schema");
126+
}
127+
118128
$serialized = $TL->serializeObject(['type' => 'FileReferenceOrigins'], $value, '');
119129
$valueDe = $TL->deserialize($serialized, ['type' => '', 'connection' => null, 'encrypted' => true]);
120130
Assert::true($value == $valueDe);
121131
file_put_contents($refMapFile, $serialized);
122132
file_put_contents($refMapFileJson, json_encode($valueDe, flags: JSON_THROW_ON_ERROR));
123133
}
124134

125-
private static function stringifySchema(string $constructor, array $params, string $cType): string
135+
private function stringifySchema(string $constructor, array $params, string $cType): string
126136
{
137+
Assert::keyNotExists($this->blacklistedPredicates, $constructor, "$constructor is blacklisted and cannot be used in the schema");
127138
$paramsStr = "$constructor ";
128139
foreach ($params as $name => $type) {
129140
Assert::notContains($type, 'InputPeer', "$constructor cannot contain InputPeer");
@@ -192,7 +203,7 @@ public function addNode(TLContext $ctx, ?array $action = null, ?string $why = nu
192203
}
193204
}
194205
foreach ($stored as $name => ['type' => $type]) {
195-
throw new AssertionError("Leftover parameter $constructor.$name:$type for ".self::stringifySchema($constructor, $existing, 'FileSource'));
206+
throw new AssertionError("Leftover parameter $constructor.$name:$type for ".$this->stringifySchema($constructor, $existing, 'FileSource'));
196207
}
197208
} else {
198209
$types = [];

tools/FileRefExtractor/FileRefGenerator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public static function generate(int|string $layer, string $inputSchema, string $
5252
$TL = new TL(null);
5353
$TL->init($schema);
5454

55+
$blacklistedPredicates = array_fill_keys(array_column($TL->getConstructors()->by_id, 'predicate'), true)
56+
+ array_fill_keys(array_column($TL->getMethods()->by_id, 'method'), true);
57+
unset($blacklistedPredicates['boolTrue'], $blacklistedPredicates['boolFalse'], $blacklistedPredicates['true'], $blacklistedPredicates['vector']);
58+
5559
$TL = new TLWrapper($TL);
5660
$locations = [];
5761
Logger::log("Generating file reference map for layer $layer...");
@@ -494,7 +498,7 @@ public static function generate(int|string $layer, string $inputSchema, string $
494498
}
495499
}
496500

497-
$tmp = new Ast(allowUnpacking: true, outputSchema: $pre);
501+
$tmp = new Ast(blacklistedPredicates: $blacklistedPredicates, allowUnpacking: true, outputSchema: $pre);
498502
foreach ($incomingCons as $constructor => $_) {
499503
$type = ucfirst($constructor);
500504
$stack = [[$constructor, 'file_reference']];
@@ -583,7 +587,7 @@ static function (array $stack) use ($locations, $TL, $tmp, &$validated, $storyMe
583587
throw new AssertionError("Leftover ops!");
584588
}
585589

586-
$output = new Ast(allowUnpacking: false, outputSchema: $pre);
590+
$output = new Ast(blacklistedPredicates: $blacklistedPredicates, allowUnpacking: false, outputSchema: $pre);
587591
foreach ($locations as $constructor => $ops) {
588592
foreach ($ops as $idx => $op) {
589593
$op->build(new TLContext($TL, $output, $constructor, $TL->isConstructor($constructor)));

0 commit comments

Comments
 (0)