diff options
author | 2022-01-27 16:51:51 +0000 | |
---|---|---|
committer | 2022-01-31 12:28:04 +0000 | |
commit | 545c59273de8e741ca8820656d972fbeb03f1ef9 (patch) | |
tree | 976a6ec03a3e543865157e6eaf24146ea31d1f5c /sdk/build_release.go | |
parent | 5ddefa3df4be4bcf3ed86a13df2897a79b97adc3 (diff) |
Refactor build_release and test code
Minor restructuring of the build_release pruning code to make it easier
to add logic for handling fields of maps of structs. That includes:
1. Moving some code that is specific to clearing a selected field
inside the associated if block.
2. Replacing an if with a switch.
3. Improving the error handling by separating the reporting of the
container that broke from information about which field could not be
set. That allows follow up code to provide information about the map
key instead.
The tests were restructed by:
1. Switching from using AssertDeepEquals to compare the structs to
comparing the output of marshalling the structs to JSON. That was
for a couple of reasons. Firstly, because JSON will marshal (and so
allow comparison of) the contents of pointers to structs whereas
AssertDeepEquals will just compare the pointers themselves.
Secondly, because JSON can pretty print the output and make it
easier to read.
2. Using a func to create a new instance of the input structure for
each test. That is to allow the test to modify the input structure,
e.g. by clearing a field in a struct that is pointed to by a map.
The test previously relied on the input structure being immutable
and passed by value but a follow up change will change that by
adding a map field that contains pointers to structs.
Bug: 204763318
Test: m nothing
Change-Id: I84dc99621497b7263e30466895b823eb02cb2b56
Diffstat (limited to 'sdk/build_release.go')
-rw-r--r-- | sdk/build_release.go | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/sdk/build_release.go b/sdk/build_release.go index a3f089973..212582ac6 100644 --- a/sdk/build_release.go +++ b/sdk/build_release.go @@ -230,51 +230,63 @@ func (p *propertyPruner) gatherFields(structType reflect.Type, containingStructA return container.Field(fieldIndex) } - zeroValue := reflect.Zero(field.Type) - fieldPruner := func(container reflect.Value) { - if containingStructAccessor != nil { - // This is an embedded structure so first access the field for the embedded - // structure. - container = containingStructAccessor(container) - } + fieldType := field.Type + if selector(name, field) { + zeroValue := reflect.Zero(fieldType) + fieldPruner := func(container reflect.Value) { + if containingStructAccessor != nil { + // This is an embedded structure so first access the field for the embedded + // structure. + container = containingStructAccessor(container) + } - // Skip through interface and pointer values to find the structure. - container = getStructValue(container) + // Skip through interface and pointer values to find the structure. + container = getStructValue(container) - defer func() { - if r := recover(); r != nil { - panic(fmt.Errorf("%s for fieldIndex %d of field %s of container %#v", r, fieldIndex, name, container.Interface())) - } - }() + defer func() { + if r := recover(); r != nil { + panic(fmt.Errorf("%s\n\tfor field (index %d, name %s)", r, fieldIndex, name)) + } + }() - // Set the field. - container.Field(fieldIndex).Set(zeroValue) - } + // Set the field. + container.Field(fieldIndex).Set(zeroValue) + } - if selector(name, field) { property := prunerProperty{ name, fieldPruner, } p.properties = append(p.properties, property) - } else if field.Type.Kind() == reflect.Struct { - // Gather fields from the nested or embedded structure. - var subNamePrefix string - if field.Anonymous { - subNamePrefix = namePrefix - } else { - subNamePrefix = name + "." + } else { + switch fieldType.Kind() { + case reflect.Struct: + // Gather fields from the nested or embedded structure. + var subNamePrefix string + if field.Anonymous { + subNamePrefix = namePrefix + } else { + subNamePrefix = name + "." + } + p.gatherFields(fieldType, fieldGetter, subNamePrefix, selector) } - p.gatherFields(field.Type, fieldGetter, subNamePrefix, selector) } } } -// pruneProperties will prune (set to zero value) any properties in the supplied struct. +// pruneProperties will prune (set to zero value) any properties in the struct referenced by the +// supplied struct pointer. // // The struct must be of the same type as was originally passed to newPropertyPruner to create this // propertyPruner. func (p *propertyPruner) pruneProperties(propertiesStruct interface{}) { + + defer func() { + if r := recover(); r != nil { + panic(fmt.Errorf("%s\n\tof container %#v", r, propertiesStruct)) + } + }() + structValue := reflect.ValueOf(propertiesStruct) for _, property := range p.properties { property.prunerFunc(structValue) |