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í 6 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 * 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. 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 * Similarly to methods, exported struct fields are difficult to garble, as the
field, whose name has been garbled. 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) // log.Printf("%#v %T", node, obj)
switch x := obj.(type) { switch x := obj.(type) {
case *types.Var: 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() { if x.Embedded() {
obj = objOf(obj.Type()) obj = objOf(obj.Type())
} }

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

Loading…
Cancel
Save