1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
use Swaggest\GoCodeBuilder\JsonSchema\GoBuilder; |
6
|
|
|
use Swaggest\GoCodeBuilder\JsonSchema\StructHookCallback; |
7
|
|
|
use Swaggest\GoCodeBuilder\Templates\GoFile; |
8
|
|
|
use Swaggest\GoCodeBuilder\Templates\Struct\StructDef; |
9
|
|
|
use Swaggest\JsonSchema\Context; |
10
|
|
|
use Swaggest\JsonSchema\RemoteRef\Preloaded; |
11
|
|
|
use Swaggest\JsonSchema\Schema; |
12
|
|
|
use Yaoi\Command; |
13
|
|
|
|
14
|
|
|
class GenGo extends Command |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
public $schema; |
18
|
|
|
/** @var string */ |
19
|
|
|
public $packageName = 'entities'; |
20
|
|
|
/** @var string */ |
21
|
|
|
public $rootName = 'Structure'; |
22
|
|
|
/** @var bool */ |
23
|
|
|
public $showConstProperties = false; |
24
|
|
|
/** @var bool */ |
25
|
|
|
public $keepParentInPropertyNames = false; |
26
|
|
|
/** @var []string */ |
|
|
|
|
27
|
|
|
public $defPtr = ['#/definitions']; |
28
|
|
|
/** @var []string */ |
|
|
|
|
29
|
|
|
public $ptrInSchema; |
30
|
|
|
|
31
|
|
|
public $output; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Command\Definition $definition |
36
|
|
|
* @param \stdClass|static $options |
37
|
|
|
*/ |
38
|
1 |
|
public static function setUpDefinition(Command\Definition $definition, $options) |
39
|
|
|
{ |
40
|
1 |
|
$definition->description = 'Generate Go code from JSON schema'; |
41
|
1 |
|
$options->schema = Command\Option::create() |
|
|
|
|
42
|
1 |
|
->setDescription('Path to JSON schema file')->setIsUnnamed()->setIsRequired(); |
43
|
|
|
|
44
|
1 |
|
$options->output = Command\Option::create() |
45
|
1 |
|
->setDescription('Path to output .go file, STDOUT is used by default')->setType(); |
46
|
|
|
|
47
|
1 |
|
$options->ptrInSchema = Command\Option::create()->setType()->setIsVariadic() |
48
|
1 |
|
->setDescription('JSON pointers to structure in in root schema, default #'); |
49
|
|
|
|
50
|
1 |
|
$options->packageName = Command\Option::create()->setType() |
|
|
|
|
51
|
1 |
|
->setDescription('Go package name, default "entities"'); |
52
|
|
|
|
53
|
1 |
|
$options->rootName = Command\Option::create()->setType() |
|
|
|
|
54
|
1 |
|
->setDescription('Go root struct name, default "Structure", only used for # pointer'); |
55
|
|
|
|
56
|
1 |
|
$options->defPtr = Command\Option::create()->setType()->setIsVariadic() |
57
|
1 |
|
->setDescription('Definitions pointers to strip from symbol names, default #/definitions'); |
58
|
|
|
|
59
|
1 |
|
$options->showConstProperties = Command\Option::create() |
|
|
|
|
60
|
1 |
|
->setDescription('Show properties with constant values, hidden by default'); |
61
|
|
|
|
62
|
1 |
|
$options->keepParentInPropertyNames = Command\Option::create() |
|
|
|
|
63
|
1 |
|
->setDescription('Keep parent prefix in property name, removed by default'); |
64
|
|
|
|
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
3 |
|
public function performAction() |
69
|
|
|
{ |
70
|
3 |
|
$dataValue = Base::readJsonOrYaml($this->schema, $this->response); |
71
|
3 |
|
if (!$dataValue) { |
72
|
|
|
$this->response->error('Unable to find schema in ' . $this->schema); |
73
|
|
|
die(1); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$skipRoot = false; |
77
|
3 |
|
if (empty($this->ptrInSchema)) { |
78
|
1 |
|
$schema = Schema::import($dataValue); |
79
|
|
|
} else { |
80
|
2 |
|
$baseName = basename($this->schema); |
81
|
2 |
|
$skipRoot = true; |
82
|
2 |
|
$preloaded = new Preloaded(); |
83
|
2 |
|
$preloaded->setSchemaData($baseName, $dataValue); |
84
|
2 |
|
$data = new \stdClass(); |
85
|
2 |
|
foreach ($this->ptrInSchema as $i => $ptr) { |
86
|
2 |
|
$data->oneOf[$i] = (object)[Schema::PROP_REF => $baseName . $ptr]; |
87
|
|
|
} |
88
|
2 |
|
$schema = Schema::import($data, new Context($preloaded)); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
$builder = new GoBuilder(); |
92
|
3 |
|
$builder->options->hideConstProperties = !$this->showConstProperties; |
93
|
3 |
|
$builder->options->trimParentFromPropertyNames = !$this->keepParentInPropertyNames; |
94
|
3 |
|
if (!empty($this->defPtr)) { |
95
|
3 |
|
$builder->pathToNameHook->prefixes = []; |
96
|
3 |
|
foreach ($this->defPtr as $defPtr) { |
97
|
3 |
|
if (isset($baseName)) { |
98
|
2 |
|
$builder->pathToNameHook->prefixes[] = $baseName . $defPtr; |
99
|
|
|
} |
100
|
3 |
|
$builder->pathToNameHook->prefixes[] = $defPtr; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
if (isset($baseName)) { |
105
|
2 |
|
$builder->pathToNameHook->prefixes[] = $baseName; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
$builder->structCreatedHook = new StructHookCallback(function (StructDef $structDef, $path, $schema) use ($builder) { |
|
|
|
|
109
|
3 |
|
if ('#' === $path) { |
110
|
2 |
|
$structDef->setName($this->rootName); |
111
|
|
|
} |
112
|
3 |
|
}); |
113
|
3 |
|
$builder->getType($schema); |
114
|
|
|
|
115
|
3 |
|
$goFile = new GoFile($this->packageName); |
116
|
3 |
|
$goFile->fileComment = 'Code generated by github.com/swaggest/json-cli, DO NOT EDIT.'; |
117
|
3 |
|
$goFile->setComment('Package ' . $this->packageName . ' contains JSON mapping structures.'); |
118
|
3 |
|
foreach ($builder->getGeneratedStructs() as $generatedStruct) { |
119
|
3 |
|
if ($skipRoot && $generatedStruct->path === '#') { |
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
3 |
|
$goFile->getCode()->addSnippet($generatedStruct->structDef); |
123
|
|
|
} |
124
|
3 |
|
$goFile->getCode()->addSnippet($builder->getCode()); |
125
|
|
|
|
126
|
3 |
|
if ($this->output) { |
127
|
|
|
file_put_contents($this->output, $goFile->render()); |
128
|
|
|
} else { |
129
|
3 |
|
echo $goFile->render(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |