added structToSlice() so we can iterate structs
This commit is contained in:
12
utils.go
12
utils.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"samhofi.us/x/keybase/types/chat1"
|
||||
@ -58,3 +59,14 @@ func isRootCommand(s string, baseCommand string, botName string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// this converts structs to slices of (Name, Value) pairs
|
||||
func structToSlice(v interface{}) []reflectStruct {
|
||||
x := reflect.ValueOf(v)
|
||||
values := make([]reflectStruct, x.NumField())
|
||||
for i := 0; i < x.NumField(); i++ {
|
||||
values[i].Value = x.Field(i).Interface()
|
||||
values[i].Name = x.Type().Field(i).Name
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user