Skip to content

Commit 6c65dec

Browse files
authored
Allow regex rewrites for symbol names (#45)
1 parent 318e272 commit 6c65dec

File tree

10 files changed

+7366
-53
lines changed

10 files changed

+7366
-53
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.51] - 2022-09-15
8+
9+
### Added
10+
11+
- Symbol rewrites using regular expressions
12+
13+
### Fixed
14+
15+
- Missing replaces in field names
16+
717
## [0.4.50] - 2022-04-28
818

919
### Fixed
@@ -294,6 +304,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
294304
### Fixed
295305
- Removed unnecessary regexp dependency, #7.
296306

307+
[0.4.51]: https://github.com/swaggest/go-code-builder/compare/v0.4.50...v0.4.51
297308
[0.4.50]: https://github.com/swaggest/go-code-builder/compare/v0.4.49...v0.4.50
298309
[0.4.49]: https://github.com/swaggest/go-code-builder/compare/v0.4.48...v0.4.49
299310
[0.4.48]: https://github.com/swaggest/go-code-builder/compare/v0.4.47...v0.4.48

composer.lock

Lines changed: 21 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JsonSchema/GoBuilder.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,7 @@ public function typeName($schema, $path, $parentStruct = null)
465465
$typeName = $this->codeBuilder->exportableName($parentStruct->getName() . $propertyName);
466466
}
467467

468-
if (isset($this->options->renames[$typeName])) {
469-
$typeName = $this->options->renames[$typeName];
470-
}
468+
$typeName = $this->replace($typeName);
471469

472470
$tn = $typeName;
473471
$i = 2;
@@ -482,4 +480,29 @@ public function typeName($schema, $path, $parentStruct = null)
482480

483481
return $typeName;
484482
}
483+
484+
/**
485+
* @param string $symbol
486+
* @return mixed
487+
*/
488+
public function replace($symbol)
489+
{
490+
if (empty($symbol)) {
491+
return $symbol;
492+
}
493+
494+
if (isset($this->options->renames[$symbol])) {
495+
return $this->options->renames[$symbol];
496+
}
497+
498+
if (!empty($this->options->rewrites)) {
499+
foreach ($this->options->rewrites as $from => $to) {
500+
if (is_string($from) && is_string($to) && is_string($symbol)) {
501+
$symbol = preg_replace($from, $to, $symbol);
502+
}
503+
}
504+
}
505+
506+
return $symbol;
507+
}
485508
}

