Skip to content

Commit

Permalink
Generate field ensurers when fluent setters are enabled (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Apr 7, 2020
1 parent 5145acb commit 922ccbf
Show file tree
Hide file tree
Showing 4 changed files with 750 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.33] - 2020-04-08

### Added
- Field ensurer generation when fluent setters are enabled.

## [0.4.32] - 2020-04-04

### Added
Expand Down Expand Up @@ -196,6 +201,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Removed unnecessary regexp dependency, #7.

[0.4.33]: https://github.com/swaggest/go-code-builder/compare/v0.4.32...v0.4.33
[0.4.32]: https://github.com/swaggest/go-code-builder/compare/v0.4.31...v0.4.32
[0.4.31]: https://github.com/swaggest/go-code-builder/compare/v0.4.30...v0.4.31
[0.4.30]: https://github.com/swaggest/go-code-builder/compare/v0.4.29...v0.4.30
Expand Down
33 changes: 33 additions & 0 deletions src/Templates/Struct/FluentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ public static function addToStruct(StructDef $structDef, StructProperty $goPrope
if ($map !== null) {
$structDef->addFunc($map);
}
$ensurer = self::makeEnsurer($structDef, $goProperty);
if ($ensurer !== null) {
$structDef->addFunc($ensurer);
}
}

public static function makeEnsurer(StructDef $structDef, StructProperty $goProperty) {
$type = $goProperty->getType();

if (!$type instanceof Pointer || !$type->getType() instanceof StructType) {
return null;
}

$receiver = strtolower($structDef->getType()->getName()[0]);

$ensurer = new FuncDef(
$goProperty->getName() . 'Ens',
$goProperty->getName() . 'Ens ensures returned ' . $goProperty->getName() . ' is not nil.'
);
$ensurer->setSelf(new Argument($receiver, new Pointer($structDef->getType())));

$ensurer->setResult((new Result())->add(null, $type));

$ensurer->setBody(new Code(<<<GO
if {$receiver}.{$goProperty->getName()} == nil {
{$receiver}.{$goProperty->getName()} = new({$type->getType()->getTypeString()})
}
return {$receiver}.{$goProperty->getName()}
GO
));

return $ensurer;
}

public static function make(StructDef $structDef, StructProperty $goProperty)
Expand Down
117 changes: 117 additions & 0 deletions tests/resources/go/draft7/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,30 @@ func (s *Schema) WithAdditionalItems(val SchemaOrBool) *Schema {
return s
}

// AdditionalItemsEns ensures returned AdditionalItems is not nil.
func (s *Schema) AdditionalItemsEns() *SchemaOrBool {
if s.AdditionalItems == nil {
s.AdditionalItems = new(SchemaOrBool)
}

return s.AdditionalItems
}

// WithItems sets Items value.
func (s *Schema) WithItems(val Items) *Schema {
s.Items = &val
return s
}

// ItemsEns ensures returned Items is not nil.
func (s *Schema) ItemsEns() *Items {
if s.Items == nil {
s.Items = new(Items)
}

return s.Items
}

