Skip to content

Commit

Permalink
Skip unexported fields in encode
Browse files Browse the repository at this point in the history
  • Loading branch information
LK4D4 authored and BurntSushi committed May 12, 2014
1 parent 79bdfaa commit 72196a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value) error {
addFields = func(rt reflect.Type, rv reflect.Value, start []int) error {
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
// skip unexporded fields
if f.PkgPath != "" {
continue
}
frv := rv.Field(i)
if f.Anonymous {
t := frv.Type()
Expand Down
7 changes: 7 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func TestEncode(t *testing.T) {
input: struct{ String string }{"foo"},
wantOutput: `String = "foo"`,
},
"string field and unxeported field": {
input: struct {
String string
unexported int
}{"foo", 0},
wantOutput: `String = "foo"`,
},
"array fields": {
input: struct {
IntArray0 [0]int
Expand Down

0 comments on commit 72196a7

Please sign in to comment.