Skip to content

Commit

Permalink
cty: Additional path convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng authored Apr 7, 2020
1 parent ae5cf61 commit e385b14
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cty/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,31 @@ func (p Path) Index(v Value) Path {
return ret
}

// IndexInt is a typed convenience method for Index.
func (p Path) IndexInt(v int) Path {
return p.Index(NumberIntVal(int64(v)))
}

// IndexString is a typed convenience method for Index.
func (p Path) IndexString(v string) Path {
return p.Index(StringVal(v))
}

// IndexPath is a convenience method to start a new Path with an IndexStep.
func IndexPath(v Value) Path {
return Path{}.Index(v)
}

// IndexIntPath is a typed convenience method for IndexPath.
func IndexIntPath(v int) Path {
return IndexPath(NumberIntVal(int64(v)))
}

// IndexStringPath is a typed convenience method for IndexPath.
func IndexStringPath(v string) Path {
return IndexPath(StringVal(v))
}

// GetAttr returns a new Path that is the reciever with a GetAttrStep appended
// to the end.
//
Expand Down
52 changes: 52 additions & 0 deletions cty/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,58 @@ func TestPathEquals(t *testing.T) {
cty.GetAttrStep{Name: "attr"},
},
},

// tests for convenience methods
{
A: cty.Path{
cty.GetAttrStep{Name: "attr"},
},
B: cty.GetAttrPath("attr"),
Prefix: true,
Equal: true,
},
{
A: cty.Path{
cty.IndexStep{Key: cty.NumberIntVal(0)},
},
B: cty.IndexPath(cty.NumberIntVal(0)),
Prefix: true,
Equal: true,
},
{
A: cty.Path{
cty.IndexStep{Key: cty.NumberIntVal(0)},
},
B: cty.IndexIntPath(0),
Prefix: true,
Equal: true,
},
{
A: cty.Path{
cty.IndexStep{Key: cty.StringVal("key")},
},
B: cty.IndexStringPath("key"),
Prefix: true,
Equal: true,
},
{
A: cty.Path{
cty.GetAttrStep{Name: "attr"},
cty.IndexStep{Key: cty.NumberIntVal(0)},
},
B: cty.GetAttrPath("attr").IndexInt(0),
Prefix: true,
Equal: true,
},
{
A: cty.Path{
cty.GetAttrStep{Name: "attr"},
cty.IndexStep{Key: cty.StringVal("key")},
},
B: cty.GetAttrPath("attr").IndexString("key"),
Prefix: true,
Equal: true,
},
}

for i, test := range tests {
Expand Down

0 comments on commit e385b14

Please sign in to comment.