]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: simplify unwrapping reflect.Interface value
authorJoe Taber <infogulch@gmail.com>
Mon, 18 Mar 2024 05:52:30 +0000 (05:52 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 18 Mar 2024 21:31:46 +0000 (21:31 +0000)
When text/template is evaluating a pipeline command and encounters an
`interface{}`, it "digs down one level to the thing inside". Currently it
does this with `value = reflect.ValueOf(value.Interface())`, which is
unnecessary since it could just use `value = value.Elem()`. This commit
changes it to use the latter.

Why it was written that way is mysterious because the proposed change
appears to be strictly better, but given the blame date (13 years ago)
it may have been written while reflect was still in development before
`Elem()` was added.

Change-Id: I6c4f6283e78de07732c4120ce11f26f113fa46e4
GitHub-Last-Rev: bdfc6973ab227f951f244fda4d803da55fb49e71
GitHub-Pull-Request: golang/go#66373
Reviewed-on: https://go-review.googlesource.com/c/go/+/572355
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Rob Pike <r@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
src/text/template/exec.go

index 20d8f98f28c114f07572bdeff95a8972f55e3d2e..4c899b1c79d045ea3fe0876ce1e88431538fe781 100644 (file)
@@ -479,7 +479,7 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref
                value = s.evalCommand(dot, cmd, value) // previous value is this one's final arg.
                // If the object has type interface{}, dig down one level to the thing inside.
                if value.Kind() == reflect.Interface && value.Type().NumMethod() == 0 {
-                       value = reflect.ValueOf(value.Interface()) // lovely!
+                       value = value.Elem()
                }
        }
        for _, variable := range pipe.Decl {