9 "github.com/zclconf/go-cty/cty"
12 // RequiresReplace takes a list of flatmapped paths from a
13 // InstanceDiff.Attributes along with the corresponding cty.Type, and returns
14 // the list of the cty.Paths that are flagged as causing the resource
15 // replacement (RequiresNew).
16 // This will filter out redundant paths, paths that refer to flatmapped indexes
17 // (e.g. "#", "%"), and will return any changes within a set as the path to the
19 func RequiresReplace(attrs []string, ty cty.Type) ([]cty.Path, error) {
22 for _, attr := range attrs {
23 p, err := requiresReplacePath(attr, ty)
28 paths = append(paths, p)
31 // now trim off any trailing paths that aren't GetAttrSteps, since only an
32 // attribute itself can require replacement
33 paths = trimPaths(paths)
35 // There may be redundant paths due to set elements or index attributes
36 // Do some ugly n^2 filtering, but these are always fairly small sets.
37 for i := 0; i < len(paths)-1; i++ {
38 for j := i + 1; j < len(paths); j++ {
39 if reflect.DeepEqual(paths[i], paths[j]) {
40 // swap the tail and slice it off
41 paths[j], paths[len(paths)-1] = paths[len(paths)-1], paths[j]
42 paths = paths[:len(paths)-1]
51 // trimPaths removes any trailing steps that aren't of type GetAttrSet, since
52 // only an attribute itself can require replacement
53 func trimPaths(paths []cty.Path) []cty.Path {
54 var trimmed []cty.Path
55 for _, path := range paths {
58 trimmed = append(trimmed, path)
64 func trimPath(path cty.Path) cty.Path {
66 _, isGetAttr := path[len(path)-1].(cty.GetAttrStep)
70 path = path[:len(path)-1]
75 // requiresReplacePath takes a key from a flatmap along with the cty.Type
76 // describing the structure, and returns the cty.Path that would be used to
77 // reference the nested value in the data structure.
78 // This is used specifically to record the RequiresReplace attributes from a
79 // ResourceInstanceDiff.
80 func requiresReplacePath(k string, ty cty.Type) (cty.Path, error) {
84 if !ty.IsObjectType() {
85 panic(fmt.Sprintf("requires replace path on non-object type: %#v", ty))
88 path, err := pathFromFlatmapKeyObject(k, ty.AttributeTypes())
90 return path, fmt.Errorf("[%s] %s", k, err)
95 func pathSplit(p string) (string, string) {
96 parts := strings.SplitN(p, ".", 2)
105 func pathFromFlatmapKeyObject(key string, atys map[string]cty.Type) (cty.Path, error) {
106 k, rest := pathSplit(key)
108 path := cty.Path{cty.GetAttrStep{Name: k}}
112 return path, fmt.Errorf("attribute %q not found", k)
119 p, err := pathFromFlatmapKeyValue(rest, ty)
124 return append(path, p...), nil
127 func pathFromFlatmapKeyValue(key string, ty cty.Type) (cty.Path, error) {
132 case ty.IsPrimitiveType():
133 err = fmt.Errorf("invalid step %q with type %#v", key, ty)
134 case ty.IsObjectType():
135 path, err = pathFromFlatmapKeyObject(key, ty.AttributeTypes())
136 case ty.IsTupleType():
137 path, err = pathFromFlatmapKeyTuple(key, ty.TupleElementTypes())
139 path, err = pathFromFlatmapKeyMap(key, ty)
140 case ty.IsListType():
141 path, err = pathFromFlatmapKeyList(key, ty)
143 path, err = pathFromFlatmapKeySet(key, ty)
145 err = fmt.Errorf("unrecognized type: %s", ty.FriendlyName())
155 func pathFromFlatmapKeyTuple(key string, etys []cty.Type) (cty.Path, error) {
159 k, rest := pathSplit(key)
161 // we don't need to convert the index keys to paths
166 idx, err := strconv.Atoi(k)
171 path = cty.Path{cty.IndexStep{Key: cty.NumberIntVal(int64(idx))}}
173 if idx >= len(etys) {
174 return path, fmt.Errorf("index %s out of range in %#v", key, etys)
183 p, err := pathFromFlatmapKeyValue(rest, ty.ElementType())
188 return append(path, p...), nil
191 func pathFromFlatmapKeyMap(key string, ty cty.Type) (cty.Path, error) {
196 if !ty.ElementType().IsPrimitiveType() {
197 k, rest = pathSplit(key)
200 // we don't need to convert the index keys to paths
205 path = cty.Path{cty.IndexStep{Key: cty.StringVal(k)}}
211 p, err := pathFromFlatmapKeyValue(rest, ty.ElementType())
216 return append(path, p...), nil
219 func pathFromFlatmapKeyList(key string, ty cty.Type) (cty.Path, error) {
223 k, rest := pathSplit(key)
225 // we don't need to convert the index keys to paths
230 idx, err := strconv.Atoi(k)
235 path = cty.Path{cty.IndexStep{Key: cty.NumberIntVal(int64(idx))}}
241 p, err := pathFromFlatmapKeyValue(rest, ty.ElementType())
246 return append(path, p...), nil
249 func pathFromFlatmapKeySet(key string, ty cty.Type) (cty.Path, error) {
250 // once we hit a set, we can't return consistent paths, so just mark the
251 // set as a whole changed.
255 // FlatmapKeyFromPath returns the flatmap equivalent of the given cty.Path for
256 // use in generating legacy style diffs.
257 func FlatmapKeyFromPath(path cty.Path) string {
260 for _, step := range path {
261 switch step := step.(type) {
262 case cty.GetAttrStep:
263 parts = append(parts, step.Name)
265 switch ty := step.Key.Type(); {
266 case ty == cty.String:
267 parts = append(parts, step.Key.AsString())
268 case ty == cty.Number:
269 i, _ := step.Key.AsBigFloat().Int64()
270 parts = append(parts, strconv.Itoa(int(i)))
275 return strings.Join(parts, ".")