From 9b0122bdcd5603746c2ec59d360d6dee1494ca7c Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 17 Jun 2026 16:51:34 +0200 Subject: columnar: fix handling of json:"-" fields --- columnar/columnar.go | 2 +- columnar/columnar_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'columnar') diff --git a/columnar/columnar.go b/columnar/columnar.go index bb549f8..5e339ab 100644 --- a/columnar/columnar.go +++ b/columnar/columnar.go @@ -59,7 +59,7 @@ type List[T any] []T func foreachRelevantField(t reflect.Type, action func(f reflect.StructField)) { for idx := range t.NumField() { f := t.Field(idx) - if f.PkgPath == "" { + if f.PkgPath == "" && f.Tag.Get("json") != "-" { action(f) } } diff --git a/columnar/columnar_test.go b/columnar/columnar_test.go index 17dc964..70e27c4 100644 --- a/columnar/columnar_test.go +++ b/columnar/columnar_test.go @@ -116,3 +116,23 @@ func TestJSONUnmarshalFromInconsistentLengths(t *testing.T) { err := json.Unmarshal([]byte(`{"Foo":[1,2],"Bar":[3,4,5]}`), PointerTo(columnar.List[Record]{})) AssertEqual(t, err.Error(), `cannot unmarshal from columns with inconsistent lengths [2 3]`) } + +func TestIgnoredFields(t *testing.T) { + type Record struct { + Foo int `json:"foo"` + Bonus int `json:"-"` + } + + // There used to be a bug where columnar got confused that the `Bonus` field + // ends up as a zero-length array after unmarshaling. + testSuccessfulJSONRoundtrip(t, + []Record{ + {Foo: 1, Bonus: 0}, + {Foo: 2, Bonus: 0}, + {Foo: 3, Bonus: 0}, + }, + jsonmatch.Object{ + "foo": jsonmatch.Array{1, 2, 3}, + }, + ) +} -- cgit v1.3.1