From 7634022b0e097536e32845072cb404dca34a30c1 Mon Sep 17 00:00:00 2001 From: Inhere Date: Wed, 10 Nov 2021 14:55:26 +0800 Subject: [PATCH] up: update some text parse and generate logic --- app/Console/Controller/GenerateController.php | 4 +- app/Console/Controller/JsonController.php | 4 +- app/Lib/Generate/AbstractJsonToCode.php | 33 ++--- app/Lib/Generate/Json/JsonField.php | 112 +--------------- app/Lib/Generate/Json5Data.php | 4 +- app/Lib/Parser/DBMdTable.php | 17 ++- app/Lib/Parser/DBSchemeSQL.php | 17 ++- app/Lib/Parser/DBTable.php | 39 ++++-- app/Lib/Parser/Item/FieldItem.php | 125 ++++++++++++++++++ app/Lib/Parser/MySQL/TableField.php | 53 ++++++++ ...son5LineParser.php => Json5ItemParser.php} | 2 +- app/Lib/Parser/Text/TextParser.php | 13 +- 12 files changed, 263 insertions(+), 160 deletions(-) create mode 100644 app/Lib/Parser/Item/FieldItem.php create mode 100644 app/Lib/Parser/MySQL/TableField.php rename app/Lib/Parser/Text/{Json5LineParser.php => Json5ItemParser.php} (98%) diff --git a/app/Console/Controller/GenerateController.php b/app/Console/Controller/GenerateController.php index 0ab0a5e..0ccd7d4 100644 --- a/app/Console/Controller/GenerateController.php +++ b/app/Console/Controller/GenerateController.php @@ -9,7 +9,7 @@ use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Kite; use Inhere\Kite\Lib\Parser\IniParser; -use Inhere\Kite\Lib\Template\TextTemplate; +use Inhere\Kite\Lib\Template\PhpTemplate; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Str; use Toolkit\Sys\Proc\ProcWrapper; @@ -99,7 +99,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void $vars = IniParser::decode(trim($varDefine)); $output->aList($vars, 'template vars', ['ucFirst' => false]); - $logic = new TextTemplate(); + $logic = new PhpTemplate(); $result = $logic->renderString(trim($template), $vars); $output->success('Render Result:'); diff --git a/app/Console/Controller/JsonController.php b/app/Console/Controller/JsonController.php index b2bccfd..81dd945 100644 --- a/app/Console/Controller/JsonController.php +++ b/app/Console/Controller/JsonController.php @@ -17,7 +17,7 @@ use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Kite; use Inhere\Kite\Lib\Generate\JsonToCode; -use Inhere\Kite\Lib\Parser\Text\Json5LineParser; +use Inhere\Kite\Lib\Parser\Text\Json5ItemParser; use Inhere\Kite\Lib\Parser\Text\TextParser; use InvalidArgumentException; use Throwable; @@ -237,7 +237,7 @@ public function fieldsCommand(FlagsParser $fs, Output $output): void throw new InvalidArgumentException('please input json(5) text for handle'); } - $parser = TextParser::newWithParser($json, new Json5LineParser()); + $parser = TextParser::newWithParser($json, new Json5ItemParser()); $fields = $parser->getStringMap('field', 'comment'); $output->aList($fields); diff --git a/app/Lib/Generate/AbstractJsonToCode.php b/app/Lib/Generate/AbstractJsonToCode.php index 5902e32..7d6bfe1 100644 --- a/app/Lib/Generate/AbstractJsonToCode.php +++ b/app/Lib/Generate/AbstractJsonToCode.php @@ -2,32 +2,16 @@ namespace Inhere\Kite\Lib\Generate; -use ColinODell\Json5\Json5Decoder; use Inhere\Kite\Helper\KiteUtil; -use Inhere\Kite\Lib\Generate\Java\JavaType; -use Inhere\Kite\Lib\Generate\Json\JsonField; -use Inhere\Kite\Lib\Parser\Text\Json5LineParser; -use Inhere\Kite\Lib\Parser\Text\TextParser; use InvalidArgumentException; -use Throwable; use Toolkit\FsUtil\File; use Toolkit\Stdlib\Obj; use Toolkit\Stdlib\OS; -use Toolkit\Stdlib\Str; -use Toolkit\Stdlib\Type; use function array_merge; use function date; use function dirname; -use function gettype; use function is_file; -use function preg_match; -use function str_contains; -use function str_ends_with; -use function str_replace; -use function strpos; -use function substr; use function trim; -use function vdump; /** * class AbstractJsonToCode @@ -121,17 +105,18 @@ protected function renderTplText(): string $tplEng = KiteUtil::newTplEngine(); $settings = array_merge([ - 'lang' => 'java', - 'user' => OS::getUserName(), - 'date' => date('Y-m-d'), + 'lang' => 'java', + 'user' => OS::getUserName(), + 'date' => date('Y-m-d'), ], $this->contexts); - $tplVars = [ - 'ctx' => $settings, - 'fields' => $this->fields, - ]; + $settings['fields'] = $this->fields; + // $tplVars = [ + // 'ctx' => $settings, + // 'fields' => $this->fields, + // ]; - return $tplEng->renderString($tplText, $tplVars); + return $tplEng->renderString($tplText, $settings); } /** diff --git a/app/Lib/Generate/Json/JsonField.php b/app/Lib/Generate/Json/JsonField.php index 28fc885..62d02ef 100644 --- a/app/Lib/Generate/Json/JsonField.php +++ b/app/Lib/Generate/Json/JsonField.php @@ -2,120 +2,12 @@ namespace Inhere\Kite\Lib\Generate\Json; -use Inhere\Kite\Lib\Generate\Java\JavaType; -use JsonSerializable; -use Toolkit\Stdlib\Json; -use Toolkit\Stdlib\Obj\AbstractObj; -use Toolkit\Stdlib\Str; -use Toolkit\Stdlib\Type; -use function preg_match; +use Inhere\Kite\Lib\Parser\Item\FieldItem; /** * class JsonField */ -class JsonField extends AbstractObj implements JsonSerializable +class JsonField extends FieldItem { - public string $name; - public string $type; - public string $desc; - /** - * @param string $lang - * - * @return string - */ - public function getType(string $lang = 'php'): string - { - if ($lang === 'php') { - return $this->type; - } - - return $this->toJavaType(); - } - - /** - * @return string - */ - public function toJavaType(): string - { - if ($this->type === Type::ARRAY) { - return JavaType::OBJECT; - } - - if (str_ends_with($this->name, 'id') || str_ends_with($this->name, 'Id')) { - return JavaType::LONG; - } - - return Str::upFirst($this->type); - } - - /** - * @return bool - */ - public function isMultiWords(): bool - { - if (str_contains($this->name, '_')) { - return true; - } - - if (preg_match('/[A-Z]/', $this->name)) { - return true; - } - - return false; - } - - /** - * @return array - */ - public function toArray(): array - { - return [ - 'name' => $this->name, - 'type' => $this->type, - 'desc' => $this->desc, - ]; - } - - /** - * @return bool - */ - public function isInt(): bool - { - return $this->isType(Type::INTEGER); - } - - /** - * @return bool - */ - public function isStr(): bool - { - return $this->isType(Type::STRING); - } - - /** - * @param string $type - * - * @return bool - */ - public function isType(string $type): bool - { - return $type === $this->type; - } - - /** - * @return string - */ - public function __toString(): string - { - return Json::encodeCN($this->toArray()); - } - - /** - * @return array - */ - public function jsonSerialize(): array - { - return $this->toArray(); - } } diff --git a/app/Lib/Generate/Json5Data.php b/app/Lib/Generate/Json5Data.php index 2a5b13c..aed35fd 100644 --- a/app/Lib/Generate/Json5Data.php +++ b/app/Lib/Generate/Json5Data.php @@ -4,7 +4,7 @@ use ColinODell\Json5\Json5Decoder; use Inhere\Kite\Lib\Generate\Json\JsonField; -use Inhere\Kite\Lib\Parser\Text\Json5LineParser; +use Inhere\Kite\Lib\Parser\Text\Json5ItemParser; use Inhere\Kite\Lib\Parser\Text\TextParser; use Toolkit\Stdlib\Obj\AbstractObj; use function gettype; @@ -43,7 +43,7 @@ public function loadFrom(string $json): self // has comments chars if (str_contains($json, '//')) { - $p = TextParser::newWithParser($json, new Json5LineParser()) + $p = TextParser::newWithParser($json, new Json5ItemParser()) ->withConfig(function (TextParser $p) { $p->headerSep = "\n//###\n"; }) diff --git a/app/Lib/Parser/DBMdTable.php b/app/Lib/Parser/DBMdTable.php index b9a61c2..c2eed3a 100644 --- a/app/Lib/Parser/DBMdTable.php +++ b/app/Lib/Parser/DBMdTable.php @@ -19,10 +19,22 @@ use function ucfirst; /** - * class MkDownTable + * class DBMdTable + * + * @author inhere */ class DBMdTable { + /** + * @param string $mdTable + * + * @return DBTable + */ + public static function parseMD(string $mdTable): DBTable + { + return (new self())->parse($mdTable); + } + /** * @param string $mdTable * @@ -88,7 +100,6 @@ public function parseLine(string $line): array return trim($value, '`\': '); }, explode('|', $line)); - $typeLen = 0; $typeExt = ''; $typeNode = $nodes[1]; @@ -148,7 +159,7 @@ public function parseLine(string $line): array 'type' => strtolower($upType), 'typeLen' => $typeLen, 'typeExt' => $typeExt, - 'nullable' => $allowNull === '', + 'allowNull' => $allowNull === '', 'default' => $defValue, 'comment' => $fieldComment, ]; diff --git a/app/Lib/Parser/DBSchemeSQL.php b/app/Lib/Parser/DBSchemeSQL.php index ca8cff4..4548976 100644 --- a/app/Lib/Parser/DBSchemeSQL.php +++ b/app/Lib/Parser/DBSchemeSQL.php @@ -19,6 +19,16 @@ */ class DBSchemeSQL { + /** + * @param string $createSQL + * + * @return DBTable + */ + public static function parseSQL(string $createSQL): DBTable + { + return (new self())->parse($createSQL); + } + /** * @param string $createSQL * @@ -63,6 +73,11 @@ public function parse(string $createSQL): DBTable return $dbt; } + /** + * @param string $row + * + * @return array + */ public function parseLine(string $row): array { [$field, $other] = explode(' ', $row, 2); @@ -105,7 +120,7 @@ public function parseLine(string $row): array 'type' => strtolower($type), 'typeLen' => $typeLen, 'typeExt' => $typeExt, - 'nullable' => $allowNull, + 'allowNull' => $allowNull, 'default' => $default, 'comment' => $comment, ]; diff --git a/app/Lib/Parser/DBTable.php b/app/Lib/Parser/DBTable.php index 7af74c9..eb7a26c 100644 --- a/app/Lib/Parser/DBTable.php +++ b/app/Lib/Parser/DBTable.php @@ -8,7 +8,6 @@ use function array_merge; use function implode; use function sprintf; -use function str_contains; use function strtoupper; /** @@ -17,13 +16,13 @@ class DBTable { public const FIELD_META = [ - 'name' => '', - 'type' => '', // int - 'typeLen' => '', // eg: 10 - 'typeExt' => '', // eg: UNSIGNED - 'nullable' => true, - 'default' => '', - 'comment' => '', + 'name' => '', + 'type' => '', // int + 'typeLen' => 0, // eg: 10 + 'typeExt' => '', // eg: UNSIGNED + 'allowNull' => true, + 'default' => '', + 'comment' => '', ]; protected string $source = ''; @@ -31,14 +30,14 @@ class DBTable /** * @var string */ - protected string $tableName = ''; + public string $tableName = ''; /** * table comments desc * * @var string */ - protected string $tableComment = ''; + public string $tableComment = ''; /** * @see FIELD_META @@ -194,7 +193,7 @@ public function toCreateSQL(): string } $nodes[] = $typeNode; - if (!$meta['nullable']) { + if (!$meta['allowNull']) { $nodes[] = 'NOT NULL'; } @@ -207,7 +206,7 @@ public function toCreateSQL(): string } // if ($this->isNoDefault($type)) { // } else { - } elseif (!$meta['nullable'] && TypeMap::isStringType($type)) { + } elseif (!$meta['allowNull'] && TypeMap::isStringType($type)) { $nodes[] = "DEFAULT ''"; } @@ -248,7 +247,7 @@ public function toMDTable(): string $upType .= ' ' . $meta['typeExt']; } - $allowNull = $meta['nullable'] ? 'Yes' : 'No'; + $allowNull = $meta['allowNull'] ? 'Yes' : 'No'; $default = $meta['default']; $mdNodes[] = sprintf( @@ -295,6 +294,20 @@ public function getFields(): array return $this->fields; } + /** + * @param class-string $fieldClass + * + * @return array[] + */ + public function getObjFields(string $fieldClass): array + { + $map = []; + foreach ($this->fields as $field => $info) { + $map[$field] = new $fieldClass($fieldClass); + } + return $map; + } + /** * @return array */ diff --git a/app/Lib/Parser/Item/FieldItem.php b/app/Lib/Parser/Item/FieldItem.php new file mode 100644 index 0000000..e3d04fa --- /dev/null +++ b/app/Lib/Parser/Item/FieldItem.php @@ -0,0 +1,125 @@ +type; + } + + return $this->toJavaType(); + } + + /** + * @return string + */ + public function toJavaType(): string + { + if ($this->type === Type::ARRAY) { + return JavaType::OBJECT; + } + + if (str_ends_with($this->name, 'id') || str_ends_with($this->name, 'Id')) { + return JavaType::LONG; + } + + return Str::upFirst($this->type); + } + + /** + * @return bool + */ + public function isMultiWords(): bool + { + if (str_contains($this->name, '_')) { + return true; + } + + if (preg_match('/[A-Z]/', $this->name)) { + return true; + } + + return false; + } + + /** + * @return array + */ + public function toArray(): array + { + return [ + 'name' => $this->name, + 'type' => $this->type, + 'comment' => $this->comment, + ]; + } + + /** + * @return bool + */ + public function isInt(): bool + { + return $this->isType(Type::INTEGER); + } + + /** + * @return bool + */ + public function isStr(): bool + { + return $this->isType(Type::STRING); + } + + /** + * @param string $type + * + * @return bool + */ + public function isType(string $type): bool + { + return $type === $this->type; + } + + /** + * @return string + */ + public function __toString(): string + { + return Json::encodeCN($this->toArray()); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } +} diff --git a/app/Lib/Parser/MySQL/TableField.php b/app/Lib/Parser/MySQL/TableField.php new file mode 100644 index 0000000..82388a4 --- /dev/null +++ b/app/Lib/Parser/MySQL/TableField.php @@ -0,0 +1,53 @@ +typeLen; + $map['typeExt'] = $this->typeExt; + $map['allowNull'] = $this->allowNull; + $map['default'] = $this->default; + return $map; + } +} diff --git a/app/Lib/Parser/Text/Json5LineParser.php b/app/Lib/Parser/Text/Json5ItemParser.php similarity index 98% rename from app/Lib/Parser/Text/Json5LineParser.php rename to app/Lib/Parser/Text/Json5ItemParser.php index e86f9ad..a9b1a87 100644 --- a/app/Lib/Parser/Text/Json5LineParser.php +++ b/app/Lib/Parser/Text/Json5ItemParser.php @@ -11,7 +11,7 @@ /** * class Json5LineParser - parse json5 line, get field and comments */ -class Json5LineParser +class Json5ItemParser { public const KEY_FIELD = 'field'; public const KEY_COMMENT = 'comment'; diff --git a/app/Lib/Parser/Text/TextParser.php b/app/Lib/Parser/Text/TextParser.php index 7715b54..96e5f53 100644 --- a/app/Lib/Parser/Text/TextParser.php +++ b/app/Lib/Parser/Text/TextParser.php @@ -3,8 +3,8 @@ namespace Inhere\Kite\Lib\Parser\Text; use Closure; -use Inhere\Kite\Lib\Parser\IniParser; use InvalidArgumentException; +use PhpPkg\Ini\Ini; use Toolkit\Stdlib\Str; use function array_combine; use function array_merge; @@ -12,6 +12,7 @@ use function count; use function explode; use function implode; +use function is_array; use function str_contains; use function str_replace; use function trim; @@ -313,7 +314,7 @@ protected function parseHeaderSettings(string $header): void $header = $beforeFn($header); } - $this->settings = IniParser::decode($header); + $this->settings = Ini::decode($header); foreach ($allowConfig as $prop) { if (!isset($this->settings[$prop])) { continue; @@ -422,6 +423,10 @@ public function getData(bool $indexToField = false): array if ($indexToField && $this->fields) { $new = []; foreach ($this->data as $item) { + if (!is_array($item)) { + throw new InvalidArgumentException('data item is not array, cannot replace index to field'); + } + // on item elem number < $this->fieldNum, pad empty string value. if (count($item) < $this->fieldNum) { $item = array_pad($item, $this->fieldNum, ''); @@ -448,6 +453,10 @@ public function getDataMap(int|string $keyIdxOrName = 0, bool $indexToField = fa $indexToField = $indexToField && $this->fields; foreach ($this->data as $item) { + if (!is_array($item)) { + throw new InvalidArgumentException('data item is not array, cannot replace index to field'); + } + if (!isset($item[$keyIdxOrName])) { throw new InvalidArgumentException("the data item index/key '$keyIdxOrName' not exists"); }