Skip to content

Commit

Permalink
add CollectFieldNames
Browse files Browse the repository at this point in the history
  • Loading branch information
sjqzhang committed Feb 10, 2023
1 parent 74d64be commit eb08e07
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/url"
"os"
"reflect"
"regexp"
"runtime"
"runtime/debug"
Expand Down Expand Up @@ -98,6 +99,28 @@ func Parse(dsn string) (*DSN, error) {
return &d, nil
}


func CollectFieldNames(t reflect.Type,m map[string]struct{},prefix string) {
// Return if not struct or pointer to struct.
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return
}

// Iterate through fields collecting names in map.
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
m[prefix+ sf.Name] = struct{}{}

// Recurse into anonymous fields.
if sf.Anonymous {
CollectFieldNames(sf.Type, m,prefix+sf.Name+",")
}
}
}

// Parses query and returns dsn values
func ParseQuery(query string) (*DSNValues, error) {
parsed, err := url.ParseQuery(query)
Expand Down

0 comments on commit eb08e07

Please sign in to comment.