Skip to content

Commit

Permalink
👔 up: refactoring the class dto generate logic, remove some invalid c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
inhere committed Feb 7, 2024
1 parent 3d8dba5 commit 525a933
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 333 deletions.
5 changes: 3 additions & 2 deletions app/Console/SubCmd/GolangCmd/GenerateStructCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Defines\DataField\JsonField;
use Inhere\Kite\Lib\Defines\ProgramLang;
use Inhere\Kite\Lib\Generate\GenCodeFactory;
use Inhere\Kite\Lib\Generate\DTOGenerator;
use Inhere\Kite\Lib\Parser\Text\Json5ItemParser;
use Inhere\Kite\Lib\Parser\Text\TextItemParser;
use Inhere\Kite\Lib\Parser\Text\TextParser;
Expand Down Expand Up @@ -144,14 +144,15 @@ protected function execute(Input $input, Output $output): void
}

$config = array_merge($config, array_filter([
'lang' => $lang,
'tplDir' => $tplDir,
'tplFile' => $tplFile,
'parser' => get_class($itemParser),
]));

$output->aList($config);

$gen = GenCodeFactory::create($lang)
$gen = DTOGenerator::new()
->setClassName($fs->getOpt('name'))
->addTplVar('genMark', $input->getFullScript(true))
->configThis($config)
Expand Down
61 changes: 9 additions & 52 deletions app/Console/SubCmd/JavaCmd/GenerateDTOCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Lib\Defines\FieldMeta;
use Inhere\Kite\Lib\Generate\Json5Data;
use Inhere\Kite\Lib\Parser\DBTable;
use Inhere\Kite\Lib\Parser\MySQL\TableField;
use Inhere\Kite\Lib\Parser\Text\TextParser;
use Inhere\Kite\Lib\Stream\ListStream;
use InvalidArgumentException;
use Inhere\Kite\Lib\Defines\ProgramLang;
use Inhere\Kite\Lib\Generate\DTOGenerator;

/**
* Class GenerateDTOCmd
Expand Down Expand Up @@ -86,49 +81,11 @@ protected function execute(Input $input, Output $output): void
}
}

$subObjects = [];
switch ($srcType) {
case 'txt':
$p = TextParser::new($source)->parse();
$mp = ListStream::new($p->getData())
->filter(function (array $item) {
return count($item) >= 3;
})
->eachToMap(function (array $item) {
return [
$item[0],
FieldMeta::new([
'name' => $item[0],
'type' => $item[1],
'comment' => $item[2],
])
];
});

break;
case 'sql':
$dbt = DBTable::fromSchemeSQL($source);
$mp = $dbt->getObjFields(TableField::class);

break;
case 'md':
$dbt = DBTable::fromMdTable($source);
$mp = $dbt->getObjFields(TableField::class);

// $mp = MapStream::new($dbt->getFields())
// ->eachToMap($this->dbTableInfoHandler());
break;
case 'json':
case 'json5':
$jd = Json5Data::new()->loadFrom($source);
$mp = $jd->getFields();

$subObjects = $jd->getSubObjects();
$this->config->load($jd->getSettings());
break;
default:
throw new InvalidArgumentException("unsupported source type: $srcType");
}
$lang = ProgramLang::JAVA;
$dg = DTOGenerator::new()
->setLang($lang)
->setSource($source)
->setSourceType($srcType);

$mainName = $this->config->getString('name');
if (!$mainName) {
Expand All @@ -141,7 +98,7 @@ protected function execute(Input $input, Output $output): void
$this->config->set('className', ucfirst($mainName));

$tplFile = $fs->getOpt('tpl-file');
$tplFile = $tplFile ?: "@custom/template/java-code-tpl/req_dto.tpl";
$tplFile = $tplFile ?: "@custom/template/$lang-code-tpl/req_dto.tpl";
$outFile = $fs->getOpt('output');

if ($fs->getOpt('show-info')) {
Expand All @@ -151,7 +108,7 @@ protected function execute(Input $input, Output $output): void
$ctx['outFile'] = $outFile;
$output->mList([
'info' => $ctx,
'fields' => $mp,
'fields' => $dg->getFields(),
]);
return;
}
Expand Down
21 changes: 21 additions & 0 deletions app/Lib/Defines/ClassMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ class ClassMeta extends BaseObject
*/
public array $children = [];

/**
* @param array $fields
*
* @return void
*/
public function setFields(array $fields): void
{
if (!$fields) {
return;
}

$first = reset($fields);
if ($first instanceof FieldMeta) {
$this->fields = $fields;
} else {
foreach ($fields as $field) {
$this->fields[] = FieldMeta::new($field);
}
}
}

/**
* @param string $comment
*
Expand Down
Loading

0 comments on commit 525a933

Please sign in to comment.