// WithMaxItems sets MaxItems value.
func (s *Schema) WithMaxItems(val int64) *Schema {
s.MaxItems = &val
Expand All @@ -197,6 +215,15 @@ func (s *Schema) WithContains(val SchemaOrBool) *Schema {
return s
}

// ContainsEns ensures returned Contains is not nil.
func (s *Schema) ContainsEns() *SchemaOrBool {
if s.Contains == nil {
s.Contains = new(SchemaOrBool)
}

return s.Contains
}

// WithMaxProperties sets MaxProperties value.
func (s *Schema) WithMaxProperties(val int64) *Schema {
s.MaxProperties = &val
Expand All @@ -221,6 +248,15 @@ func (s *Schema) WithAdditionalProperties(val SchemaOrBool) *Schema {
return s
}

// AdditionalPropertiesEns ensures returned AdditionalProperties is not nil.
func (s *Schema) AdditionalPropertiesEns() *SchemaOrBool {
if s.AdditionalProperties == nil {
s.AdditionalProperties = new(SchemaOrBool)
}

return s.AdditionalProperties
}

// WithDefinitions sets Definitions value.
func (s *Schema) WithDefinitions(val map[string]SchemaOrBool) *Schema {
s.Definitions = val
Expand Down Expand Up @@ -295,6 +331,15 @@ func (s *Schema) WithPropertyNames(val SchemaOrBool) *Schema {
return s
}

// PropertyNamesEns ensures returned PropertyNames is not nil.
func (s *Schema) PropertyNamesEns() *SchemaOrBool {
if s.PropertyNames == nil {
s.PropertyNames = new(SchemaOrBool)
}

return s.PropertyNames
}

// WithConst sets Const value.
func (s *Schema) WithConst(val interface{}) *Schema {
s.Const = &val
Expand All @@ -313,6 +358,15 @@ func (s *Schema) WithType(val Type) *Schema {
return s
}

// TypeEns ensures returned Type is not nil.
func (s *Schema) TypeEns() *Type {
if s.Type == nil {
s.Type = new(Type)
}

return s.Type
}

// WithFormat sets Format value.
func (s *Schema) WithFormat(val string) *Schema {
s.Format = &val
Expand All @@ -337,18 +391,45 @@ func (s *Schema) WithIf(val SchemaOrBool) *Schema {
return s
}

// IfEns ensures returned If is not nil.
func (s *Schema) IfEns() *SchemaOrBool {
if s.If == nil {
s.If = new(SchemaOrBool)
}

return s.If
}

// WithThen sets Then value.
func (s *Schema) WithThen(val SchemaOrBool) *Schema {
s.Then = &val
return s
}

// ThenEns ensures returned Then is not nil.
func (s *Schema) ThenEns() *SchemaOrBool {
if s.Then == nil {
s.Then = new(SchemaOrBool)
}

return s.Then
}

// WithElse sets Else value.
func (s *Schema) WithElse(val SchemaOrBool) *Schema {
s.Else = &val
return s
}

// ElseEns ensures returned Else is not nil.
func (s *Schema) ElseEns() *SchemaOrBool {
if s.Else == nil {
s.Else = new(SchemaOrBool)
}

return s.Else
}

// WithAllOf sets AllOf value.
func (s *Schema) WithAllOf(val ...SchemaOrBool) *Schema {
s.AllOf = val
Expand All @@ -373,6 +454,15 @@ func (s *Schema) WithNot(val SchemaOrBool) *Schema {
return s
}

// NotEns ensures returned Not is not nil.
func (s *Schema) NotEns() *SchemaOrBool {
if s.Not == nil {
s.Not = new(SchemaOrBool)
}

return s.Not
}

// WithExtraProperties sets ExtraProperties value.
func (s *Schema) WithExtraProperties(val map[string]interface{}) *Schema {
s.ExtraProperties = val
Expand Down Expand Up @@ -519,6 +609,15 @@ func (s *SchemaOrBool) WithTypeObject(val Schema) *SchemaOrBool {
return s
}

// TypeObjectEns ensures returned TypeObject is not nil.
func (s *SchemaOrBool) TypeObjectEns() *Schema {
if s.TypeObject == nil {
s.TypeObject = new(Schema)
}

return s.TypeObject
}

// WithTypeBoolean sets TypeBoolean value.
func (s *SchemaOrBool) WithTypeBoolean(val bool) *SchemaOrBool {
s.TypeBoolean = &val
Expand Down Expand Up @@ -573,6 +672,15 @@ func (i *Items) WithSchemaOrBool(val SchemaOrBool) *Items {
return i
}

// SchemaOrBoolEns ensures returned SchemaOrBool is not nil.
func (i *Items) SchemaOrBoolEns() *SchemaOrBool {
if i.SchemaOrBool == nil {
i.SchemaOrBool = new(SchemaOrBool)
}

return i.SchemaOrBool
}

// WithSchemaArray sets SchemaArray value.
func (i *Items) WithSchemaArray(val ...SchemaOrBool) *Items {
i.SchemaArray = val
Expand Down Expand Up @@ -626,6 +734,15 @@ func (d *DependenciesAdditionalProperties) WithSchemaOrBool(val SchemaOrBool) *D
return d
}

// SchemaOrBoolEns ensures returned SchemaOrBool is not nil.
func (d *DependenciesAdditionalProperties) SchemaOrBoolEns() *SchemaOrBool {
if d.SchemaOrBool == nil {
d.SchemaOrBool = new(SchemaOrBool)
}

return d.SchemaOrBool
}

// WithStringArray sets StringArray value.
func (d *DependenciesAdditionalProperties) WithStringArray(val ...string) *DependenciesAdditionalProperties {
d.StringArray = val
Expand Down
Loading

0 comments on commit 922ccbf

Please sign in to comment.