Skip to content

Commit

Permalink
fix: fix some error on use json generate go code
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 22, 2022
1 parent 8178f30 commit 3661025
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 45 deletions.
27 changes: 22 additions & 5 deletions app/Console/Attach/Golang/GenerateStructCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
use Inhere\Kite\Lib\Parser\Text\TextItemParser;
use Inhere\Kite\Lib\Parser\Text\TextParser;
use InvalidArgumentException;
use PhpPkg\Config\ConfigUtil;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\FlagType;
use Toolkit\PFlag\Validator\EnumValidator;
use Toolkit\Stdlib\Json;
use Toolkit\Stdlib\Str;
use Toolkit\Stdlib\Util\Stream\ListStream;
use function array_filter;
use function array_merge;
use function get_class;
use function str_replace;

/**
Expand All @@ -31,7 +34,7 @@
class GenerateStructCmd extends Command
{
protected static string $name = 'struct';
protected static string $desc = 'quick generate an go struct by json, text and more';
protected static string $desc = 'quick generate an go struct by json, yaml, text and more';

public static function aliases(): array
{
Expand All @@ -51,14 +54,16 @@ protected function configFlags(FlagsParser $fs): void
'--is, --item-sep' => 'The item sep char. default is NL(newline).',
'--vn, --value-num' => 'int;set the item value number(cols number), default get from first item.',
'--vs, --value-sep' => 'The item value sep char for "space" parser. default is SPACE',
'--parser, --item-parser' => [
'-t, --type, --parser' => [
'type' => FlagType::STRING,
'desc' => 'The item parser name for difference data type.
TYPE:
space, text - parser substr by space, use for text data.
json, json5 - parser json(5) line',
json, json5 - parser json(5) line
yaml, yml - convert to json then parse.
',
'default' => 'text',
'validator' => EnumValidator::new(['json', 'json5', 'text', 'space'])
'validator' => EnumValidator::new(['json', 'json5', 'text', 'space', 'yaml', 'yml'])
]
]);

Expand Down Expand Up @@ -94,10 +99,18 @@ protected function execute(Input $input, Output $output): void
$p->setFields(Str::explode($nameString, ','));
}

switch ($fs->getOpt('item-parser')) {
switch ($fs->getOpt('parser')) {
case 'yml':
case 'yaml':
$data = ConfigUtil::parseYamlString($source);
$p->setText(Json::pretty($data));
$itemParser = new Json5ItemParser;
$itemParser->setKeyField('name');
break;
case 'json':
case 'json5':
$itemParser = new Json5ItemParser;
$itemParser->setKeyField('name');
break;
case 'text':
case 'space':
Expand All @@ -111,6 +124,9 @@ protected function execute(Input $input, Output $output): void
$p->parse();
$data = $p->getData(true);
// $output->aList($data, 'parsed field list');
if (!$data) {
throw new InvalidArgumentException('no field data collected from the source.');
}

$lang = GenCodeFactory::LANG_GO;

Expand All @@ -126,6 +142,7 @@ protected function execute(Input $input, Output $output): void
$config = array_merge($config, array_filter([
'tplDir' => $tplDir,
'tplFile' => $tplFile,
'parser' => get_class($itemParser),
]));

$output->aList($config);
Expand Down
4 changes: 2 additions & 2 deletions app/Lib/Parser/Item/FieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class FieldItem extends AbstractObj implements JsonSerializable
/**
* @var string
*/
public string $name;
public string $name = '';

/**
* @var string
*/
public string $type;
public string $type = '';

/**
* sub-elem type on type is array
Expand Down
51 changes: 18 additions & 33 deletions app/Lib/Parser/Text/Json5ItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,17 @@

namespace Inhere\Kite\Lib\Parser\Text;

use Toolkit\Stdlib\Str;
use function in_array;
use function preg_match;
use function preg_replace;
use function strpos;
use function substr;

/**
* class Json5LineParser - parse json5 line, get field and comments
*/
class Json5ItemParser
class Json5ItemParser extends JsonItemParser
{
public const KEY_FIELD = 'field';
public const KEY_COMMENT = 'comment';

/**
* exclude fields
*
* @var array<string>
*/
public array $exclude = [];

/**
* @param array $exclude
*
* @return static
*/
public static function new(array $exclude = []): self
{
$self = new self();

$self->exclude = $exclude;
return $self;
}

/**
* @param string $line
*
Expand All @@ -56,6 +33,13 @@ public function __invoke(string $line): array
{
$pos = strpos($line, '//');
if ($pos < 1) {
// fallback parse json line
if ($matches = self::matchField($line)) {
return [
$this->keyField => $matches[1],
$this->keyComment => Str::toLowerWords($matches[1]),
];
}
return [];
}

Expand All @@ -69,24 +53,25 @@ public function __invoke(string $line): array
}

// match field
if (!preg_match('/^\s*[\'"]?([a-zA-Z][\w_]+)/', $line, $matches)) {
if (!$matches = self::matchField($line)) {
return [];
}

$field = $matches[0];
$item = [
'field' => $field,
];
$field = $matches[1];
if ($this->exclude && in_array($field, $this->exclude, true)) {
return [];
}

$item = [
$this->keyField => $field,
$this->keyComment => Str::toLowerWords($field),
];

// get comments
if (!$comment = trim(substr($line, $pos + 2))) {
return [];
if ($comment = trim(substr($line, $pos + 2))) {
$item[$this->keyComment] = $comment;
}

$item['comment'] = $comment;
return $item;
}
}
62 changes: 57 additions & 5 deletions app/Lib/Parser/Text/JsonItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Inhere\Kite\Lib\Parser\Text;

use Toolkit\Stdlib\Str;
use function preg_match;
use function str_contains;

/**
* class JsonItemParser
Expand All @@ -11,19 +13,69 @@
*/
class JsonItemParser
{
public const KEY_FIELD = 'field';
public string $keyField = 'field';

public string $keyComment = 'comment';

/**
* exclude fields
*
* @var array<string>
*/
public array $exclude = [];

/**
* @param array $exclude
*
* @return static
*/
public static function new(array $exclude = []): self
{
$self = new self();

$self->exclude = $exclude;
return $self;
}

/**
* @param string $line
*
* @return array
*/
public function __invoke(string $line): array
{
if ($matches = self::matchField($line)) {
return [];
}

return [
$this->keyField => $matches[1],
$this->keyComment => Str::toLowerWords($matches[1]),
];
}

/**
* @param string $str
* @param string $line
*
* @return array
*/
public function __invoke(string $str): array
public static function matchField(string $line): array
{
if (!preg_match('/[a-zA-Z][\w_]+/', $str, $matches)) {
// is value of array.
if (!str_contains($line, '": ')) {
return [];
}

return [self::KEY_FIELD => $matches[1]];
$ok = preg_match('/^\s*[\'"]?([a-zA-Z][\w_]+)/', $line, $matches);

return $ok ? $matches : [];
}

/**
* @param string $keyField
*/
public function setKeyField(string $keyField): void
{
$this->keyField = $keyField;
}
}

0 comments on commit 3661025

Please sign in to comment.