|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Jobcloud\SchemaConsole\Command; |
| 4 | + |
| 5 | +use GuzzleHttp\Exception\RequestException; |
| 6 | +use Jobcloud\SchemaConsole\Helper\SchemaFileHelper; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 12 | + |
| 13 | +class CheckAllSchemaTemplatesDefaultTypeCommand extends Command |
| 14 | +{ |
| 15 | + private const TYPE_MAP = [ |
| 16 | + "null" => "null", |
| 17 | + "boolean" => "boolean", |
| 18 | + "integer" => "int", |
| 19 | + "string" => "string", |
| 20 | + "double" => "double", |
| 21 | + "array" => "array", |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + protected function configure(): void |
| 28 | + { |
| 29 | + $this |
| 30 | + ->setName('kafka-schema-registry:check:template:default:type:all') |
| 31 | + ->setDescription('Checks for default value type for all schema templates in folder') |
| 32 | + ->setHelp('Checks for default value type for all schema templates in folder') |
| 33 | + ->addArgument( |
| 34 | + 'schemaTemplateDirectory', |
| 35 | + InputArgument::REQUIRED, |
| 36 | + 'Path to avro schema template directory' |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param InputInterface $input |
| 42 | + * @param OutputInterface $output |
| 43 | + * @return integer |
| 44 | + * @throws RequestException |
| 45 | + */ |
| 46 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 47 | + { |
| 48 | + /** @var string $directory */ |
| 49 | + $directory = $input->getArgument('schemaTemplateDirectory'); |
| 50 | + $avroFiles = SchemaFileHelper::getAvroFiles($directory); |
| 51 | + |
| 52 | + $io = new SymfonyStyle($input, $output); |
| 53 | + |
| 54 | + $failed = []; |
| 55 | + |
| 56 | + if (false === $this->checkSchemas($avroFiles, $failed)) { |
| 57 | + $io->error('Following schema templates have invalid default value types:'); |
| 58 | + $io->listing($failed); |
| 59 | + |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + $io->success('All schema templates have valid default value types'); |
| 64 | + |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array<string, mixed> $avroFiles |
| 70 | + * @param array<string, mixed> $failed |
| 71 | + * @return boolean |
| 72 | + */ |
| 73 | + private function checkSchemas(array $avroFiles, array &$failed = []): bool |
| 74 | + { |
| 75 | + $failed = []; |
| 76 | + |
| 77 | + foreach ($avroFiles as $schemaName => $avroFile) { |
| 78 | + /** @var string $localSchema */ |
| 79 | + $localSchema = file_get_contents($avroFile); |
| 80 | + |
| 81 | + $invalidFields = $this->checkDefaultType($localSchema); |
| 82 | + |
| 83 | + foreach ($invalidFields as $invalidField) { |
| 84 | + $failed[] = $invalidField; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return 0 === count($failed); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $localSchema |
| 93 | + * @return array<string, mixed> |
| 94 | + */ |
| 95 | + private function checkDefaultType(string $localSchema): array |
| 96 | + { |
| 97 | + $decodedSchema = json_decode($localSchema); |
| 98 | + if (!property_exists($decodedSchema, 'fields')) { |
| 99 | + return []; |
| 100 | + } |
| 101 | + |
| 102 | + return $this->checkAllFields($decodedSchema); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param mixed $decodedSchema |
| 107 | + * @param array<mixed, mixed> $defaultFields |
| 108 | + * @return array<string, mixed> |
| 109 | + */ |
| 110 | + private function checkAllFields($decodedSchema, array $defaultFields = []): array |
| 111 | + { |
| 112 | + foreach ($decodedSchema->fields as $field) { |
| 113 | + if (!property_exists($field, 'default')) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + $defaultFields[$field->name] = $this->getFieldName($decodedSchema, $field); |
| 118 | + |
| 119 | + $fieldTypes = $field->type; |
| 120 | + |
| 121 | + if (!is_array($fieldTypes)) { |
| 122 | + $fieldTypes = [$fieldTypes]; |
| 123 | + } |
| 124 | + |
| 125 | + foreach ($fieldTypes as $fieldType) { |
| 126 | + $defaultFields = $this->checkSingleField($fieldType, $field, $defaultFields); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return $defaultFields; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param mixed $fieldType |
| 135 | + * @param mixed $field |
| 136 | + * @param array<mixed, mixed> $defaultFields |
| 137 | + * @return array<string, mixed> |
| 138 | + */ |
| 139 | + private function checkSingleField($fieldType, $field, array $defaultFields): array |
| 140 | + { |
| 141 | + $defaultType = strtolower(gettype($field->default)); |
| 142 | + |
| 143 | + if (is_string($fieldType)) { |
| 144 | + if ( |
| 145 | + self::TYPE_MAP[$defaultType] === $fieldType |
| 146 | + || $this->isContainedInBiggerType(self::TYPE_MAP[$defaultType], $fieldType) |
| 147 | + ) { |
| 148 | + unset($defaultFields[$field->name]); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (property_exists($fieldType, 'type') && $fieldType->type === 'array') { |
| 153 | + if (is_string($defaultType) && self::TYPE_MAP[$defaultType] === $fieldType->type) { |
| 154 | + unset($defaultFields[$field->name]); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return $defaultFields; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @param string $defaultType |
| 163 | + * @param string $currentType |
| 164 | + * @return bool |
| 165 | + */ |
| 166 | + private function isContainedInBiggerType(string $defaultType, string $currentType): bool |
| 167 | + { |
| 168 | + if ($currentType === 'double' && ($defaultType === 'int' || $defaultType === 'float')) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + if ($currentType === 'float' && $defaultType === 'int') { |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param mixed $decodedSchema |
| 181 | + * @param mixed $field |
| 182 | + * @return string |
| 183 | + */ |
| 184 | + private function getFieldName($decodedSchema, $field): string |
| 185 | + { |
| 186 | + return $decodedSchema->namespace . '.' . $decodedSchema->name . '.' . $field->name; |
| 187 | + } |
| 188 | +} |
0 commit comments