feat: kubesphere 4.0 (#6115)
* feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> * feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> --------- Signed-off-by: ci-bot <ci-bot@kubesphere.io> Co-authored-by: ks-ci-bot <ks-ci-bot@example.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
committed by
GitHub
parent
b5015ec7b9
commit
447a51f08b
139
vendor/go.starlark.net/starlark/unpack.go
generated
vendored
139
vendor/go.starlark.net/starlark/unpack.go
generated
vendored
@@ -8,37 +8,103 @@ import (
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"go.starlark.net/internal/spell"
|
||||
)
|
||||
|
||||
// An Unpacker defines custom argument unpacking behavior.
|
||||
// See UnpackArgs.
|
||||
type Unpacker interface {
|
||||
Unpack(v Value) error
|
||||
}
|
||||
|
||||
// UnpackArgs unpacks the positional and keyword arguments into the
|
||||
// supplied parameter variables. pairs is an alternating list of names
|
||||
// and pointers to variables.
|
||||
//
|
||||
// If the variable is a bool, int, string, *List, *Dict, Callable,
|
||||
// If the variable is a bool, integer, string, *List, *Dict, Callable,
|
||||
// Iterable, or user-defined implementation of Value,
|
||||
// UnpackArgs performs the appropriate type check.
|
||||
// An int uses the AsInt32 check.
|
||||
// If the parameter name ends with "?",
|
||||
// it and all following parameters are optional.
|
||||
// Predeclared Go integer types uses the AsInt check.
|
||||
//
|
||||
// If the parameter name ends with "?", it is optional.
|
||||
//
|
||||
// If the parameter name ends with "??", it is optional and treats the None value
|
||||
// as if the argument was absent.
|
||||
//
|
||||
// If a parameter is marked optional, then all following parameters are
|
||||
// implicitly optional where or not they are marked.
|
||||
//
|
||||
// If the variable implements Unpacker, its Unpack argument
|
||||
// is called with the argument value, allowing an application
|
||||
// to define its own argument validation and conversion.
|
||||
//
|
||||
// If the variable implements Value, UnpackArgs may call
|
||||
// its Type() method while constructing the error message.
|
||||
//
|
||||
// Beware: an optional *List, *Dict, Callable, Iterable, or Value variable that is
|
||||
// not assigned is not a valid Starlark Value, so the caller must
|
||||
// explicitly handle such cases by interpreting nil as None or some
|
||||
// computed default.
|
||||
// Examples:
|
||||
//
|
||||
// var (
|
||||
// a Value
|
||||
// b = MakeInt(42)
|
||||
// c Value = starlark.None
|
||||
// )
|
||||
//
|
||||
// // 1. mixed parameters, like def f(a, b=42, c=None).
|
||||
// err := UnpackArgs("f", args, kwargs, "a", &a, "b?", &b, "c?", &c)
|
||||
//
|
||||
// // 2. keyword parameters only, like def f(*, a, b, c=None).
|
||||
// if len(args) > 0 {
|
||||
// return fmt.Errorf("f: unexpected positional arguments")
|
||||
// }
|
||||
// err := UnpackArgs("f", args, kwargs, "a", &a, "b?", &b, "c?", &c)
|
||||
//
|
||||
// // 3. positional parameters only, like def f(a, b=42, c=None, /) in Python 3.8.
|
||||
// err := UnpackPositionalArgs("f", args, kwargs, 1, &a, &b, &c)
|
||||
//
|
||||
// More complex forms such as def f(a, b=42, *args, c, d=123, **kwargs)
|
||||
// require additional logic, but their need in built-ins is exceedingly rare.
|
||||
//
|
||||
// In the examples above, the declaration of b with type Int causes UnpackArgs
|
||||
// to require that b's argument value, if provided, is also an int.
|
||||
// To allow arguments of any type, while retaining the default value of 42,
|
||||
// declare b as a Value:
|
||||
//
|
||||
// var b Value = MakeInt(42)
|
||||
//
|
||||
// The zero value of a variable of type Value, such as 'a' in the
|
||||
// examples above, is not a valid Starlark value, so if the parameter is
|
||||
// optional, the caller must explicitly handle the default case by
|
||||
// interpreting nil as None or some computed default. The same is true
|
||||
// for the zero values of variables of type *List, *Dict, Callable, or
|
||||
// Iterable. For example:
|
||||
//
|
||||
// // def myfunc(d=None, e=[], f={})
|
||||
// var (
|
||||
// d Value
|
||||
// e *List
|
||||
// f *Dict
|
||||
// )
|
||||
// err := UnpackArgs("myfunc", args, kwargs, "d?", &d, "e?", &e, "f?", &f)
|
||||
// if d == nil { d = None; }
|
||||
// if e == nil { e = new(List); }
|
||||
// if f == nil { f = new(Dict); }
|
||||
//
|
||||
func UnpackArgs(fnname string, args Tuple, kwargs []Tuple, pairs ...interface{}) error {
|
||||
nparams := len(pairs) / 2
|
||||
var defined intset
|
||||
defined.init(nparams)
|
||||
|
||||
paramName := func(x interface{}) string { // (no free variables)
|
||||
name := x.(string)
|
||||
if name[len(name)-1] == '?' {
|
||||
paramName := func(x interface{}) (name string, skipNone bool) { // (no free variables)
|
||||
name = x.(string)
|
||||
if strings.HasSuffix(name, "??") {
|
||||
name = strings.TrimSuffix(name, "??")
|
||||
skipNone = true
|
||||
} else if name[len(name)-1] == '?' {
|
||||
name = name[:len(name)-1]
|
||||
}
|
||||
return name
|
||||
|
||||
return name, skipNone
|
||||
}
|
||||
|
||||
// positional arguments
|
||||
@@ -48,8 +114,13 @@ func UnpackArgs(fnname string, args Tuple, kwargs []Tuple, pairs ...interface{})
|
||||
}
|
||||
for i, arg := range args {
|
||||
defined.set(i)
|
||||
name, skipNone := paramName(pairs[2*i])
|
||||
if skipNone {
|
||||
if _, isNone := arg.(NoneType); isNone {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if err := unpackOneArg(arg, pairs[2*i+1]); err != nil {
|
||||
name := paramName(pairs[2*i])
|
||||
return fmt.Errorf("%s: for parameter %s: %s", fnname, name, err)
|
||||
}
|
||||
}
|
||||
@@ -59,12 +130,20 @@ kwloop:
|
||||
for _, item := range kwargs {
|
||||
name, arg := item[0].(String), item[1]
|
||||
for i := 0; i < nparams; i++ {
|
||||
if paramName(pairs[2*i]) == string(name) {
|
||||
pName, skipNone := paramName(pairs[2*i])
|
||||
if pName == string(name) {
|
||||
// found it
|
||||
if defined.set(i) {
|
||||
return fmt.Errorf("%s: got multiple values for keyword argument %s",
|
||||
fnname, name)
|
||||
}
|
||||
|
||||
if skipNone {
|
||||
if _, isNone := arg.(NoneType); isNone {
|
||||
continue kwloop
|
||||
}
|
||||
}
|
||||
|
||||
ptr := pairs[2*i+1]
|
||||
if err := unpackOneArg(arg, ptr); err != nil {
|
||||
return fmt.Errorf("%s: for parameter %s: %s", fnname, name, err)
|
||||
@@ -72,7 +151,16 @@ kwloop:
|
||||
continue kwloop
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s: unexpected keyword argument %s", fnname, name)
|
||||
err := fmt.Errorf("%s: unexpected keyword argument %s", fnname, name)
|
||||
names := make([]string, 0, nparams)
|
||||
for i := 0; i < nparams; i += 2 {
|
||||
param, _ := paramName(pairs[i])
|
||||
names = append(names, param)
|
||||
}
|
||||
if n := spell.Nearest(string(name), names); n != "" {
|
||||
err = fmt.Errorf("%s (did you mean %s?)", err.Error(), n)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Check that all non-optional parameters are defined.
|
||||
@@ -97,6 +185,8 @@ kwloop:
|
||||
// UnpackPositionalArgs reports an error if the number of arguments is
|
||||
// less than min or greater than len(vars), if kwargs is nonempty, or if
|
||||
// any conversion fails.
|
||||
//
|
||||
// See UnpackArgs for general comments.
|
||||
func UnpackPositionalArgs(fnname string, args Tuple, kwargs []Tuple, min int, vars ...interface{}) error {
|
||||
if len(kwargs) > 0 {
|
||||
return fmt.Errorf("%s: unexpected keyword arguments", fnname)
|
||||
@@ -127,6 +217,8 @@ func UnpackPositionalArgs(fnname string, args Tuple, kwargs []Tuple, min int, va
|
||||
func unpackOneArg(v Value, ptr interface{}) error {
|
||||
// On failure, don't clobber *ptr.
|
||||
switch ptr := ptr.(type) {
|
||||
case Unpacker:
|
||||
return ptr.Unpack(v)
|
||||
case *Value:
|
||||
*ptr = v
|
||||
case *string:
|
||||
@@ -141,12 +233,15 @@ func unpackOneArg(v Value, ptr interface{}) error {
|
||||
return fmt.Errorf("got %s, want bool", v.Type())
|
||||
}
|
||||
*ptr = bool(b)
|
||||
case *int:
|
||||
i, err := AsInt32(v)
|
||||
if err != nil {
|
||||
return err
|
||||
case *int, *int8, *int16, *int32, *int64,
|
||||
*uint, *uint8, *uint16, *uint32, *uint64, *uintptr:
|
||||
return AsInt(v, ptr)
|
||||
case *float64:
|
||||
f, ok := v.(Float)
|
||||
if !ok {
|
||||
return fmt.Errorf("got %s, want float", v.Type())
|
||||
}
|
||||
*ptr = i
|
||||
*ptr = float64(f)
|
||||
case **List:
|
||||
list, ok := v.(*List)
|
||||
if !ok {
|
||||
@@ -205,7 +300,9 @@ func unpackOneArg(v Value, ptr interface{}) error {
|
||||
// Attempt to call Value.Type method.
|
||||
func() {
|
||||
defer func() { recover() }()
|
||||
paramType = paramVar.MethodByName("Type").Call(nil)[0].String()
|
||||
if typer, _ := paramVar.Interface().(interface{ Type() string }); typer != nil {
|
||||
paramType = typer.Type()
|
||||
}
|
||||
}()
|
||||
return fmt.Errorf("got %s, want %s", v.Type(), paramType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user