33
44package configschema
55
6- type FilterT [T any ] func (string , T ) bool
6+ import "github.com/zclconf/go-cty/cty"
7+
8+ type FilterT [T any ] func (cty.Path , T ) bool
79
810var (
9- FilterReadOnlyAttribute = func (name string , attribute * Attribute ) bool {
11+ FilterReadOnlyAttribute = func (path cty. Path , attribute * Attribute ) bool {
1012 return attribute .Computed && ! attribute .Optional
1113 }
1214
13- FilterHelperSchemaIdAttribute = func (name string , attribute * Attribute ) bool {
14- if name == "id" && attribute .Computed && attribute .Optional {
15+ FilterHelperSchemaIdAttribute = func (path cty. Path , attribute * Attribute ) bool {
16+ if path . Equals ( cty . GetAttrPath ( "id" )) && attribute .Computed && attribute .Optional {
1517 return true
1618 }
1719 return false
1820 }
1921
20- FilterDeprecatedAttribute = func (name string , attribute * Attribute ) bool {
22+ FilterDeprecatedAttribute = func (path cty. Path , attribute * Attribute ) bool {
2123 return attribute .Deprecated
2224 }
2325
24- FilterDeprecatedBlock = func (name string , block * NestedBlock ) bool {
26+ FilterDeprecatedBlock = func (path cty. Path , block * NestedBlock ) bool {
2527 return block .Deprecated
2628 }
2729)
2830
2931func FilterOr [T any ](filters ... FilterT [T ]) FilterT [T ] {
30- return func (name string , value T ) bool {
32+ return func (path cty. Path , value T ) bool {
3133 for _ , f := range filters {
32- if f (name , value ) {
34+ if f (path , value ) {
3335 return true
3436 }
3537 }
@@ -38,6 +40,10 @@ func FilterOr[T any](filters ...FilterT[T]) FilterT[T] {
3840}
3941
4042func (b * Block ) Filter (filterAttribute FilterT [* Attribute ], filterBlock FilterT [* NestedBlock ]) * Block {
43+ return b .filter (nil , filterAttribute , filterBlock )
44+ }
45+
46+ func (b * Block ) filter (path cty.Path , filterAttribute FilterT [* Attribute ], filterBlock FilterT [* NestedBlock ]) * Block {
4147 ret := & Block {
4248 Description : b .Description ,
4349 DescriptionKind : b .DescriptionKind ,
@@ -48,10 +54,11 @@ func (b *Block) Filter(filterAttribute FilterT[*Attribute], filterBlock FilterT[
4854 ret .Attributes = make (map [string ]* Attribute , len (b .Attributes ))
4955 }
5056 for name , attrS := range b .Attributes {
51- if filterAttribute == nil || ! filterAttribute (name , attrS ) {
57+ path := path .GetAttr (name )
58+ if filterAttribute == nil || ! filterAttribute (path , attrS ) {
5259 ret .Attributes [name ] = attrS
5360 if attrS .NestedType != nil {
54- ret .Attributes [name ].NestedType = filterNestedType (attrS .NestedType , filterAttribute )
61+ ret .Attributes [name ].NestedType = filterNestedType (attrS .NestedType , path , filterAttribute )
5562 }
5663 }
5764 }
@@ -60,8 +67,9 @@ func (b *Block) Filter(filterAttribute FilterT[*Attribute], filterBlock FilterT[
6067 ret .BlockTypes = make (map [string ]* NestedBlock , len (b .BlockTypes ))
6168 }
6269 for name , blockS := range b .BlockTypes {
63- if filterBlock == nil || ! filterBlock (name , blockS ) {
64- block := blockS .Filter (filterAttribute , filterBlock )
70+ path := path .GetAttr (name )
71+ if filterBlock == nil || ! filterBlock (path , blockS ) {
72+ block := blockS .filter (path , filterAttribute , filterBlock )
6573 ret .BlockTypes [name ] = & NestedBlock {
6674 Block : * block ,
6775 Nesting : blockS .Nesting ,
@@ -74,7 +82,7 @@ func (b *Block) Filter(filterAttribute FilterT[*Attribute], filterBlock FilterT[
7482 return ret
7583}
7684
77- func filterNestedType (obj * Object , filterAttribute FilterT [* Attribute ]) * Object {
85+ func filterNestedType (obj * Object , path cty. Path , filterAttribute FilterT [* Attribute ]) * Object {
7886 if obj == nil {
7987 return nil
8088 }
@@ -85,10 +93,11 @@ func filterNestedType(obj *Object, filterAttribute FilterT[*Attribute]) *Object
8593 }
8694
8795 for name , attrS := range obj .Attributes {
88- if filterAttribute == nil || ! filterAttribute (name , attrS ) {
96+ path := path .GetAttr (name )
97+ if filterAttribute == nil || ! filterAttribute (path , attrS ) {
8998 ret .Attributes [name ] = attrS
9099 if attrS .NestedType != nil {
91- ret .Attributes [name ].NestedType = filterNestedType (attrS .NestedType , filterAttribute )
100+ ret .Attributes [name ].NestedType = filterNestedType (attrS .NestedType , path , filterAttribute )
92101 }
93102 }
94103 }
0 commit comments