8 // A Path is a sequence of operations to locate a nested value within a
11 // The empty Path represents the given item. Any PathSteps within represent
12 // taking a single step down into a data structure.
14 // Path has some convenience methods for gradually constructing a path,
15 // but callers can also feel free to just produce a slice of PathStep manually
16 // and convert to this type, which may be more appropriate in environments
17 // where memory pressure is a concern.
19 // Although a Path is technically mutable, by convention callers should not
20 // mutate a path once it has been built and passed to some other subsystem.
21 // Instead, use Copy and then mutate the copy before using it.
24 // PathStep represents a single step down into a data structure, as part
25 // of a Path. PathStep is a closed interface, meaning that the only
26 // permitted implementations are those within this package.
27 type PathStep interface {
28 pathStepSigil() pathStepImpl
29 Apply(Value) (Value, error)
32 // embed pathImpl into a struct to declare it a PathStep implementation
33 type pathStepImpl struct{}
35 func (p pathStepImpl) pathStepSigil() pathStepImpl {
39 // Index returns a new Path that is the reciever with an IndexStep appended
42 // This is provided as a convenient way to construct paths, but each call
43 // will create garbage so it should not be used where memory pressure is a
45 func (p Path) Index(v Value) Path {
46 ret := make(Path, len(p)+1)
48 ret[len(p)] = IndexStep{
54 // IndexPath is a convenience method to start a new Path with an IndexStep.
55 func IndexPath(v Value) Path {
56 return Path{}.Index(v)
59 // GetAttr returns a new Path that is the reciever with a GetAttrStep appended
62 // This is provided as a convenient way to construct paths, but each call
63 // will create garbage so it should not be used where memory pressure is a
65 func (p Path) GetAttr(name string) Path {
66 ret := make(Path, len(p)+1)
68 ret[len(p)] = GetAttrStep{
74 // Equals compares 2 Paths for exact equality.
75 func (p Path) Equals(other Path) bool {
76 if len(p) != len(other) {
82 switch pv := pv.(type) {
84 ov, ok := other[i].(GetAttrStep)
89 ov, ok := other[i].(IndexStep)
94 if !pv.Key.RawEquals(ov.Key) {
98 // Any invalid steps default to evaluating false.
107 // HasPrefix determines if the path p contains the provided prefix.
108 func (p Path) HasPrefix(prefix Path) bool {
109 if len(prefix) > len(p) {
113 return p[:len(prefix)].Equals(prefix)
116 // GetAttrPath is a convenience method to start a new Path with a GetAttrStep.
117 func GetAttrPath(name string) Path {
118 return Path{}.GetAttr(name)
121 // Apply applies each of the steps in turn to successive values starting with
122 // the given value, and returns the result. If any step returns an error,
123 // the whole operation returns an error.
124 func (p Path) Apply(val Value) (Value, error) {
126 for i, step := range p {
127 val, err = step.Apply(val)
129 return NilVal, fmt.Errorf("at step %d: %s", i, err)
135 // LastStep applies the given path up to the last step and then returns
136 // the resulting value and the final step.
138 // This is useful when dealing with assignment operations, since in that
139 // case the *value* of the last step is not important (and may not, in fact,
140 // present at all) and we care only about its location.
142 // Since LastStep applies all steps except the last, it will return errors
143 // for those steps in the same way as Apply does.
145 // If the path has *no* steps then the returned PathStep will be nil,
146 // representing that any operation should be applied directly to the
148 func (p Path) LastStep(val Value) (Value, PathStep, error) {
155 journey := p[:len(p)-1]
156 val, err = journey.Apply(val)
158 return NilVal, nil, err
160 return val, p[len(p)-1], nil
163 // Copy makes a shallow copy of the receiver. Often when paths are passed to
164 // caller code they come with the constraint that they are valid only until
165 // the caller returns, due to how they are constructed internally. Callers
166 // can use Copy to conveniently produce a copy of the value that _they_ control
168 func (p Path) Copy() Path {
169 ret := make(Path, len(p))
174 // IndexStep is a Step implementation representing applying the index operation
175 // to a value, which must be of either a list, map, or set type.
177 // When describing a path through a *type* rather than a concrete value,
178 // the Key may be an unknown value, indicating that the step applies to
179 // *any* key of the given type.
181 // When indexing into a set, the Key is actually the element being accessed
182 // itself, since in sets elements are their own identity.
183 type IndexStep struct {
188 // Apply returns the value resulting from indexing the given value with
190 func (s IndexStep) Apply(val Value) (Value, error) {
191 if val == NilVal || val.IsNull() {
192 return NilVal, errors.New("cannot index a null value")
195 switch s.Key.Type() {
197 if !(val.Type().IsListType() || val.Type().IsTupleType()) {
198 return NilVal, errors.New("not a list type")
201 if !val.Type().IsMapType() {
202 return NilVal, errors.New("not a map type")
205 return NilVal, errors.New("key value not number or string")
208 has := val.HasIndex(s.Key)
210 return UnknownVal(val.Type().ElementType()), nil
213 return NilVal, errors.New("value does not have given index key")
216 return val.Index(s.Key), nil
219 func (s IndexStep) GoString() string {
220 return fmt.Sprintf("cty.IndexStep{Key:%#v}", s.Key)
223 // GetAttrStep is a Step implementation representing retrieving an attribute
224 // from a value, which must be of an object type.
225 type GetAttrStep struct {
230 // Apply returns the value of our named attribute from the given value, which
231 // must be of an object type that has a value of that name.
232 func (s GetAttrStep) Apply(val Value) (Value, error) {
233 if val == NilVal || val.IsNull() {
234 return NilVal, errors.New("cannot access attributes on a null value")
237 if !val.Type().IsObjectType() {
238 return NilVal, errors.New("not an object type")
241 if !val.Type().HasAttribute(s.Name) {
242 return NilVal, fmt.Errorf("object has no attribute %q", s.Name)
245 return val.GetAttr(s.Name), nil
248 func (s GetAttrStep) GoString() string {
249 return fmt.Sprintf("cty.GetAttrStep{Name:%q}", s.Name)