Skip to content

Commit 68780af

Browse files
authored
Merge pull request fluent#99 from dearonesama/fix-panic-struct
Fix panic when accessing unexported struct field
2 parents a32a5f4 + 0c79bf1 commit 68780af

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

fluent/fluent.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,18 @@ func (f *Fluent) PostWithTime(tag string, tm time.Time, message interface{}) err
212212
fields := msgtype.NumField()
213213
for i := 0; i < fields; i++ {
214214
field := msgtype.Field(i)
215+
value := msg.FieldByIndex(field.Index)
216+
// ignore unexported fields
217+
if !value.CanInterface() {
218+
continue
219+
}
215220
name := field.Name
216221
if n1 := field.Tag.Get("msg"); n1 != "" {
217222
name = n1
218223
} else if n2 := field.Tag.Get("codec"); n2 != "" {
219224
name = n2
220225
}
221-
kv[name] = msg.FieldByIndex(field.Index).Interface()
226+
kv[name] = value.Interface()
222227
}
223228
return f.EncodeAndPostData(tag, tm, kv)
224229
}

fluent/fluent_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ func TestPostWithTime(t *testing.T) {
436436

437437
_ = f.PostWithTime("tag_name", time.Unix(1482493046, 0), map[string]string{"foo": "bar"})
438438
_ = f.PostWithTime("tag_name", time.Unix(1482493050, 0), map[string]string{"fluentd": "is awesome"})
439+
_ = f.PostWithTime("tag_name", time.Unix(1634263200, 0),
440+
struct {Welcome string `msg:"welcome"`; cannot string}{"to use", "see me"})
439441
}()
440442

441443
conn := d.waitForNextDialing(true)
@@ -446,6 +448,9 @@ func TestPostWithTime(t *testing.T) {
446448
assertReceived(t,
447449
conn.waitForNextWrite(true, ""),
448450
"[\"acme.tag_name\",1482493050,{\"fluentd\":\"is awesome\"},{}]")
451+
assertReceived(t,
452+
conn.waitForNextWrite(true, ""),
453+
"[\"acme.tag_name\",1634263200,{\"welcome\":\"to use\"},{}]")
449454
})
450455
}
451456
}

0 commit comments

Comments
 (0)