Commit e9ee0bf6 authored by Robert Griesemer's avatar Robert Griesemer

cmd/gofmt: minor internal cleanups

Reflect changes of reflect API.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/69240044
parent a36b5b99
...@@ -48,7 +48,7 @@ func parseExpr(s, what string) ast.Expr { ...@@ -48,7 +48,7 @@ func parseExpr(s, what string) ast.Expr {
/* /*
func dump(msg string, val reflect.Value) { func dump(msg string, val reflect.Value) {
fmt.Printf("%s:\n", msg) fmt.Printf("%s:\n", msg)
ast.Print(fset, val.Interface()) ast.Print(fileSet, val.Interface())
fmt.Println() fmt.Println()
} }
*/ */
...@@ -59,8 +59,9 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File { ...@@ -59,8 +59,9 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
m := make(map[string]reflect.Value) m := make(map[string]reflect.Value)
pat := reflect.ValueOf(pattern) pat := reflect.ValueOf(pattern)
repl := reflect.ValueOf(replace) repl := reflect.ValueOf(replace)
var f func(val reflect.Value) reflect.Value // f is recursive
f = func(val reflect.Value) reflect.Value { var rewriteVal func(val reflect.Value) reflect.Value
rewriteVal = func(val reflect.Value) reflect.Value {
// don't bother if val is invalid to start with // don't bother if val is invalid to start with
if !val.IsValid() { if !val.IsValid() {
return reflect.Value{} return reflect.Value{}
...@@ -68,22 +69,22 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File { ...@@ -68,22 +69,22 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
for k := range m { for k := range m {
delete(m, k) delete(m, k)
} }
val = apply(f, val) val = apply(rewriteVal, val)
if match(m, pat, val) { if match(m, pat, val) {
val = subst(m, repl, reflect.ValueOf(val.Interface().(ast.Node).Pos())) val = subst(m, repl, reflect.ValueOf(val.Interface().(ast.Node).Pos()))
} }
return val return val
} }
r := apply(f, reflect.ValueOf(p)).Interface().(*ast.File)
r := apply(rewriteVal, reflect.ValueOf(p)).Interface().(*ast.File)
r.Comments = cmap.Filter(r).Comments() // recreate comments list r.Comments = cmap.Filter(r).Comments() // recreate comments list
return r return r
} }
// setValue is a wrapper for x.SetValue(y); it protects // set is a wrapper for x.Set(y); it protects the caller from panics if x cannot be changed to y.
// the caller from panics if x cannot be changed to y. func set(x, y reflect.Value) {
func setValue(x, y reflect.Value) { // don't bother if x cannot be set or y is invalid
// don't bother if y is invalid to start with if !x.CanSet() || !y.IsValid() {
if !y.IsValid() {
return return
} }
defer func() { defer func() {
...@@ -134,16 +135,16 @@ func apply(f func(reflect.Value) reflect.Value, val reflect.Value) reflect.Value ...@@ -134,16 +135,16 @@ func apply(f func(reflect.Value) reflect.Value, val reflect.Value) reflect.Value
case reflect.Slice: case reflect.Slice:
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
e := v.Index(i) e := v.Index(i)
setValue(e, f(e)) set(e, f(e))
} }
case reflect.Struct: case reflect.Struct:
for i := 0; i < v.NumField(); i++ { for i := 0; i < v.NumField(); i++ {
e := v.Field(i) e := v.Field(i)
setValue(e, f(e)) set(e, f(e))
} }
case reflect.Interface: case reflect.Interface:
e := v.Elem() e := v.Elem()
setValue(v, f(e)) set(v, f(e))
} }
return val return val
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment