Skip to content

Commit

Permalink
Merge pull request #15663 from hashicorp/jbardin/diff-field-reader
Browse files Browse the repository at this point in the history
memoize DiffFieldReader.ReadField
  • Loading branch information
jbardin authored Aug 3, 2017
2 parents dc30761 + ca68723 commit 8a8b431
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions helper/schema/field_reader_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,59 @@ type DiffFieldReader struct {
Diff *terraform.InstanceDiff
Source FieldReader
Schema map[string]*Schema

// cache for memoizing ReadField calls.
cache map[string]cachedFieldReadResult
}

type cachedFieldReadResult struct {
val FieldReadResult
err error
}

func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
if r.cache == nil {
r.cache = make(map[string]cachedFieldReadResult)
}

// Create the cache key by joining around a value that isn't a valid part
// of an address. This assumes that the Source and Schema are not changed
// for the life of this DiffFieldReader.
cacheKey := strings.Join(address, "|")
if cached, ok := r.cache[cacheKey]; ok {
return cached.val, cached.err
}

schemaList := addrToSchema(address, r.Schema)
if len(schemaList) == 0 {
r.cache[cacheKey] = cachedFieldReadResult{}
return FieldReadResult{}, nil
}

var res FieldReadResult
var err error

schema := schemaList[len(schemaList)-1]
switch schema.Type {
case TypeBool, TypeInt, TypeFloat, TypeString:
return r.readPrimitive(address, schema)
res, err = r.readPrimitive(address, schema)
case TypeList:
return readListField(r, address, schema)
res, err = readListField(r, address, schema)
case TypeMap:
return r.readMap(address, schema)
res, err = r.readMap(address, schema)
case TypeSet:
return r.readSet(address, schema)
res, err = r.readSet(address, schema)
case typeObject:
return readObjectField(r, address, schema.Elem.(map[string]*Schema))
res, err = readObjectField(r, address, schema.Elem.(map[string]*Schema))
default:
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
}

r.cache[cacheKey] = cachedFieldReadResult{
val: res,
err: err,
}
return res, err
}

func (r *DiffFieldReader) readMap(
Expand Down

0 comments on commit 8a8b431

Please sign in to comment.