Skip to content

Commit

Permalink
👔 up: update the java class parser and some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 25, 2024
1 parent 00c3c10 commit a815bbb
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 259 deletions.
54 changes: 47 additions & 7 deletions app/Console/SubCmd/JavaCmd/ClassToJsonCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,75 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Lib\Parser\Java\JavaDTOParser;
use Toolkit\Stdlib\Json;
use Toolkit\Stdlib\Str\StrBuilder;

/**
* Class ClassToJsonCmd
*/
class ClassToJsonCmd extends Command
{
protected static string $name = 'to-json';
protected static string $desc = 'parse java DTO class, convert fields to json';
protected static string $desc = 'parse java DTO class, convert fields to json or json5';

public static function aliases(): array
{
return ['json'];
}

protected function configure(): void
{
// $this->flags->addOptByRule($name, $rule);
$this->flags->addOptsByRules([
'class' => 'string;The class code or path to parse;required',
'json5,5' => 'bool;set output json5 format data',
'source,s' => 'string;The class code or filepath to parse. allow use @c for clipboard;required',
'output,o' => 'string;The output target for result;;stdout',
'json5,5' => 'bool;set output json5 format data, will with comment',
'start,pos' => 'int;The start position for parse. 0=header, 1=class, 2=body;;0',
]);
}

/**
* Do execute command
*
* @param Input $input
* @param Output $output
*
* @return int
*/
protected function execute(Input $input, Output $output): int
{
$output->println(__METHOD__);
$fs = $this->flags;

$source = $fs->getOpt('source');
$source = ContentsAutoReader::readFrom($source);

$m = JavaDTOParser::parse($source, [
'startPos' => $fs->getOpt('start'),
]);

// output json
if (!$fs->getOpt('json5')) {
$map = [];
foreach ($m->fields as $field) {
$map[$field->name] = $field->exampleValue();
}

$str = Json::pretty($map);
} else {
// output json5
$sb = StrBuilder::new("{\n");
$sb->append("\n");
foreach ($m->fields as $field) {
$sb->writef(" // %s\n", $field->comment);
$sb->writef(" \"%s\": %s,\n", $field->name, $field->exampleValue(true));
}

$sb->append('}');

$str = $sb->getAndClear();
}

ContentsAutoWriter::writeTo($fs->getOpt('output'), $str);
return 0;
}
}
17 changes: 13 additions & 4 deletions app/Console/SubCmd/JavaCmd/MetadataCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Lib\Parser\Java\JavaDtoParser;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Lib\Parser\Java\JavaDTOParser;
use Toolkit\Stdlib\Json;

/**
* Class MetadataCmd
*/
class MetadataCmd extends Command
{
protected static string $name = 'metadata';
protected static string $desc = 'parse java DTO class, collect field metadata information';
protected static string $desc = 'parse java DTO class, collect metadata information';

public static function aliases(): array
{
Expand Down Expand Up @@ -45,9 +47,16 @@ protected function execute(Input $input, Output $output): int
$source = $fs->getOpt('source');
$source = ContentsAutoReader::readFrom($source);

$p = JavaDtoParser::parse($source);
$m = JavaDTOParser::parse($source);

$distOut = $fs->getOpt('output');
if ($distOut === 'stdout') {
$output->prettyJSON($m->toArray());
} else {
$str = Json::pretty($m->toArray());
ContentsAutoWriter::writeTo($distOut, $str);
}

$output->prettyJSON($p->toArray());
return 0;
}
}
87 changes: 87 additions & 0 deletions app/Lib/Defines/ClassMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Lib\Defines;

use Toolkit\Stdlib\Obj\BaseObject;

