pkg/terminal: use reflect.Value.IsValid to check a zero Value (#3450)
I searched the source code of Go, and found no usages of
"== (reflect.Value{})" and "!= (reflect.Value{})". I think
it's more idiomatic to use "IsValid" to check a zero Value.
This commit is contained in:
parent
7db57df266
commit
b5c9edccff
@ -134,13 +134,13 @@ func Print(out io.Writer, path string, reader io.Reader, startLine, endLine, arr
|
|||||||
|
|
||||||
tokposval := nval.FieldByName("TokPos")
|
tokposval := nval.FieldByName("TokPos")
|
||||||
tokval := nval.FieldByName("Tok")
|
tokval := nval.FieldByName("Tok")
|
||||||
if tokposval != (reflect.Value{}) && tokval != (reflect.Value{}) {
|
if tokposval.IsValid() && tokval.IsValid() {
|
||||||
emit(tokval.Interface().(token.Token), tokposval.Interface().(token.Pos), token.NoPos)
|
emit(tokval.Interface().(token.Token), tokposval.Interface().(token.Pos), token.NoPos)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, kwname := range []string{"Case", "Begin", "Defer", "For", "Func", "Go", "Interface", "Map", "Return", "Select", "Struct", "Switch"} {
|
for _, kwname := range []string{"Case", "Begin", "Defer", "For", "Func", "Go", "Interface", "Map", "Return", "Select", "Struct", "Switch"} {
|
||||||
kwposval := nval.FieldByName(kwname)
|
kwposval := nval.FieldByName(kwname)
|
||||||
if kwposval != (reflect.Value{}) {
|
if kwposval.IsValid() {
|
||||||
kwpos, ok := kwposval.Interface().(token.Pos)
|
kwpos, ok := kwposval.Interface().(token.Pos)
|
||||||
if ok && kwpos != token.NoPos {
|
if ok && kwpos != token.NoPos {
|
||||||
emit(token.ILLEGAL, kwpos, token.NoPos)
|
emit(token.ILLEGAL, kwpos, token.NoPos)
|
||||||
|
|||||||
@ -181,7 +181,7 @@ func (v structAsStarlarkValue) Attr(name string) (starlark.Value, error) {
|
|||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
r := v.v.FieldByName(name)
|
r := v.v.FieldByName(name)
|
||||||
if r == (reflect.Value{}) {
|
if !r.IsValid() {
|
||||||
return starlark.None, fmt.Errorf("no field named %q in %T", name, v.v.Interface())
|
return starlark.None, fmt.Errorf("no field named %q in %T", name, v.v.Interface())
|
||||||
}
|
}
|
||||||
return v.env.interfaceToStarlarkValue(r.Interface()), nil
|
return v.env.interfaceToStarlarkValue(r.Interface()), nil
|
||||||
@ -681,7 +681,7 @@ func unmarshalStarlarkValueIntl(val starlark.Value, dst reflect.Value, path stri
|
|||||||
}
|
}
|
||||||
fieldName := string(k.(starlark.String))
|
fieldName := string(k.(starlark.String))
|
||||||
dstfield := dst.FieldByName(fieldName)
|
dstfield := dst.FieldByName(fieldName)
|
||||||
if dstfield == (reflect.Value{}) {
|
if !dstfield.IsValid() {
|
||||||
return converr(fmt.Sprintf("unknown field %s", fieldName))
|
return converr(fmt.Sprintf("unknown field %s", fieldName))
|
||||||
}
|
}
|
||||||
valfield, _, _ := val.Get(starlark.String(fieldName))
|
valfield, _, _ := val.Get(starlark.String(fieldName))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user