]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - vendor/github.com/zclconf/go-cty/cty/path.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / zclconf / go-cty / cty / path.go
index 84a9de0c42a38bc5e14867cc079056d9819bf79a..bf1a7c15aa017eff10017eea9dca77715a8c65ae 100644 (file)
@@ -15,6 +15,10 @@ import (
 // but callers can also feel free to just produce a slice of PathStep manually
 // and convert to this type, which may be more appropriate in environments
 // where memory pressure is a concern.
+//
+// Although a Path is technically mutable, by convention callers should not
+// mutate a path once it has been built and passed to some other subsystem.
+// Instead, use Copy and then mutate the copy before using it.
 type Path []PathStep
 
 // PathStep represents a single step down into a data structure, as part
@@ -47,6 +51,11 @@ func (p Path) Index(v Value) Path {
        return ret
 }
 
+// IndexPath is a convenience method to start a new Path with an IndexStep.
+func IndexPath(v Value) Path {
+       return Path{}.Index(v)
+}
+
 // GetAttr returns a new Path that is the reciever with a GetAttrStep appended
 // to the end.
 //
@@ -62,6 +71,11 @@ func (p Path) GetAttr(name string) Path {
        return ret
 }
 
+// GetAttrPath is a convenience method to start a new Path with a GetAttrStep.
+func GetAttrPath(name string) Path {
+       return Path{}.GetAttr(name)
+}
+
 // Apply applies each of the steps in turn to successive values starting with
 // the given value, and returns the result. If any step returns an error,
 // the whole operation returns an error.
@@ -132,9 +146,13 @@ type IndexStep struct {
 // Apply returns the value resulting from indexing the given value with
 // our key value.
 func (s IndexStep) Apply(val Value) (Value, error) {
+       if val == NilVal || val.IsNull() {
+               return NilVal, errors.New("cannot index a null value")
+       }
+
        switch s.Key.Type() {
        case Number:
-               if !val.Type().IsListType() {
+               if !(val.Type().IsListType() || val.Type().IsTupleType()) {
                        return NilVal, errors.New("not a list type")
                }
        case String:
@@ -170,6 +188,10 @@ type GetAttrStep struct {
 // Apply returns the value of our named attribute from the given value, which
 // must be of an object type that has a value of that name.
 func (s GetAttrStep) Apply(val Value) (Value, error) {
+       if val == NilVal || val.IsNull() {
+               return NilVal, errors.New("cannot access attributes on a null value")
+       }
+
        if !val.Type().IsObjectType() {
                return NilVal, errors.New("not an object type")
        }