src/JsonSchema/Options.php

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Options extends ClassStructure
1010
{
1111
/**
12-
* Remove parent prefix from property name
12+
* Remove parent prefix from property name.
1313
* Example:
1414
* type Property struct { PropertySimple *PropertySimple `json:"-"`}
1515
* would become
@@ -19,25 +19,25 @@ class Options extends ClassStructure
1919
public $trimParentFromPropertyNames = true;
2020

2121
/**
22-
* Hide properties with constant values
22+
* Hide properties with constant values.
2323
* @var bool
2424
*/
2525
public $hideConstProperties = true;
2626

2727
/**
28-
* Use integer types based on minimum/maximum
28+
* Use integer types based on minimum/maximum.
2929
* @var bool
3030
*/
3131
public $optimizeIntegers = true;
3232

3333
/**
34-
* Skip Unmarshal generation
34+
* Skip Unmarshal generation.
3535
* @var bool
3636
*/
3737
public $skipUnmarshal = false;
3838

3939
/**
40-
* Skip Marshal generation
40+
* Skip Marshal generation.
4141
* @var bool
4242
*/
4343
public $skipMarshal = false;
@@ -108,6 +108,12 @@ class Options extends ClassStructure
108108
*/
109109
public $renames = [];
110110

111+
/**
112+
* Map of regexps to replaces, e.g. {"/Definitions([\w\d]+)JSON([\w\d]+)?/i":"${1}${2}"}.
113+
* @var array
114+
*/
115+
public $rewrites = [];
116+
111117
/**
112118
* Only generate schemas that have `x-generate: true`.
113119
* @var bool
@@ -126,11 +132,43 @@ class Options extends ClassStructure
126132
*/
127133
public static function setUpProperties($properties, Schema $ownerSchema)
128134
{
129-
$properties->trimParentFromPropertyNames = Schema::boolean()->setDefault(true);
130-
$properties->hideConstProperties = Schema::boolean()->setDefault(true);
131-
$properties->skipMarshal = Schema::boolean();
132-
$properties->skipUnmarshal = Schema::boolean();
135+
$properties->trimParentFromPropertyNames = Schema::boolean()->setDefault(true)
136+
->setDescription('Remove parent prefix from property name.');
137+
$properties->hideConstProperties = Schema::boolean()->setDefault(true)
138+
->setDescription('Hide properties with constant values');
139+
$properties->optimizeIntegers = Schema::boolean()->setDefault(true)
140+
->setDescription('Use integer types based on minimum/maximum');
141+
$properties->skipMarshal = Schema::boolean()
142+
->setDescription('Skip Marshal generation');
143+
$properties->skipUnmarshal = Schema::boolean()
144+
->setDescription('Skip Unmarshal generation');
145+
$properties->ignoreXGoType = Schema::boolean()
146+
->setDescription('Generate structure for schema with `x-go-type` available.');
147+
$properties->enableXNullable = Schema::boolean()
148+
->setDescription('Add `null` to types if `x-nullable` or `nullable` is available.');
149+
$properties->withZeroValues = Schema::boolean()
150+
->setDescription('Use pointer types to avoid zero value ambiguity.');
151+
$properties->ignoreRequired = Schema::boolean()
152+
->setDescription('Ignore if property is required when deciding on pointer type or omitempty.');
153+
$properties->validateRequired = Schema::boolean()->setDefault(true)
154+
->setDescription('Validate that required properties are present when unmarshaling.');
155+
$properties->ignoreNullable = Schema::boolean()
156+
->setDescription('Add omitempty to nullable values.');
157+
$properties->distinctNull = Schema::boolean()->setDefault(true)
158+
->setDescription('Separate `null` from non-existent key by using `*interface{}` type in property.');
159+
$properties->defaultAdditionalProperties = Schema::boolean()->setDefault(true)
160+
->setDescription('Enable `additionalProperties` if they are missing (null) in schema.');
161+
$properties->inheritSchemaFromExamples = Schema::boolean()
162+
->setDescription('Inherit schema from schema examples where available.');
163+
$properties->fluentSetters = Schema::boolean()
164+
->setDescription('Generate fluent setters for struct fields.');
165+
$properties->renames = Schema::object()->setAdditionalProperties(Schema::string())
166+
->setDescription('Map of default exported symbol names to new ones, e.g. {"PoorlyGenerated":"NicelyReadable"}.');
167+
$properties->rewrites = Schema::object()->setAdditionalProperties(Schema::string())
168+
->setDescription('Map of regexps to replaces, e.g. {"/Definitions([\w\d]+)JSON([\w\d]+)?/i":"${1}${2}"}.');
169+
$properties->requireXGenerate = Schema::boolean()
170+
->setDescription('Only generate schemas that have `x-generate: true`.');
171+
$properties->nameTags = Schema::arr()->setItems(Schema::string())
172+
->setDescription('Set additional field tags with property name.');
133173
}
134-
135-
136174
}

src/JsonSchema/TypeBuilder.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ private function makeName(AnyType $type)
8080
return $type->getName();
8181
}
8282
if ($type instanceof StructType) {
83-
return $type->getName();
83+
$name = $type->getName();
84+
$name = $this->goBuilder->replace($name);
85+
86+
return $name;
8487
}
8588
if ($type instanceof NamedType) {
8689
if ($type->getName() === 'interface{}') {
@@ -232,6 +235,8 @@ private function processOr($orSchemas, $kind)
232235
$betterName = $this->goBuilder->codeBuilder->exportableName($betterName, true);
233236
}
234237

238+
$betterName = $this->goBuilder->replace($betterName);
239+
235240
if (!empty($betterName) && !isset($props[$betterName])) {
236241
$name = $betterName;
237242
}
@@ -564,9 +569,7 @@ private function processConst()
564569
$constName .= 'Empty';
565570
}
566571

567-
if (isset($this->goBuilder->options->renames[$constName])) {
568-
$constName = $this->goBuilder->options->renames[$constName];
569-
}
572+
$constName = $this->goBuilder->replace($constName);
570573

571574
$typeConstBlock->addValue(
572575
$constName,
@@ -706,9 +709,7 @@ private function processEnum(NamedType $baseType)
706709
}
707710
}
708711

709-
if (isset($this->goBuilder->options->renames[$itemName])) {
710-
$itemName = $this->goBuilder->options->renames[$itemName];
711-
}
712+
$itemName = $this->goBuilder->replace($itemName);
712713

713714
$typeConstBlock->addValue($itemName, $item, $comment);
714715
}

src/Templates/Struct/StructProperty.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,4 @@ protected function toString()
6868
// no op
6969
return '';
7070
}
71-
72-
7371
}

0 commit comments

Comments
 (0)