Improve robutness of helper code

As per request, changed usage of pointer to int into plain int.
Use -1 value where nil use to be used.
This commit is contained in:
Jerome Brette
2019-06-18 17:29:55 -05:00
parent 9edecffcc8
commit 4556eb3a0c
3 changed files with 27 additions and 28 deletions

View File

@@ -92,7 +92,7 @@ func (fs *UnstructAdapter) selectSubtree(path string) (map[string]interface{}, [
idx := sections[sectionIdx].idx
fields := sections[sectionIdx].fields
if idx == nil {
if idx == -1 {
// This section has no index
return content, fields, true, nil
}
@@ -107,24 +107,24 @@ func (fs *UnstructAdapter) selectSubtree(path string) (map[string]interface{}, [
if !ok {
return content, fields, false, fmt.Errorf("%v is of the type %T, expected []interface{}", indexedField, indexedField)
}
if *idx >= len(s) {
return content, fields, false, fmt.Errorf("index %d is out of bounds", *idx)
if idx >= len(s) {
return content, fields, false, fmt.Errorf("index %d is out of bounds", idx)
}
if sectionIdx == lastSectionIdx-1 {
// This is the last section. Let's build a fake map
// to let the rest of the field extraction to work.
idxstring := fmt.Sprintf("[%v]", *idx)
newContent := map[string]interface{}{idxstring: s[*idx]}
idxstring := fmt.Sprintf("[%v]", idx)
newContent := map[string]interface{}{idxstring: s[idx]}
newFields := []string{idxstring}
return newContent, newFields, true, nil
}
newContent, ok := s[*idx].(map[string]interface{})
newContent, ok := s[idx].(map[string]interface{})
if !ok {
// Only map are supported here
return content, fields, false,
fmt.Errorf("%#v is expected to be of type map[string]interface{}", s[*idx])
fmt.Errorf("%#v is expected to be of type map[string]interface{}", s[idx])
}
content = newContent
}