From 89d9b91ab710534e19bb36953b55b9934af6291a Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 20:20:54 +0100 Subject: [PATCH 1/6] feat: add new schema builder methods which allow GC to collect built schemas --- datamodel/high/base/schema_proxy.go | 13 +++++++++++++ datamodel/low/base/schema_proxy.go | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index 188e7347..e59dd838 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -164,6 +164,19 @@ func (sp *SchemaProxy) BuildSchema() (*Schema, error) { return schema, er } +func (sp *SchemaProxy) BuildTempSchema() (*Schema, error) { + if sp == nil { + return nil, nil + } + s := sp.schema.Value.TempSchema() + if s == nil { + return nil, sp.schema.Value.GetBuildError() + } + sch := NewSchema(s) + sch.ParentProxy = sp + return sch, nil +} + // GetBuildError returns any error that was thrown when calling Schema() func (sp *SchemaProxy) GetBuildError() error { return sp.buildError diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index 54e5f54c..b366d4a2 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -88,6 +88,11 @@ func (sp *SchemaProxy) Schema() *Schema { if sp.rendered != nil { return sp.rendered } + sp.rendered = sp.TempSchema() + return sp.rendered +} + +func (sp *SchemaProxy) TempSchema() *Schema { schema := new(Schema) utils.CheckForMergeNodes(sp.vn) err := schema.Build(sp.ctx, sp.vn, sp.idx) @@ -96,8 +101,6 @@ func (sp *SchemaProxy) Schema() *Schema { return nil } schema.ParentProxy = sp // https://github.com/pb33f/libopenapi/issues/29 - sp.rendered = schema - // for all the nodes added, copy them over to the schema if sp.NodeMap != nil { sp.NodeMap.Nodes.Range(func(key, value any) bool { From cd4317019d8b9d0998f44573ee8b406a944df89e Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 21:39:12 +0100 Subject: [PATCH 2/6] debug nil ptr --- datamodel/high/base/schema_proxy.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index e59dd838..e7e90637 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -168,7 +168,9 @@ func (sp *SchemaProxy) BuildTempSchema() (*Schema, error) { if sp == nil { return nil, nil } - s := sp.schema.Value.TempSchema() + schema := sp.schema + value := schema.Value + s := value.TempSchema() if s == nil { return nil, sp.schema.Value.GetBuildError() } From 0d525061bd118361ff60d95a3c0c0ed991d477af Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 21:51:28 +0100 Subject: [PATCH 3/6] add lock nil check? --- datamodel/high/base/schema_proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index e7e90637..c7ec40a1 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -165,7 +165,7 @@ func (sp *SchemaProxy) BuildSchema() (*Schema, error) { } func (sp *SchemaProxy) BuildTempSchema() (*Schema, error) { - if sp == nil { + if sp == nil || sp.lock == nil { return nil, nil } schema := sp.schema From 6e0321a3d29e5617f7e1499ac6f16fad1d431309 Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 22:08:41 +0100 Subject: [PATCH 4/6] nil ptr :crying emoji: --- datamodel/high/base/schema_proxy.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index c7ec40a1..b9e7ab05 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -165,12 +165,10 @@ func (sp *SchemaProxy) BuildSchema() (*Schema, error) { } func (sp *SchemaProxy) BuildTempSchema() (*Schema, error) { - if sp == nil || sp.lock == nil { + if sp.schema == nil { return nil, nil } - schema := sp.schema - value := schema.Value - s := value.TempSchema() + s := sp.schema.Value.TempSchema() if s == nil { return nil, sp.schema.Value.GetBuildError() } From 056b7e33e276cd2cc5b74934c1fac349722d34c3 Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 22:38:55 +0100 Subject: [PATCH 5/6] nill ptr .rendered? --- datamodel/high/base/schema_proxy.go | 3 +++ datamodel/low/base/schema_proxy.go | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/datamodel/high/base/schema_proxy.go b/datamodel/high/base/schema_proxy.go index b9e7ab05..2ebd7ee3 100644 --- a/datamodel/high/base/schema_proxy.go +++ b/datamodel/high/base/schema_proxy.go @@ -165,6 +165,9 @@ func (sp *SchemaProxy) BuildSchema() (*Schema, error) { } func (sp *SchemaProxy) BuildTempSchema() (*Schema, error) { + if sp.rendered != nil { + return sp.rendered, nil + } if sp.schema == nil { return nil, nil } diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index b366d4a2..035cbfec 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -85,14 +85,14 @@ func (sp *SchemaProxy) Build(ctx context.Context, key, value *yaml.Node, idx *in // If anything goes wrong during the build, then nothing is returned and the error that occurred can // be retrieved by using GetBuildError() func (sp *SchemaProxy) Schema() *Schema { - if sp.rendered != nil { - return sp.rendered - } sp.rendered = sp.TempSchema() return sp.rendered } func (sp *SchemaProxy) TempSchema() *Schema { + if sp.rendered != nil { + return sp.rendered + } schema := new(Schema) utils.CheckForMergeNodes(sp.vn) err := schema.Build(sp.ctx, sp.vn, sp.idx) From 717c3a5f0b1a89f5c799c33cd8c9ac577ce0d8e0 Mon Sep 17 00:00:00 2001 From: Paddy Steed Date: Wed, 2 Oct 2024 22:49:54 +0100 Subject: [PATCH 6/6] no low .rendered return --- datamodel/low/base/schema_proxy.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/datamodel/low/base/schema_proxy.go b/datamodel/low/base/schema_proxy.go index 035cbfec..cf35f6ef 100644 --- a/datamodel/low/base/schema_proxy.go +++ b/datamodel/low/base/schema_proxy.go @@ -90,9 +90,6 @@ func (sp *SchemaProxy) Schema() *Schema { } func (sp *SchemaProxy) TempSchema() *Schema { - if sp.rendered != nil { - return sp.rendered - } schema := new(Schema) utils.CheckForMergeNodes(sp.vn) err := schema.Build(sp.ctx, sp.vn, sp.idx)