99
1010 "github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
1111 "github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
12+ "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1213)
1314
1415type DataSourceAttribute interface {
@@ -20,6 +21,7 @@ type DataSourceAttribute interface {
2021
2122type DataSourceNestedAttribute interface {
2223 ApplyNestedOverride ([]string , explorer.Override ) (DataSourceAttribute , error )
24+ NestedMerge ([]string , DataSourceAttribute , schema.ComputedOptionalRequired ) (DataSourceAttribute , error )
2325}
2426
2527type DataSourceAttributes []DataSourceAttribute
@@ -58,6 +60,46 @@ func (targetSlice DataSourceAttributes) Merge(mergeSlices ...DataSourceAttribute
5860 return targetSlice , errResult
5961}
6062
63+ func (targetSlice DataSourceAttributes ) MergeAttribute (path []string , attribute DataSourceAttribute , intermediateComputability schema.ComputedOptionalRequired ) (DataSourceAttributes , error ) {
64+ var errResult error
65+ if len (path ) == 0 {
66+ return targetSlice , errResult
67+ }
68+ for i , target := range targetSlice {
69+ if target .GetName () == path [0 ] {
70+
71+ if len (path ) > 1 {
72+ nestedTarget , ok := target .(DataSourceNestedAttribute )
73+ if ! ok {
74+ // TODO: error? there is a nested override for an attribute that is not a nested type
75+ break
76+ }
77+
78+ // The attribute we need to override is deeper nested, move up
79+ nextPath := path [1 :]
80+
81+ overriddenTarget , err := nestedTarget .NestedMerge (nextPath , attribute , intermediateComputability )
82+ errResult = errors .Join (errResult , err )
83+
84+ targetSlice [i ] = overriddenTarget
85+
86+ } else {
87+ // No more path to traverse, apply merge, bidirectional
88+ overriddenTarget , err := attribute .Merge (target )
89+ errResult = errors .Join (errResult , err )
90+ overriddenTarget , err = overriddenTarget .Merge (attribute )
91+ errResult = errors .Join (errResult , err )
92+
93+ targetSlice [i ] = overriddenTarget
94+ }
95+
96+ break
97+ }
98+ }
99+
100+ return targetSlice , errResult
101+ }
102+
61103func (attributes DataSourceAttributes ) ToSpec () []datasource.Attribute {
62104 specAttributes := make ([]datasource.Attribute , 0 , len (attributes ))
63105 for _ , attribute := range attributes {
0 commit comments