/**
* @author inhere
*/
class ClassMeta extends BaseObject
{
/**
* @var string package path for java,go. namespace for php
*/
public string $package = '';

/**
* @var array imports classes for java. use classes for php
*/
public array $imports = [];

/**
* @var string access modifier
*/
public string $accessModifier = AccessModifier::DEFAULT;

/**
* @var string[] other modifiers: final|static|abstract
*/
public array $otherModifiers = [];

/**
* @var string class type
*/
public string $type = ElementType::TYPE_CLASS;

/**
* @var string class name
*/
public string $name = '';

/**
* @var string class description
*/
public string $description = '';

/**
* @var string[] class comment
*/
public array $comments = [];

/**
* @var array annotations on class
*/
public array $annotations = [];

/**
* @var string extends class
*/
public string $extends = '';

/**
* @var string[] interface classes
*/
public array $interfaces = [];

/**
* @var FieldMeta[] fields in class
*/
public array $fields = [];

/**
* @param string $comment
*
* @return void
*/
public function addComment(string $comment): void
{
$s = trim($comment, "/* \t");
if ($s && !$this->description && $s[0] !== '@') {
$this->description = $s;
}

$this->comments[] = $comment;
}

}
58 changes: 21 additions & 37 deletions app/Lib/Defines/FieldMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FieldMeta extends BaseObject
public string $type = UniformType::STRING;

/**
* sub-elem type on type is array
* sub-elem type on type is array, map, object.
*
* @var string
*/
Expand All @@ -57,6 +57,7 @@ class FieldMeta extends BaseObject
*/
public ?FieldMeta $elem = null;

/** @var string uniform type */
private string $_uniformType = '';

/**
Expand Down Expand Up @@ -90,22 +91,15 @@ protected function toUniformType(): string
*/
public function langType(string $lang, string $name = ''): string
{
$name = $name ?: $this->name;
$type = $this->type;
$name = $name ?: $this->name;

if ($lang === ProgramLang::PHP) {
return $this->toPhpType($type, $name);
}

if ($lang === ProgramLang::JAVA) {
return $this->toJavaType($type, $name);
}

if ($lang === ProgramLang::GO) {
return $this->toGoType($type, $name);
}

throw new InvalidArgumentException('unknown language: ' . $lang);
return match ($lang) {
ProgramLang::PHP => $this->toPhpType($type, $name),
ProgramLang::JAVA => $this->toJavaType($type, $name),
ProgramLang::GO => $this->toGoType($type, $name),
default => throw new InvalidArgumentException('unknown language: ' . $lang),
};
}

private function toGoType(string $type, string $name = ''): string
Expand All @@ -131,21 +125,10 @@ public function toJavaType(string $type, string $name = ''): string
}

if ($type === UniformType::ARRAY) {
$elemType = $this->subType ?: $name;
if ($elemType === 'List') {
$elemType .= '_KW';
}

$elemType = $this->subType ?: $name . '_KW';
return sprintf('%s<%s>', JavaType::LIST, Str::upFirst($elemType));
}

if (Str::hasSuffixIC($name, 'ids')) {
return sprintf('%s<%s>', JavaType::LIST, JavaType::LONG);
}

if ($type === UniformType::OBJECT) {
return Str::upFirst($name);
}
return Str::upFirst($type);
}

Expand Down Expand Up @@ -181,7 +164,7 @@ public function exampleValue(bool $quote = false): string|int|float
{
if ($this->example !== '') {
$value = $this->example;
} elseif ($this->isString()) {
} elseif ($this->isUniType(UniformType::STRING)) {
$value = $this->type;
} else {
$value = $this->zeroValue();
Expand Down Expand Up @@ -228,7 +211,6 @@ public function isMultiWords(): bool
if (preg_match('/[A-Z]/', $this->name)) {
return true;
}

return false;
}

Expand Down Expand Up @@ -270,14 +252,6 @@ public function isInt64X(): bool
return in_array($this->uniformType(), [UniformType::INT64, UniformType::UINT64], true);
}

/**
* @return bool
*/
public function isString(): bool
{
return $this->isUniType(UniformType::STRING);
}

/**
* @param string $type
*
Expand Down Expand Up @@ -314,6 +288,16 @@ public function isNeedQuote(): bool
return false;
}

/**
* @param string $type
*
* @return void
*/
public function setType(string $type): void
{
$this->type = $type;
}

/**
* @param string $comment
*
Expand Down
14 changes: 0 additions & 14 deletions app/Lib/Generate/Java/JavaType.php

This file was deleted.

15 changes: 0 additions & 15 deletions app/Lib/Generate/LangName.php

This file was deleted.

Loading

0 comments on commit a815bbb

Please sign in to comment.