1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli\GenGo; |
4
|
|
|
|
5
|
|
|
use Swaggest\GoCodeBuilder\JsonSchema\Options; |
6
|
|
|
use Swaggest\JsonCli\ExitCode; |
7
|
|
|
use Swaggest\PhpCodeBuilder\Markdown\TypeBuilder; |
8
|
|
|
use Yaoi\Command\Option; |
9
|
|
|
|
10
|
|
|
trait BuilderOptions |
11
|
|
|
{ |
12
|
|
|
/** @var bool */ |
13
|
|
|
public $showConstProperties = false; |
14
|
|
|
/** @var bool */ |
15
|
|
|
public $keepParentInPropertyNames = false; |
16
|
|
|
/** @var bool */ |
17
|
|
|
public $ignoreNullable; |
18
|
|
|
/** @var bool */ |
19
|
|
|
public $ignoreXGoType; |
20
|
|
|
/** @var bool */ |
21
|
|
|
public $withZeroValues; |
22
|
|
|
/** @var bool */ |
23
|
|
|
public $enableXNullable; |
24
|
|
|
/** @var bool */ |
25
|
|
|
public $enableDefaultAdditionalProperties; |
26
|
|
|
/** @var bool */ |
27
|
|
|
public $fluentSetters; |
28
|
|
|
/** @var bool */ |
29
|
|
|
public $ignoreRequired = false; |
30
|
|
|
|
31
|
|
|
/** @var array<string, string> */ |
32
|
|
|
public $renames = []; |
33
|
|
|
|
34
|
|
|
/** @var bool */ |
35
|
|
|
public $requireXGenerate = false; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
public $validateRequired = false; |
39
|
|
|
|
40
|
|
|
/** @var string[] */ |
41
|
|
|
public $nameTags = []; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
public $config; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \stdClass|static $options |
48
|
|
|
*/ |
49
|
|
|
public static function setUpBuilderOptions($options) |
50
|
|
|
{ |
51
|
|
|
$options->showConstProperties = Option::create() |
|
|
|
|
52
|
|
|
->setDescription('Show properties with constant values, hidden by default'); |
53
|
|
|
|
54
|
|
|
$options->keepParentInPropertyNames = Option::create() |
|
|
|
|
55
|
|
|
->setDescription('Keep parent prefix in property name, removed by default'); |
56
|
|
|
|
57
|
|
|
$options->ignoreNullable = Option::create() |
|
|
|
|
58
|
|
|
->setDescription('Add `omitempty` to nullable properties, removed by default'); |
59
|
|
|
|
60
|
|
|
$options->ignoreXGoType = Option::create() |
|
|
|
|
61
|
|
|
->setName('ignore-x-go-type') |
62
|
|
|
->setDescription('Ignore `x-go-type` in schema to skip generation'); |
63
|
|
|
|
64
|
|
|
$options->withZeroValues = Option::create() |
|
|
|
|
65
|
|
|
->setDescription('Use pointer types to avoid zero value ambiguity'); |
66
|
|
|
|
67
|
|
|
$options->enableXNullable = Option::create() |
|
|
|
|
68
|
|
|
->setDescription('Add `null` to types if `x-nullable` or `nullable` is available'); |
69
|
|
|
|
70
|
|
|
$options->enableDefaultAdditionalProperties = Option::create() |
|
|
|
|
71
|
|
|
->setDescription('Add field property for undefined `additionalProperties`'); |
72
|
|
|
|
73
|
|
|
$options->fluentSetters = Option::create() |
|
|
|
|
74
|
|
|
->setDescription('Add fluent setters to struct fields'); |
75
|
|
|
|
76
|
|
|
$options->ignoreRequired = Option::create() |
|
|
|
|
77
|
|
|
->setDescription('Ignore if property is required when deciding on pointer type or omitempty'); |
78
|
|
|
|
79
|
|
|
$options->renames = Option::create()->setIsVariadic()->setType() |
|
|
|
|
80
|
|
|
->setDescription('Map of exported symbol renames, example From:To'); |
81
|
|
|
|
82
|
|
|
$options->requireXGenerate = Option::create() |
|
|
|
|
83
|
|
|
->setDescription('Generate properties with `x-generate: true` only'); |
84
|
|
|
|
85
|
|
|
$options->validateRequired = Option::create() |
|
|
|
|
86
|
|
|
->setDescription('Generate validation code to check required properties during unmarshal'); |
87
|
|
|
|
88
|
|
|
$options->nameTags = Option::create()->setIsVariadic()->setType() |
|
|
|
|
89
|
|
|
->setDescription('Set additional field tags with property name, example "msgp bson"'); |
90
|
|
|
|
91
|
|
|
$options->config = Option::create()->setType() |
|
|
|
|
92
|
|
|
->setDescription('Path to config JSON file to load options from. Schema:'); |
93
|
|
|
|
94
|
|
|
$tb = new TypeBuilder(); |
95
|
|
|
$tb->getTypeString(Options::schema()->exportSchema()); |
96
|
|
|
$options->config->description .= "\n" . trim(substr($tb->file, 97)); // Stripping header. |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Options |
101
|
|
|
* @throws ExitCode |
102
|
|
|
*/ |
103
|
|
|
protected function makeGoBuilderOptions() |
104
|
|
|
{ |
105
|
|
|
$options = new Options(); |
106
|
|
|
if (!empty($this->config)) { |
107
|
|
|
$data = file_get_contents($this->config); |
108
|
|
|
if (empty($data)) { |
109
|
|
|
throw new ExitCode("empty or missing config file", 1); |
110
|
|
|
} |
111
|
|
|
$json = json_decode($data); |
112
|
|
|
if (empty($json)) { |
113
|
|
|
throw new ExitCode("invalid json in config file", 1); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$options = Options::import($json); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$options = $options->jsonSerialize(); |
120
|
|
|
|
121
|
|
|
$options->hideConstProperties = !$this->showConstProperties; |
122
|
|
|
$options->trimParentFromPropertyNames = !$this->keepParentInPropertyNames; |
123
|
|
|
$options->ignoreNullable = $this->ignoreNullable; |
124
|
|
|
$options->ignoreXGoType = $this->ignoreXGoType; |
125
|
|
|
$options->withZeroValues = $this->withZeroValues; |
126
|
|
|
$options->enableXNullable = $this->enableXNullable; |
127
|
|
|
$options->defaultAdditionalProperties = $this->enableDefaultAdditionalProperties; |
128
|
|
|
$options->fluentSetters = $this->fluentSetters; |
129
|
|
|
$options->ignoreRequired = $this->ignoreRequired; |
130
|
|
|
$options->requireXGenerate = $this->requireXGenerate; |
131
|
|
|
$options->validateRequired = $this->validateRequired; |
132
|
|
|
$options->nameTags = $this->nameTags; |
133
|
|
|
if (!empty($this->renames)) { |
134
|
|
|
foreach ($this->renames as $rename) { |
135
|
|
|
$rename = explode(':', $rename, 2); |
136
|
|
|
$options->renames[$rename[0]] = $rename[1]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $options; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..