do some error checking before asserting orderedmap

This commit is contained in:
2023-04-18 09:20:36 +10:00
parent c500216105
commit 4bffb9cbfc

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"reflect"
"time" "time"
"unicode/utf8" "unicode/utf8"
@@ -102,11 +103,18 @@ func main() {
if !ok { if !ok {
fmt.Printf("Missing key for multitype array") fmt.Printf("Missing key for multitype array")
} }
// Get an interface that we can work with to access the sub elements // Get an interface that we can work with to access the sub elements
vslice := vislice.([]interface{}) vslice := vislice.([]interface{})
// Check that the first element is what we expected
if _, ok := vslice[0].(orderedmap.OrderedMap); !ok {
error := fmt.Sprintf("Type of first vslice element is not an ordered map. It appears to be '%v'\n", reflect.TypeOf(vslice[0]))
panic(error)
}
// Get the keys for the first element so we know what the column names will be // Get the keys for the first element so we know what the column names will be
columnMap := vslice[0].(orderedmap.OrderedMap) columnMap := vslice[0].(orderedmap.OrderedMap)
fmt.Printf("First vslice element is an ordered map")
columnNames := columnMap.Keys() columnNames := columnMap.Keys()
// Run code to add column names to the first row of the workbook // Run code to add column names to the first row of the workbook