2021-01-12 00:11:37 +01:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AttributeByPath looks up the Attribute schema which corresponds to the given
|
|
|
|
// cty.Path. A nil value is returned if the given path does not correspond to a
|
|
|
|
// specific attribute.
|
|
|
|
func (b *Block) AttributeByPath(path cty.Path) *Attribute {
|
|
|
|
block := b
|
2021-07-22 15:45:33 +02:00
|
|
|
for i, step := range path {
|
2021-01-12 00:11:37 +01:00
|
|
|
switch step := step.(type) {
|
|
|
|
case cty.GetAttrStep:
|
|
|
|
if attr := block.Attributes[step.Name]; attr != nil {
|
2021-07-22 15:45:33 +02:00
|
|
|
// If the Attribute is defined with a NestedType and there's
|
|
|
|
// more to the path, descend into the NestedType
|
|
|
|
if attr.NestedType != nil && i < len(path)-1 {
|
|
|
|
return attr.NestedType.AttributeByPath(path[i+1:])
|
|
|
|
} else if i < len(path)-1 { // There's more to the path, but not more to this Attribute.
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-12 00:11:37 +01:00
|
|
|
return attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if nestedBlock := block.BlockTypes[step.Name]; nestedBlock != nil {
|
|
|
|
block = &nestedBlock.Block
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 15:45:33 +02:00
|
|
|
|
|
|
|
// AttributeByPath recurses through a NestedType to look up the Attribute scheme
|
|
|
|
// which corresponds to the given cty.Path. A nil value is returned if the given
|
|
|
|
// path does not correspond to a specific attribute.
|
|
|
|
func (o *Object) AttributeByPath(path cty.Path) *Attribute {
|
|
|
|
for i, step := range path {
|
|
|
|
switch step := step.(type) {
|
|
|
|
case cty.GetAttrStep:
|
|
|
|
if attr := o.Attributes[step.Name]; attr != nil {
|
|
|
|
if attr.NestedType != nil && i < len(path)-1 {
|
|
|
|
return attr.NestedType.AttributeByPath(path[i+1:])
|
|
|
|
} else if i < len(path)-1 { // There's more to the path, but not more to this Attribute.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|