don't garble exported struct fields

They might reasonably affect the behavior of the code, such as when
encoding/json is used without tags.
pull/22/head
Daniel Martí 5 years ago
parent b10cce34f8
commit 1ce5310440

@ -52,5 +52,5 @@ binary doesn't include paths from the current filesystem.
* Deciding what method names to garble is always going to be difficult, due to
interfaces that could be implemented up or down the package import tree.
* Some uses of the `reflect` package may break, such as accessing a struct's
field, whose name has been garbled.
* Similarly to methods, exported struct fields are difficult to garble, as the
names might be relevant for reflection work like `encoding/json`

@ -353,6 +353,11 @@ func transformGo(node ast.Node, info *types.Info) ast.Node {
// log.Printf("%#v %T", node, obj)
switch x := obj.(type) {
case *types.Var:
if x.IsField() && x.Exported() {
// might be used for reflection, e.g.
// encoding/json without struct tags
return true
}
if x.Embedded() {
obj = objOf(obj.Type())
}

@ -5,15 +5,22 @@ cmp stderr main.stderr
-- main.go --
package main
import "go/ast"
import (
"encoding/json"
"go/ast"
)
var V interface{}
type T struct{
type T struct {
ast.Node
*ast.Ident
}
type EncodingT struct {
Foo int
}
func main() {
switch V := V.(type) {
case int:
@ -21,6 +28,10 @@ func main() {
case nil:
println("nil case")
}
enc, _ := json.Marshal(EncodingT{Foo: 3})
println(string(enc))
}
-- main.stderr --
nil case
{"Foo":3}

Loading…
Cancel
Save