Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tftypes: Added AttributePath LastStep method and AttributePathStep Equal method #112

Merged
merged 4 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/112.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
tftypes: Added `AttributePath` `LastStep()` method and `AttributePathStep` `Equal()` method
```
81 changes: 66 additions & 15 deletions tftypes/attribute_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,9 @@ func (a *AttributePath) Equal(o *AttributePath) bool {
}
for pos, aStep := range a.Steps() {
oStep := o.Steps()[pos]
switch aStep.(type) {
case AttributeName, ElementKeyString, ElementKeyInt:
if oStep != aStep {
return false
}
case ElementKeyValue:
oVal, ok := oStep.(ElementKeyValue)
if !ok {
return false
}
if !Value(aStep.(ElementKeyValue)).Equal(Value(oVal)) {
return false
}
default:
panic(fmt.Sprintf("unknown step %T in AttributePath.Equal", aStep))

if !aStep.Equal(oStep) {
return false
}
}
return true
Expand Down Expand Up @@ -129,6 +117,18 @@ func (a *AttributePath) NewError(err error) error {
}
}

// LastStep returns the last step in the path. If the path was
// empty, nil is returned.
func (a *AttributePath) LastStep() AttributePathStep {
steps := a.Steps()

if len(steps) == 0 {
return nil
}

return steps[len(steps)-1]
}

// WithAttributeName adds an AttributeName step to `a`, using `name` as the
// attribute's name. `a` is copied, not modified.
func (a *AttributePath) WithAttributeName(name string) *AttributePath {
Expand Down Expand Up @@ -185,6 +185,9 @@ func (a *AttributePath) WithoutLastStep() *AttributePath {
// indicating a specific attribute or element that is the next value in the
// path.
type AttributePathStep interface {
// Equal returns true if the AttributePathStep is equal to the other.
Equal(AttributePathStep) bool

unfulfillable() // make this interface fillable only by this package
}

Expand All @@ -199,20 +202,56 @@ var (
// AttributeName is the name of the attribute to be selected.
type AttributeName string

// Equal returns true if the other AttributePathStep is an AttributeName and
// has the same value.
func (a AttributeName) Equal(other AttributePathStep) bool {
otherA, ok := other.(AttributeName)

if !ok {
return false
}

return string(a) == string(otherA)
}

func (a AttributeName) unfulfillable() {}

// ElementKeyString is an AttributePathStep implementation that indicates the
// next step in the AttributePath is to select an element using a string key.
// The value of the ElementKeyString is the key of the element to select.
type ElementKeyString string

// Equal returns true if the other AttributePathStep is an ElementKeyString and
// has the same value.
func (e ElementKeyString) Equal(other AttributePathStep) bool {
otherE, ok := other.(ElementKeyString)

if !ok {
return false
}

return string(e) == string(otherE)
}

func (e ElementKeyString) unfulfillable() {}

// ElementKeyInt is an AttributePathStep implementation that indicates the next
// step in the AttributePath is to select an element using an int64 key. The
// value of the ElementKeyInt is the key of the element to select.
type ElementKeyInt int64

// Equal returns true if the other AttributePathStep is an ElementKeyInt and
// has the same value.
func (e ElementKeyInt) Equal(other AttributePathStep) bool {
otherE, ok := other.(ElementKeyInt)

if !ok {
return false
}

return int(e) == int(otherE)
}

func (e ElementKeyInt) unfulfillable() {}

// ElementKeyValue is an AttributePathStep implementation that indicates the
Expand All @@ -221,6 +260,18 @@ func (e ElementKeyInt) unfulfillable() {}
// to select.
type ElementKeyValue Value

// Equal returns true if the other AttributePathStep is an ElementKeyValue and
// has the same value.
func (e ElementKeyValue) Equal(other AttributePathStep) bool {
otherE, ok := other.(ElementKeyValue)

if !ok {
return false
}

return Value(e).Equal(Value(otherE))
}

func (e ElementKeyValue) unfulfillable() {}

// AttributePathStepper is an interface that types can implement to make them
Expand Down
Loading