From 922ccbf7ec20b9102afab77637ca4b45ba5866f3 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Wed, 8 Apr 2020 01:21:53 +0200 Subject: [PATCH] Generate field ensurers when fluent setters are enabled (#32) --- CHANGELOG.md | 6 + src/Templates/Struct/FluentSetter.php | 33 ++ tests/resources/go/draft7/entities.go | 117 +++++ tests/resources/go/openapi3/entities.go | 594 ++++++++++++++++++++++++ 4 files changed, 750 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bac5ed8..f4a8512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/Templates/Struct/FluentSetter.php b/src/Templates/Struct/FluentSetter.php index fe70485..8f5d29e 100644 --- a/src/Templates/Struct/FluentSetter.php +++ b/src/Templates/Struct/FluentSetter.php @@ -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(<<getName()} == nil { + {$receiver}.{$goProperty->getName()} = new({$type->getType()->getTypeString()}) +} + +return {$receiver}.{$goProperty->getName()} +GO + )); + + return $ensurer; } public static function make(StructDef $structDef, StructProperty $goProperty) diff --git a/tests/resources/go/draft7/entities.go b/tests/resources/go/draft7/entities.go index 1ae32a6..6940bc4 100644 --- a/tests/resources/go/draft7/entities.go +++ b/tests/resources/go/draft7/entities.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/resources/go/openapi3/entities.go b/tests/resources/go/openapi3/entities.go index 5a0bbd2..314afe4 100644 --- a/tests/resources/go/openapi3/entities.go +++ b/tests/resources/go/openapi3/entities.go @@ -37,12 +37,30 @@ func (o *OpenAPI) WithInfo(val Info) *OpenAPI { return o } +// InfoEns ensures returned Info is not nil. +func (o *OpenAPI) InfoEns() *Info { + if o.Info == nil { + o.Info = new(Info) + } + + return o.Info +} + // WithExternalDocs sets ExternalDocs value. func (o *OpenAPI) WithExternalDocs(val ExternalDocumentation) *OpenAPI { o.ExternalDocs = &val return o } +// ExternalDocsEns ensures returned ExternalDocs is not nil. +func (o *OpenAPI) ExternalDocsEns() *ExternalDocumentation { + if o.ExternalDocs == nil { + o.ExternalDocs = new(ExternalDocumentation) + } + + return o.ExternalDocs +} + // WithServers sets Servers value. func (o *OpenAPI) WithServers(val ...Server) *OpenAPI { o.Servers = val @@ -67,12 +85,30 @@ func (o *OpenAPI) WithPaths(val Paths) *OpenAPI { return o } +// PathsEns ensures returned Paths is not nil. +func (o *OpenAPI) PathsEns() *Paths { + if o.Paths == nil { + o.Paths = new(Paths) + } + + return o.Paths +} + // WithComponents sets Components value. func (o *OpenAPI) WithComponents(val Components) *OpenAPI { o.Components = &val return o } +// ComponentsEns ensures returned Components is not nil. +func (o *OpenAPI) ComponentsEns() *Components { + if o.Components == nil { + o.Components = new(Components) + } + + return o.Components +} + // WithMapOfAnything sets MapOfAnything value. func (o *OpenAPI) WithMapOfAnything(val map[string]interface{}) *OpenAPI { o.MapOfAnything = val @@ -205,12 +241,30 @@ func (i *Info) WithContact(val Contact) *Info { return i } +// ContactEns ensures returned Contact is not nil. +func (i *Info) ContactEns() *Contact { + if i.Contact == nil { + i.Contact = new(Contact) + } + + return i.Contact +} + // WithLicense sets License value. func (i *Info) WithLicense(val License) *Info { i.License = &val return i } +// LicenseEns ensures returned License is not nil. +func (i *Info) LicenseEns() *License { + if i.License == nil { + i.License = new(License) + } + + return i.License +} + // WithVersion sets Version value. func (i *Info) WithVersion(val string) *Info { i.Version = &val @@ -925,6 +979,15 @@ func (t *Tag) WithExternalDocs(val ExternalDocumentation) *Tag { return t } +// ExternalDocsEns ensures returned ExternalDocs is not nil. +func (t *Tag) ExternalDocsEns() *ExternalDocumentation { + if t.ExternalDocs == nil { + t.ExternalDocs = new(ExternalDocumentation) + } + + return t.ExternalDocs +} + // WithMapOfAnything sets MapOfAnything value. func (t *Tag) WithMapOfAnything(val map[string]interface{}) *Tag { t.MapOfAnything = val @@ -1327,6 +1390,15 @@ func (p *Parameter) WithSchema(val SchemaOrRef) *Parameter { return p } +// SchemaEns ensures returned Schema is not nil. +func (p *Parameter) SchemaEns() *SchemaOrRef { + if p.Schema == nil { + p.Schema = new(SchemaOrRef) + } + + return p.Schema +} + // WithContent sets Content value. func (p *Parameter) WithContent(val map[string]MediaType) *Parameter { p.Content = val @@ -1373,12 +1445,30 @@ func (p *Parameter) WithSchemaXORContent(val SchemaXORContentOneOf1) *Parameter return p } +// SchemaXORContentEns ensures returned SchemaXORContent is not nil. +func (p *Parameter) SchemaXORContentEns() *SchemaXORContentOneOf1 { + if p.SchemaXORContent == nil { + p.SchemaXORContent = new(SchemaXORContentOneOf1) + } + + return p.SchemaXORContent +} + // WithLocation sets Location value. func (p *Parameter) WithLocation(val ParameterLocation) *Parameter { p.Location = &val return p } +// LocationEns ensures returned Location is not nil. +func (p *Parameter) LocationEns() *ParameterLocation { + if p.Location == nil { + p.Location = new(ParameterLocation) + } + + return p.Location +} + // WithMapOfAnything sets MapOfAnything value. func (p *Parameter) WithMapOfAnything(val map[string]interface{}) *Parameter { p.MapOfAnything = val @@ -1646,6 +1736,15 @@ func (s *Schema) WithNot(val SchemaOrRef) *Schema { return s } +// NotEns ensures returned Not is not nil. +func (s *Schema) NotEns() *SchemaOrRef { + if s.Not == nil { + s.Not = new(SchemaOrRef) + } + + return s.Not +} + // WithAllOf sets AllOf value. func (s *Schema) WithAllOf(val ...SchemaOrRef) *Schema { s.AllOf = val @@ -1670,6 +1769,15 @@ func (s *Schema) WithItems(val SchemaOrRef) *Schema { return s } +// ItemsEns ensures returned Items is not nil. +func (s *Schema) ItemsEns() *SchemaOrRef { + if s.Items == nil { + s.Items = new(SchemaOrRef) + } + + return s.Items +} + // WithProperties sets Properties value. func (s *Schema) WithProperties(val map[string]SchemaOrRef) *Schema { s.Properties = val @@ -1693,6 +1801,15 @@ func (s *Schema) WithAdditionalProperties(val SchemaAdditionalProperties) *Schem return s } +// AdditionalPropertiesEns ensures returned AdditionalProperties is not nil. +func (s *Schema) AdditionalPropertiesEns() *SchemaAdditionalProperties { + if s.AdditionalProperties == nil { + s.AdditionalProperties = new(SchemaAdditionalProperties) + } + + return s.AdditionalProperties +} + // WithDescription sets Description value. func (s *Schema) WithDescription(val string) *Schema { s.Description = &val @@ -1723,6 +1840,15 @@ func (s *Schema) WithDiscriminator(val Discriminator) *Schema { return s } +// DiscriminatorEns ensures returned Discriminator is not nil. +func (s *Schema) DiscriminatorEns() *Discriminator { + if s.Discriminator == nil { + s.Discriminator = new(Discriminator) + } + + return s.Discriminator +} + // WithReadOnly sets ReadOnly value. func (s *Schema) WithReadOnly(val bool) *Schema { s.ReadOnly = &val @@ -1747,6 +1873,15 @@ func (s *Schema) WithExternalDocs(val ExternalDocumentation) *Schema { return s } +// ExternalDocsEns ensures returned ExternalDocs is not nil. +func (s *Schema) ExternalDocsEns() *ExternalDocumentation { + if s.ExternalDocs == nil { + s.ExternalDocs = new(ExternalDocumentation) + } + + return s.ExternalDocs +} + // WithDeprecated sets Deprecated value. func (s *Schema) WithDeprecated(val bool) *Schema { s.Deprecated = &val @@ -1759,6 +1894,15 @@ func (s *Schema) WithXML(val XML) *Schema { return s } +// XMLEns ensures returned XML is not nil. +func (s *Schema) XMLEns() *XML { + if s.XML == nil { + s.XML = new(XML) + } + + return s.XML +} + // WithMapOfAnything sets MapOfAnything value. func (s *Schema) WithMapOfAnything(val map[string]interface{}) *Schema { s.MapOfAnything = val @@ -1970,12 +2114,30 @@ func (s *SchemaOrRef) WithSchema(val Schema) *SchemaOrRef { return s } +// SchemaEns ensures returned Schema is not nil. +func (s *SchemaOrRef) SchemaEns() *Schema { + if s.Schema == nil { + s.Schema = new(Schema) + } + + return s.Schema +} + // WithSchemaReference sets SchemaReference value. func (s *SchemaOrRef) WithSchemaReference(val SchemaReference) *SchemaOrRef { s.SchemaReference = &val return s } +// SchemaReferenceEns ensures returned SchemaReference is not nil. +func (s *SchemaOrRef) SchemaReferenceEns() *SchemaReference { + if s.SchemaReference == nil { + s.SchemaReference = new(SchemaReference) + } + + return s.SchemaReference +} + // UnmarshalJSON decodes JSON. func (s *SchemaOrRef) UnmarshalJSON(data []byte) error { var err error @@ -2023,6 +2185,15 @@ func (s *SchemaAdditionalProperties) WithSchemaOrRef(val SchemaOrRef) *SchemaAdd return s } +// SchemaOrRefEns ensures returned SchemaOrRef is not nil. +func (s *SchemaAdditionalProperties) SchemaOrRefEns() *SchemaOrRef { + if s.SchemaOrRef == nil { + s.SchemaOrRef = new(SchemaOrRef) + } + + return s.SchemaOrRef +} + // WithBool sets Bool value. func (s *SchemaAdditionalProperties) WithBool(val bool) *SchemaAdditionalProperties { s.Bool = &val @@ -2318,6 +2489,15 @@ func (m *MediaType) WithSchema(val SchemaOrRef) *MediaType { return m } +// SchemaEns ensures returned Schema is not nil. +func (m *MediaType) SchemaEns() *SchemaOrRef { + if m.Schema == nil { + m.Schema = new(SchemaOrRef) + } + + return m.Schema +} + // WithExample sets Example value. func (m *MediaType) WithExample(val interface{}) *MediaType { m.Example = &val @@ -2664,12 +2844,30 @@ func (e *ExampleOrRef) WithExampleReference(val ExampleReference) *ExampleOrRef return e } +// ExampleReferenceEns ensures returned ExampleReference is not nil. +func (e *ExampleOrRef) ExampleReferenceEns() *ExampleReference { + if e.ExampleReference == nil { + e.ExampleReference = new(ExampleReference) + } + + return e.ExampleReference +} + // WithExample sets Example value. func (e *ExampleOrRef) WithExample(val Example) *ExampleOrRef { e.Example = &val return e } +// ExampleEns ensures returned Example is not nil. +func (e *ExampleOrRef) ExampleEns() *Example { + if e.Example == nil { + e.Example = new(Example) + } + + return e.Example +} + // UnmarshalJSON decodes JSON. func (e *ExampleOrRef) UnmarshalJSON(data []byte) error { var err error @@ -2864,6 +3062,15 @@ func (h *Header) WithSchema(val SchemaOrRef) *Header { return h } +// SchemaEns ensures returned Schema is not nil. +func (h *Header) SchemaEns() *SchemaOrRef { + if h.Schema == nil { + h.Schema = new(SchemaOrRef) + } + + return h.Schema +} + // WithContent sets Content value. func (h *Header) WithContent(val map[string]MediaType) *Header { h.Content = val @@ -3404,24 +3611,60 @@ func (p *ParameterLocation) WithOneOf0(val ParameterLocationOneOf0) *ParameterLo return p } +// OneOf0Ens ensures returned OneOf0 is not nil. +func (p *ParameterLocation) OneOf0Ens() *ParameterLocationOneOf0 { + if p.OneOf0 == nil { + p.OneOf0 = new(ParameterLocationOneOf0) + } + + return p.OneOf0 +} + // WithOneOf1 sets OneOf1 value. func (p *ParameterLocation) WithOneOf1(val ParameterLocationOneOf1) *ParameterLocation { p.OneOf1 = &val return p } +// OneOf1Ens ensures returned OneOf1 is not nil. +func (p *ParameterLocation) OneOf1Ens() *ParameterLocationOneOf1 { + if p.OneOf1 == nil { + p.OneOf1 = new(ParameterLocationOneOf1) + } + + return p.OneOf1 +} + // WithOneOf2 sets OneOf2 value. func (p *ParameterLocation) WithOneOf2(val ParameterLocationOneOf2) *ParameterLocation { p.OneOf2 = &val return p } +// OneOf2Ens ensures returned OneOf2 is not nil. +func (p *ParameterLocation) OneOf2Ens() *ParameterLocationOneOf2 { + if p.OneOf2 == nil { + p.OneOf2 = new(ParameterLocationOneOf2) + } + + return p.OneOf2 +} + // WithOneOf3 sets OneOf3 value. func (p *ParameterLocation) WithOneOf3(val ParameterLocationOneOf3) *ParameterLocation { p.OneOf3 = &val return p } +// OneOf3Ens ensures returned OneOf3 is not nil. +func (p *ParameterLocation) OneOf3Ens() *ParameterLocationOneOf3 { + if p.OneOf3 == nil { + p.OneOf3 = new(ParameterLocationOneOf3) + } + + return p.OneOf3 +} + // UnmarshalJSON decodes JSON. func (p *ParameterLocation) UnmarshalJSON(data []byte) error { var err error @@ -3485,12 +3728,30 @@ func (p *ParameterOrRef) WithParameterReference(val ParameterReference) *Paramet return p } +// ParameterReferenceEns ensures returned ParameterReference is not nil. +func (p *ParameterOrRef) ParameterReferenceEns() *ParameterReference { + if p.ParameterReference == nil { + p.ParameterReference = new(ParameterReference) + } + + return p.ParameterReference +} + // WithParameter sets Parameter value. func (p *ParameterOrRef) WithParameter(val Parameter) *ParameterOrRef { p.Parameter = &val return p } +// ParameterEns ensures returned Parameter is not nil. +func (p *ParameterOrRef) ParameterEns() *Parameter { + if p.Parameter == nil { + p.Parameter = new(Parameter) + } + + return p.Parameter +} + // UnmarshalJSON decodes JSON. func (p *ParameterOrRef) UnmarshalJSON(data []byte) error { var err error @@ -3567,6 +3828,15 @@ func (o *Operation) WithExternalDocs(val ExternalDocumentation) *Operation { return o } +// ExternalDocsEns ensures returned ExternalDocs is not nil. +func (o *Operation) ExternalDocsEns() *ExternalDocumentation { + if o.ExternalDocs == nil { + o.ExternalDocs = new(ExternalDocumentation) + } + + return o.ExternalDocs +} + // WithID sets ID value. func (o *Operation) WithID(val string) *Operation { o.ID = &val @@ -3585,12 +3855,30 @@ func (o *Operation) WithRequestBody(val RequestBodyOrRef) *Operation { return o } +// RequestBodyEns ensures returned RequestBody is not nil. +func (o *Operation) RequestBodyEns() *RequestBodyOrRef { + if o.RequestBody == nil { + o.RequestBody = new(RequestBodyOrRef) + } + + return o.RequestBody +} + // WithResponses sets Responses value. func (o *Operation) WithResponses(val Responses) *Operation { o.Responses = &val return o } +// ResponsesEns ensures returned Responses is not nil. +func (o *Operation) ResponsesEns() *Responses { + if o.Responses == nil { + o.Responses = new(Responses) + } + + return o.Responses +} + // WithCallbacks sets Callbacks value. func (o *Operation) WithCallbacks(val map[string]CallbackOrRef) *Operation { o.Callbacks = val @@ -3929,12 +4217,30 @@ func (r *RequestBodyOrRef) WithRequestBodyReference(val RequestBodyReference) *R return r } +// RequestBodyReferenceEns ensures returned RequestBodyReference is not nil. +func (r *RequestBodyOrRef) RequestBodyReferenceEns() *RequestBodyReference { + if r.RequestBodyReference == nil { + r.RequestBodyReference = new(RequestBodyReference) + } + + return r.RequestBodyReference +} + // WithRequestBody sets RequestBody value. func (r *RequestBodyOrRef) WithRequestBody(val RequestBody) *RequestBodyOrRef { r.RequestBody = &val return r } +// RequestBodyEns ensures returned RequestBody is not nil. +func (r *RequestBodyOrRef) RequestBodyEns() *RequestBody { + if r.RequestBody == nil { + r.RequestBody = new(RequestBody) + } + + return r.RequestBody +} + // UnmarshalJSON decodes JSON. func (r *RequestBodyOrRef) UnmarshalJSON(data []byte) error { var err error @@ -3983,6 +4289,15 @@ func (r *Responses) WithDefault(val ResponseOrRef) *Responses { return r } +// DefaultEns ensures returned Default is not nil. +func (r *Responses) DefaultEns() *ResponseOrRef { + if r.Default == nil { + r.Default = new(ResponseOrRef) + } + + return r.Default +} + // WithMapOfResponseOrRefValues sets MapOfResponseOrRefValues value. func (r *Responses) WithMapOfResponseOrRefValues(val map[string]ResponseOrRef) *Responses { r.MapOfResponseOrRefValues = val @@ -4400,12 +4715,30 @@ func (h *HeaderOrRef) WithHeaderReference(val HeaderReference) *HeaderOrRef { return h } +// HeaderReferenceEns ensures returned HeaderReference is not nil. +func (h *HeaderOrRef) HeaderReferenceEns() *HeaderReference { + if h.HeaderReference == nil { + h.HeaderReference = new(HeaderReference) + } + + return h.HeaderReference +} + // WithHeader sets Header value. func (h *HeaderOrRef) WithHeader(val Header) *HeaderOrRef { h.Header = &val return h } +// HeaderEns ensures returned Header is not nil. +func (h *HeaderOrRef) HeaderEns() *Header { + if h.Header == nil { + h.Header = new(Header) + } + + return h.Header +} + // UnmarshalJSON decodes JSON. func (h *HeaderOrRef) UnmarshalJSON(data []byte) error { var err error @@ -4560,6 +4893,15 @@ func (l *Link) WithServer(val Server) *Link { return l } +// ServerEns ensures returned Server is not nil. +func (l *Link) ServerEns() *Server { + if l.Server == nil { + l.Server = new(Server) + } + + return l.Server +} + // WithMapOfAnything sets MapOfAnything value. func (l *Link) WithMapOfAnything(val map[string]interface{}) *Link { l.MapOfAnything = val @@ -4680,12 +5022,30 @@ func (l *LinkOrRef) WithLinkReference(val LinkReference) *LinkOrRef { return l } +// LinkReferenceEns ensures returned LinkReference is not nil. +func (l *LinkOrRef) LinkReferenceEns() *LinkReference { + if l.LinkReference == nil { + l.LinkReference = new(LinkReference) + } + + return l.LinkReference +} + // WithLink sets Link value. func (l *LinkOrRef) WithLink(val Link) *LinkOrRef { l.Link = &val return l } +// LinkEns ensures returned Link is not nil. +func (l *LinkOrRef) LinkEns() *Link { + if l.Link == nil { + l.Link = new(Link) + } + + return l.Link +} + // UnmarshalJSON decodes JSON. func (l *LinkOrRef) UnmarshalJSON(data []byte) error { var err error @@ -4733,12 +5093,30 @@ func (r *ResponseOrRef) WithResponseReference(val ResponseReference) *ResponseOr return r } +// ResponseReferenceEns ensures returned ResponseReference is not nil. +func (r *ResponseOrRef) ResponseReferenceEns() *ResponseReference { + if r.ResponseReference == nil { + r.ResponseReference = new(ResponseReference) + } + + return r.ResponseReference +} + // WithResponse sets Response value. func (r *ResponseOrRef) WithResponse(val Response) *ResponseOrRef { r.Response = &val return r } +// ResponseEns ensures returned Response is not nil. +func (r *ResponseOrRef) ResponseEns() *Response { + if r.Response == nil { + r.Response = new(Response) + } + + return r.Response +} + // UnmarshalJSON decodes JSON. func (r *ResponseOrRef) UnmarshalJSON(data []byte) error { var err error @@ -4946,12 +5324,30 @@ func (c *CallbackOrRef) WithCallbackReference(val CallbackReference) *CallbackOr return c } +// CallbackReferenceEns ensures returned CallbackReference is not nil. +func (c *CallbackOrRef) CallbackReferenceEns() *CallbackReference { + if c.CallbackReference == nil { + c.CallbackReference = new(CallbackReference) + } + + return c.CallbackReference +} + // WithCallback sets Callback value. func (c *CallbackOrRef) WithCallback(val Callback) *CallbackOrRef { c.Callback = &val return c } +// CallbackEns ensures returned Callback is not nil. +func (c *CallbackOrRef) CallbackEns() *Callback { + if c.Callback == nil { + c.Callback = new(Callback) + } + + return c.Callback +} + // UnmarshalJSON decodes JSON. func (c *CallbackOrRef) UnmarshalJSON(data []byte) error { var err error @@ -5118,54 +5514,135 @@ func (c *Components) WithSchemas(val ComponentsSchemas) *Components { return c } +// SchemasEns ensures returned Schemas is not nil. +func (c *Components) SchemasEns() *ComponentsSchemas { + if c.Schemas == nil { + c.Schemas = new(ComponentsSchemas) + } + + return c.Schemas +} + // WithResponses sets Responses value. func (c *Components) WithResponses(val ComponentsResponses) *Components { c.Responses = &val return c } +// ResponsesEns ensures returned Responses is not nil. +func (c *Components) ResponsesEns() *ComponentsResponses { + if c.Responses == nil { + c.Responses = new(ComponentsResponses) + } + + return c.Responses +} + // WithParameters sets Parameters value. func (c *Components) WithParameters(val ComponentsParameters) *Components { c.Parameters = &val return c } +// ParametersEns ensures returned Parameters is not nil. +func (c *Components) ParametersEns() *ComponentsParameters { + if c.Parameters == nil { + c.Parameters = new(ComponentsParameters) + } + + return c.Parameters +} + // WithExamples sets Examples value. func (c *Components) WithExamples(val ComponentsExamples) *Components { c.Examples = &val return c } +// ExamplesEns ensures returned Examples is not nil. +func (c *Components) ExamplesEns() *ComponentsExamples { + if c.Examples == nil { + c.Examples = new(ComponentsExamples) + } + + return c.Examples +} + // WithRequestBodies sets RequestBodies value. func (c *Components) WithRequestBodies(val ComponentsRequestBodies) *Components { c.RequestBodies = &val return c } +// RequestBodiesEns ensures returned RequestBodies is not nil. +func (c *Components) RequestBodiesEns() *ComponentsRequestBodies { + if c.RequestBodies == nil { + c.RequestBodies = new(ComponentsRequestBodies) + } + + return c.RequestBodies +} + // WithHeaders sets Headers value. func (c *Components) WithHeaders(val ComponentsHeaders) *Components { c.Headers = &val return c } +// HeadersEns ensures returned Headers is not nil. +func (c *Components) HeadersEns() *ComponentsHeaders { + if c.Headers == nil { + c.Headers = new(ComponentsHeaders) + } + + return c.Headers +} + // WithSecuritySchemes sets SecuritySchemes value. func (c *Components) WithSecuritySchemes(val ComponentsSecuritySchemes) *Components { c.SecuritySchemes = &val return c } +// SecuritySchemesEns ensures returned SecuritySchemes is not nil. +func (c *Components) SecuritySchemesEns() *ComponentsSecuritySchemes { + if c.SecuritySchemes == nil { + c.SecuritySchemes = new(ComponentsSecuritySchemes) + } + + return c.SecuritySchemes +} + // WithLinks sets Links value. func (c *Components) WithLinks(val ComponentsLinks) *Components { c.Links = &val return c } +// LinksEns ensures returned Links is not nil. +func (c *Components) LinksEns() *ComponentsLinks { + if c.Links == nil { + c.Links = new(ComponentsLinks) + } + + return c.Links +} + // WithCallbacks sets Callbacks value. func (c *Components) WithCallbacks(val ComponentsCallbacks) *Components { c.Callbacks = &val return c } +// CallbacksEns ensures returned Callbacks is not nil. +func (c *Components) CallbacksEns() *ComponentsCallbacks { + if c.Callbacks == nil { + c.Callbacks = new(ComponentsCallbacks) + } + + return c.Callbacks +} + // WithMapOfAnything sets MapOfAnything value. func (c *Components) WithMapOfAnything(val map[string]interface{}) *Components { c.MapOfAnything = val @@ -6083,12 +6560,30 @@ func (h *HTTPSecurityScheme) WithOneOf0(val HTTPSecuritySchemeOneOf0) *HTTPSecur return h } +// OneOf0Ens ensures returned OneOf0 is not nil. +func (h *HTTPSecurityScheme) OneOf0Ens() *HTTPSecuritySchemeOneOf0 { + if h.OneOf0 == nil { + h.OneOf0 = new(HTTPSecuritySchemeOneOf0) + } + + return h.OneOf0 +} + // WithOneOf1 sets OneOf1 value. func (h *HTTPSecurityScheme) WithOneOf1(val HTTPSecuritySchemeOneOf1) *HTTPSecurityScheme { h.OneOf1 = &val return h } +// OneOf1Ens ensures returned OneOf1 is not nil. +func (h *HTTPSecurityScheme) OneOf1Ens() *HTTPSecuritySchemeOneOf1 { + if h.OneOf1 == nil { + h.OneOf1 = new(HTTPSecuritySchemeOneOf1) + } + + return h.OneOf1 +} + // WithMapOfAnything sets MapOfAnything value. func (h *HTTPSecurityScheme) WithMapOfAnything(val map[string]interface{}) *HTTPSecurityScheme { h.MapOfAnything = val @@ -6403,6 +6898,15 @@ func (o *OAuth2SecurityScheme) WithFlows(val OAuthFlows) *OAuth2SecurityScheme { return o } +// FlowsEns ensures returned Flows is not nil. +func (o *OAuth2SecurityScheme) FlowsEns() *OAuthFlows { + if o.Flows == nil { + o.Flows = new(OAuthFlows) + } + + return o.Flows +} + // WithDescription sets Description value. func (o *OAuth2SecurityScheme) WithDescription(val string) *OAuth2SecurityScheme { o.Description = &val @@ -6527,24 +7031,60 @@ func (o *OAuthFlows) WithImplicit(val ImplicitOAuthFlow) *OAuthFlows { return o } +// ImplicitEns ensures returned Implicit is not nil. +func (o *OAuthFlows) ImplicitEns() *ImplicitOAuthFlow { + if o.Implicit == nil { + o.Implicit = new(ImplicitOAuthFlow) + } + + return o.Implicit +} + // WithPassword sets Password value. func (o *OAuthFlows) WithPassword(val PasswordOAuthFlow) *OAuthFlows { o.Password = &val return o } +// PasswordEns ensures returned Password is not nil. +func (o *OAuthFlows) PasswordEns() *PasswordOAuthFlow { + if o.Password == nil { + o.Password = new(PasswordOAuthFlow) + } + + return o.Password +} + // WithClientCredentials sets ClientCredentials value. func (o *OAuthFlows) WithClientCredentials(val ClientCredentialsFlow) *OAuthFlows { o.ClientCredentials = &val return o } +// ClientCredentialsEns ensures returned ClientCredentials is not nil. +func (o *OAuthFlows) ClientCredentialsEns() *ClientCredentialsFlow { + if o.ClientCredentials == nil { + o.ClientCredentials = new(ClientCredentialsFlow) + } + + return o.ClientCredentials +} + // WithAuthorizationCode sets AuthorizationCode value. func (o *OAuthFlows) WithAuthorizationCode(val AuthorizationCodeOAuthFlow) *OAuthFlows { o.AuthorizationCode = &val return o } +// AuthorizationCodeEns ensures returned AuthorizationCode is not nil. +func (o *OAuthFlows) AuthorizationCodeEns() *AuthorizationCodeOAuthFlow { + if o.AuthorizationCode == nil { + o.AuthorizationCode = new(AuthorizationCodeOAuthFlow) + } + + return o.AuthorizationCode +} + // WithMapOfAnything sets MapOfAnything value. func (o *OAuthFlows) WithMapOfAnything(val map[string]interface{}) *OAuthFlows { o.MapOfAnything = val @@ -7310,24 +7850,60 @@ func (s *SecurityScheme) WithAPIKeySecurityScheme(val APIKeySecurityScheme) *Sec return s } +// APIKeySecuritySchemeEns ensures returned APIKeySecurityScheme is not nil. +func (s *SecurityScheme) APIKeySecuritySchemeEns() *APIKeySecurityScheme { + if s.APIKeySecurityScheme == nil { + s.APIKeySecurityScheme = new(APIKeySecurityScheme) + } + + return s.APIKeySecurityScheme +} + // WithHTTPSecurityScheme sets HTTPSecurityScheme value. func (s *SecurityScheme) WithHTTPSecurityScheme(val HTTPSecurityScheme) *SecurityScheme { s.HTTPSecurityScheme = &val return s } +// HTTPSecuritySchemeEns ensures returned HTTPSecurityScheme is not nil. +func (s *SecurityScheme) HTTPSecuritySchemeEns() *HTTPSecurityScheme { + if s.HTTPSecurityScheme == nil { + s.HTTPSecurityScheme = new(HTTPSecurityScheme) + } + + return s.HTTPSecurityScheme +} + // WithOAuth2SecurityScheme sets OAuth2SecurityScheme value. func (s *SecurityScheme) WithOAuth2SecurityScheme(val OAuth2SecurityScheme) *SecurityScheme { s.OAuth2SecurityScheme = &val return s } +// OAuth2SecuritySchemeEns ensures returned OAuth2SecurityScheme is not nil. +func (s *SecurityScheme) OAuth2SecuritySchemeEns() *OAuth2SecurityScheme { + if s.OAuth2SecurityScheme == nil { + s.OAuth2SecurityScheme = new(OAuth2SecurityScheme) + } + + return s.OAuth2SecurityScheme +} + // WithOpenIDConnectSecurityScheme sets OpenIDConnectSecurityScheme value. func (s *SecurityScheme) WithOpenIDConnectSecurityScheme(val OpenIDConnectSecurityScheme) *SecurityScheme { s.OpenIDConnectSecurityScheme = &val return s } +// OpenIDConnectSecuritySchemeEns ensures returned OpenIDConnectSecurityScheme is not nil. +func (s *SecurityScheme) OpenIDConnectSecuritySchemeEns() *OpenIDConnectSecurityScheme { + if s.OpenIDConnectSecurityScheme == nil { + s.OpenIDConnectSecurityScheme = new(OpenIDConnectSecurityScheme) + } + + return s.OpenIDConnectSecurityScheme +} + // UnmarshalJSON decodes JSON. func (s *SecurityScheme) UnmarshalJSON(data []byte) error { var err error @@ -7391,12 +7967,30 @@ func (s *SecuritySchemeOrRef) WithSecuritySchemeReference(val SecuritySchemeRefe return s } +// SecuritySchemeReferenceEns ensures returned SecuritySchemeReference is not nil. +func (s *SecuritySchemeOrRef) SecuritySchemeReferenceEns() *SecuritySchemeReference { + if s.SecuritySchemeReference == nil { + s.SecuritySchemeReference = new(SecuritySchemeReference) + } + + return s.SecuritySchemeReference +} + // WithSecurityScheme sets SecurityScheme value. func (s *SecuritySchemeOrRef) WithSecurityScheme(val SecurityScheme) *SecuritySchemeOrRef { s.SecurityScheme = &val return s } +// SecuritySchemeEns ensures returned SecurityScheme is not nil. +func (s *SecuritySchemeOrRef) SecuritySchemeEns() *SecurityScheme { + if s.SecurityScheme == nil { + s.SecurityScheme = new(SecurityScheme) + } + + return s.SecurityScheme +} + // UnmarshalJSON decodes JSON. func (s *SecuritySchemeOrRef) UnmarshalJSON(data []byte) error { var err error