insane reflection premature optimization
This commit is contained in:
parent
90b5def36a
commit
123eabca54
1 changed files with 20 additions and 5 deletions
25
tag/tag.go
25
tag/tag.go
|
|
@ -4,15 +4,19 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var opts struct {
|
||||
query string `name:"query" help:"tag query"`
|
||||
invert bool `name:"invert" help:"invert match"`
|
||||
Query string `name:"query" help:"tag query"`
|
||||
Invert bool `name:"invert" help:"invert match"`
|
||||
}
|
||||
|
||||
func stripDashes(flag string) string {
|
||||
return strings.TrimPrefix(flag, "--")
|
||||
func flagToField(flag string) string {
|
||||
s := strings.TrimPrefix(flag, "--")
|
||||
r := []rune(s)
|
||||
r[0] = unicode.ToUpper(r[0])
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func Tag(args []string) {
|
||||
|
|
@ -22,7 +26,7 @@ func Tag(args []string) {
|
|||
field := reflect.StructField{}
|
||||
found := false
|
||||
for _, a := range args {
|
||||
field, found = t.FieldByName(stripDashes(a))
|
||||
field, found = t.FieldByName(flagToField(a))
|
||||
if !found {
|
||||
fmt.Printf("%s not found in opts\n", a)
|
||||
return
|
||||
|
|
@ -33,6 +37,17 @@ func Tag(args []string) {
|
|||
fmt.Printf("field name: %#v\n", tag.Get("name"))
|
||||
fmt.Printf("field help: %#v\n", tag.Get("help"))
|
||||
|
||||
v := reflect.ValueOf(opts)
|
||||
vfield := reflect.Value{}
|
||||
for _, a := range args {
|
||||
vfield = v.FieldByName(flagToField(a))
|
||||
if vfield.IsValid() && vfield.CanSet() && vfield.Kind() == reflect.Bool {
|
||||
vfield.SetBool(true)
|
||||
fmt.Printf("vfield: %#v\n", vfield)
|
||||
}
|
||||
}
|
||||
fmt.Printf("vfield: %#v\n", vfield)
|
||||
fmt.Printf("opts: %#v\n", opts)
|
||||
// for i := 0; i < t.NumField(); i++ {
|
||||
// field := t.Field(i)
|
||||
// if alias, ok := field.Tag.Lookup("name"); ok {
|
||||
|
|
|
|||
Loading…
Reference in a new issue