Skip to content

Commit

Permalink
up: update some text parse and generate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 10, 2021
1 parent f7bb807 commit 7634022
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 160 deletions.
4 changes: 2 additions & 2 deletions app/Console/Controller/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:');
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 9 additions & 24 deletions app/Lib/Generate/AbstractJsonToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
112 changes: 2 additions & 110 deletions app/Lib/Generate/Json/JsonField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions app/Lib/Generate/Json5Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
})
Expand Down
17 changes: 14 additions & 3 deletions app/Lib/Parser/DBMdTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -88,7 +100,6 @@ public function parseLine(string $line): array
return trim($value, '`\': ');
}, explode('|', $line));


$typeLen = 0;
$typeExt = '';
$typeNode = $nodes[1];
Expand Down Expand Up @@ -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,
];
Expand Down
17 changes: 16 additions & 1 deletion app/Lib/Parser/DBSchemeSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
];
Expand Down
Loading

0 comments on commit 7634022

Please sign in to comment.