Browse Source

added structToSlice() so we can iterate structs

master
David Haukeness 5 years ago
parent
commit
95dc451ab3
No known key found for this signature in database
GPG Key ID: 54F2372DDB7F9462
  1. 12
      utils.go

12
utils.go

@ -3,6 +3,7 @@ package main @@ -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 { @@ -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
}

Loading…
Cancel
Save