]> Cypherpunks.ru repositories - gostls13.git/commit
reflect: add VisibleFields function
authorRoger Peppe <rogpeppe@gmail.com>
Fri, 1 Jan 2021 12:14:34 +0000 (12:14 +0000)
committerroger peppe <rogpeppe@gmail.com>
Fri, 5 Mar 2021 23:47:39 +0000 (23:47 +0000)
commit009bfeae866f45549865e554420a05c10e9578ca
tree2a9967067ecd71ac605a21d01ec418da83808d51
parentf901ea701ddac5a4d600d49007e54caa32b4c9b5
reflect: add VisibleFields function

When writing code that reflects over a struct type, it's a common requirement to know the full set of struct fields, including fields available due to embedding of anonymous members while excluding fields that are erased because they're at the same level as another field with the same name.

The logic to do this is not that complex, but it's a little subtle and easy to get wrong.

This CL adds a new `VisibleFields` function to the reflect package that returns the full set of effective fields that apply in a given struct type.

Performance isn't a prime consideration, as it's common to cache results by type.

Fixes #42782

Change-Id: I7f1af76cecff9b8a2490f17eec058826e396f660
Reviewed-on: https://go-review.googlesource.com/c/go/+/281233
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Daniel Martí <mvdan@mvdan.cc>
src/reflect/visiblefields.go [new file with mode: 0644]
src/reflect/visiblefields_test.go [new file with mode: 0644]