Skip to content

Commit b4fe89b

Browse files
Introduce shallow clone for shapes (#34)
1 parent 4b7df3b commit b4fe89b

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

complex.go

+36
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func (s *ArrayShape) Base() *BaseShape {
3838
return s.BaseShape
3939
}
4040

41+
func (s *ArrayShape) cloneShallow(base *BaseShape) Shape {
42+
c := *s
43+
c.BaseShape = base
44+
return &c
45+
}
46+
4147
func (s *ArrayShape) clone(base *BaseShape, clonedMap map[int64]*BaseShape) Shape {
4248
c := *s
4349
c.BaseShape = base
@@ -307,6 +313,12 @@ func (s *ObjectShape) Base() *BaseShape {
307313
return s.BaseShape
308314
}
309315

316+
func (s *ObjectShape) cloneShallow(base *BaseShape) Shape {
317+
c := *s
318+
c.BaseShape = base
319+
return &c
320+
}
321+
310322
func (s *ObjectShape) clone(base *BaseShape, clonedMap map[int64]*BaseShape) Shape {
311323
c := *s
312324
c.BaseShape = base
@@ -734,6 +746,12 @@ func (s *UnionShape) Base() *BaseShape {
734746
return s.BaseShape
735747
}
736748

749+
func (s *UnionShape) cloneShallow(base *BaseShape) Shape {
750+
c := *s
751+
c.BaseShape = base
752+
return &c
753+
}
754+
737755
func (s *UnionShape) clone(base *BaseShape, clonedMap map[int64]*BaseShape) Shape {
738756
c := *s
739757
c.BaseShape = base
@@ -833,6 +851,12 @@ func (s *JSONShape) Base() *BaseShape {
833851
return s.BaseShape
834852
}
835853

854+
func (s *JSONShape) cloneShallow(base *BaseShape) Shape {
855+
c := *s
856+
c.BaseShape = base
857+
return &c
858+
}
859+
836860
func (s *JSONShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
837861
c := *s
838862
c.BaseShape = base
@@ -881,6 +905,12 @@ func (s *UnknownShape) Base() *BaseShape {
881905
return s.BaseShape
882906
}
883907

908+
func (s *UnknownShape) cloneShallow(base *BaseShape) Shape {
909+
c := *s
910+
c.BaseShape = base
911+
return &c
912+
}
913+
884914
func (s *UnknownShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
885915
c := *s
886916
c.BaseShape = base
@@ -919,6 +949,12 @@ func (s *RecursiveShape) Base() *BaseShape {
919949
return s.BaseShape
920950
}
921951

952+
func (s *RecursiveShape) cloneShallow(base *BaseShape) Shape {
953+
c := *s
954+
c.BaseShape = base
955+
return &c
956+
}
957+
922958
func (s *RecursiveShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
923959
c := *s
924960
c.BaseShape = base

mocks.go

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func (u MockShape) check() error {
2626
return u.MockCheck()
2727
}
2828

29+
func (u MockShape) cloneShallow(base *BaseShape) Shape {
30+
return u.clone(base, nil)
31+
}
32+
2933
func (u MockShape) clone(base *BaseShape, clonedMap map[int64]*BaseShape) Shape {
3034
return u.MockClone(base, clonedMap)
3135
}

scalars.go

+44
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (s *IntegerShape) Base() *BaseShape {
8282
return s.BaseShape
8383
}
8484

85+
func (s *IntegerShape) cloneShallow(base *BaseShape) Shape {
86+
return s.clone(base, nil)
87+
}
88+
8589
func (s *IntegerShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
8690
c := *s
8791
c.BaseShape = base
@@ -287,6 +291,10 @@ func (s *NumberShape) Base() *BaseShape {
287291
return s.BaseShape
288292
}
289293

294+
func (s *NumberShape) cloneShallow(base *BaseShape) Shape {
295+
return s.clone(base, nil)
296+
}
297+
290298
func (s *NumberShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
291299
c := *s
292300
c.BaseShape = base
@@ -464,6 +472,10 @@ func (s *StringShape) Base() *BaseShape {
464472
return s.BaseShape
465473
}
466474

475+
func (s *StringShape) cloneShallow(base *BaseShape) Shape {
476+
return s.clone(base, nil)
477+
}
478+
467479
func (s *StringShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
468480
c := *s
469481
c.BaseShape = base
@@ -617,6 +629,10 @@ func (s *FileShape) Base() *BaseShape {
617629
return s.BaseShape
618630
}
619631

632+
func (s *FileShape) cloneShallow(base *BaseShape) Shape {
633+
return s.clone(base, nil)
634+
}
635+
620636
func (s *FileShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
621637
c := *s
622638
c.BaseShape = base
@@ -748,6 +764,10 @@ func (s *BooleanShape) Base() *BaseShape {
748764
return s.BaseShape
749765
}
750766

767+
func (s *BooleanShape) cloneShallow(base *BaseShape) Shape {
768+
return s.clone(base, nil)
769+
}
770+
751771
func (s *BooleanShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
752772
c := *s
753773
c.BaseShape = base
@@ -836,6 +856,10 @@ func (s *DateTimeShape) Base() *BaseShape {
836856
return s.BaseShape
837857
}
838858

859+
func (s *DateTimeShape) cloneShallow(base *BaseShape) Shape {
860+
return s.clone(base, nil)
861+
}
862+
839863
func (s *DateTimeShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
840864
c := *s
841865
c.BaseShape = base
@@ -925,6 +949,10 @@ func (s *DateTimeOnlyShape) Base() *BaseShape {
925949
return s.BaseShape
926950
}
927951

952+
func (s *DateTimeOnlyShape) cloneShallow(base *BaseShape) Shape {
953+
return s.clone(base, nil)
954+
}
955+
928956
func (s *DateTimeOnlyShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
929957
c := *s
930958
c.BaseShape = base
@@ -983,6 +1011,10 @@ func (s *DateOnlyShape) Base() *BaseShape {
9831011
return s.BaseShape
9841012
}
9851013

1014+
func (s *DateOnlyShape) cloneShallow(base *BaseShape) Shape {
1015+
return s.clone(base, nil)
1016+
}
1017+
9861018
func (s *DateOnlyShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
9871019
c := *s
9881020
c.BaseShape = base
@@ -1038,6 +1070,10 @@ func (s *TimeOnlyShape) Base() *BaseShape {
10381070
return s.BaseShape
10391071
}
10401072

1073+
func (s *TimeOnlyShape) cloneShallow(base *BaseShape) Shape {
1074+
return s.clone(base, nil)
1075+
}
1076+
10411077
func (s *TimeOnlyShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
10421078
c := *s
10431079
c.BaseShape = base
@@ -1096,6 +1132,10 @@ func (s *AnyShape) Base() *BaseShape {
10961132
return s.BaseShape
10971133
}
10981134

1135+
func (s *AnyShape) cloneShallow(base *BaseShape) Shape {
1136+
return s.clone(base, nil)
1137+
}
1138+
10991139
func (s *AnyShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
11001140
c := *s
11011141
c.BaseShape = base
@@ -1146,6 +1186,10 @@ func (s *NilShape) Base() *BaseShape {
11461186
return s.BaseShape
11471187
}
11481188

1189+
func (s *NilShape) cloneShallow(base *BaseShape) Shape {
1190+
return s.clone(base, nil)
1191+
}
1192+
11491193
func (s *NilShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
11501194
c := *s
11511195
c.BaseShape = base

shape.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ func (s *BaseShape) AliasTo(source *BaseShape) *BaseShape {
268268
return s
269269
}
270270

271+
// CloneShallow creates a shallow copy of the shape.
272+
func (s *BaseShape) CloneShallow() *BaseShape {
273+
c := *s
274+
ptr := &c
275+
c.Shape = s.Shape.cloneShallow(ptr)
276+
return ptr
277+
}
278+
271279
// Clone creates a deep copy of the shape.
272280
//
273281
// Use this method to make a deep copy of the shape while preserving the relationships between shapes.
@@ -392,9 +400,10 @@ type ShapeInheritor interface {
392400
inherit(source Shape) (Shape, error)
393401
}
394402

395-
// ShapeJSONSchema is the interface that provide clone implementation for a RAML shape.
403+
// ShapeCloner is the interface that provide clone implementation for a RAML shape.
396404
type ShapeCloner interface {
397405
clone(base *BaseShape, clonedMap map[int64]*BaseShape) Shape
406+
cloneShallow(base *BaseShape) Shape
398407
}
399408

400409
type ShapeChecker interface {

0 commit comments

Comments
 (0)