Skip to content

Commit 28bfb46

Browse files
califlowerdaveshanley
authored andcommitted
Refactor: extract copySchemaWithParentProxy utility function
Clean up duplicate copying logic by extracting a utility function. No functional changes, just code organization.
1 parent 690be4d commit 28bfb46

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

datamodel/high/base/schema_proxy.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ func NewSchemaProxy(schema *low.NodeReference[*base.SchemaProxy]) *SchemaProxy {
6565
return &SchemaProxy{schema: schema, lock: &sync.Mutex{}}
6666
}
6767

68+
// copySchemaWithParentProxy creates a shallow copy of a schema and sets the ParentProxy
69+
func (sp *SchemaProxy) copySchemaWithParentProxy(schema *Schema) *Schema {
70+
schemaCopy := *schema
71+
schemaCopy.ParentProxy = sp
72+
return &schemaCopy
73+
}
74+
6875
// CreateSchemaProxy will create a new high-level SchemaProxy from a high-level Schema, this acts the same
6976
// as if the SchemaProxy is pre-rendered.
7077
func CreateSchemaProxy(schema *Schema) *SchemaProxy {
@@ -116,11 +123,7 @@ func (sp *SchemaProxy) Schema() *Schema {
116123
loc := fmt.Sprintf("%s:%d:%d", idx.GetSpecAbsolutePath(), sp.schema.GetValueNode().Line, sp.schema.GetValueNode().Column)
117124
if seen, ok := idx.GetHighCache().Load(loc); ok {
118125
idx.HighCacheHit()
119-
// Create a copy of the cached schema with the correct ParentProxy
120-
cachedSchema := seen.(*Schema)
121-
schemaCopy := *cachedSchema // shallow copy
122-
schemaCopy.ParentProxy = sp
123-
return &schemaCopy
126+
return sp.copySchemaWithParentProxy(seen.(*Schema))
124127
} else {
125128
idx.HighCacheMiss()
126129
}
@@ -148,11 +151,8 @@ func (sp *SchemaProxy) Schema() *Schema {
148151
}
149152
}
150153

151-
// Create a copy of the schema to avoid race conditions when setting ParentProxy
152-
schemaCopy := *sch // shallow copy
153-
schemaCopy.ParentProxy = sp
154-
sp.rendered = &schemaCopy
155-
return &schemaCopy
154+
sp.rendered = sp.copySchemaWithParentProxy(sch)
155+
return sp.rendered
156156
}
157157

158158
// IsReference returns true if the SchemaProxy is a reference to another Schema.

0 commit comments

Comments
 (0)