elvish/pkg/eval/options.go

64 lines
1.6 KiB
Go
Raw Normal View History

package eval
import (
"fmt"
"reflect"
"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/parse"
"src.elv.sh/pkg/strutil"
)
type BadOption struct {
OptName string
}
func (e BadOption) Error() string {
return "unknown option: " + parse.Quote(e.OptName)
}
2019-04-19 05:57:14 +08:00
// RawOptions is the type of an argument a Go-native function can take to
// declare that it wants to parse options itself. See the doc of NewGoFn for
2019-04-19 05:57:14 +08:00
// details.
type RawOptions map[string]interface{}
// Takes a raw option map and a pointer to a struct, and populate the struct
// with options. A field named FieldName corresponds to the option named
// field-name, unless the field has a explicit "name" tag. Fields typed
// ParsedOptions are ignored.
func scanOptions(rawOpts RawOptions, ptr interface{}) error {
ptrValue := reflect.ValueOf(ptr)
if ptrValue.Kind() != reflect.Ptr || ptrValue.Elem().Kind() != reflect.Struct {
return fmt.Errorf(
"internal bug: need struct ptr to scan options, got %T", ptr)
}
// fieldIdxForOpt maps option name to the index of field in `struc`.
fieldIdxForOpt := make(map[string]int)
struc := ptrValue.Elem()
for i := 0; i < struc.Type().NumField(); i++ {
if !struc.Field(i).CanSet() {
continue // ignore unexported fields
}
f := struc.Type().Field(i)
optName := f.Tag.Get("name")
if optName == "" {
optName = strutil.CamelToDashed(f.Name)
}
fieldIdxForOpt[optName] = i
}
for k, v := range rawOpts {
fieldIdx, ok := fieldIdxForOpt[k]
if !ok {
return BadOption{k}
}
err := vals.ScanToGo(v, struc.Field(fieldIdx).Addr().Interface())
if err != nil {
return err
}
}
return nil
}