elvish/pkg/eval/options.go
2021-12-13 01:23:14 +00:00

66 lines
1.7 KiB
Go

package eval
import (
"fmt"
"reflect"
"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/parse"
"src.elv.sh/pkg/strutil"
)
// UnknownOption is thrown by a native function when called with an unknown option.
type UnknownOption struct {
OptName string
}
// Error implements the error interface.
func (e UnknownOption) Error() string {
return "unknown option: " + parse.Quote(e.OptName)
}
// 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
// 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 UnknownOption{k}
}
err := vals.ScanToGo(v, struc.Field(fieldIdx).Addr().Interface())
if err != nil {
return err
}
}
return nil
}