From 1d340b951a360e1f554a0d65a8a0c8f6d51bbafa Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Mon, 20 Nov 2023 18:13:06 +0100 Subject: [PATCH] Remove trailing comma from lists (#46) --- field.go | 18 ++++++++++++++--- field_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/field.go b/field.go index 9773baf..eedd8e8 100644 --- a/field.go +++ b/field.go @@ -187,16 +187,22 @@ func (f Field) ValueString(w io.Writer, shouldQuote func(s string) bool) error { break arr } } - if _, err = w.Write(Comma); err != nil { - break arr + if i != a.Len()-1 { + if _, err = w.Write(Comma); err != nil { + break arr + } } } case MapType: a := reflect.ValueOf(f.Interface) iter := a.MapRange() + // Already advance to first element + if !iter.Next() { + return nil + } it: - for iter.Next() { + for { if _, err = io.WriteString(w, iter.Key().String()); err != nil { break it } @@ -219,9 +225,15 @@ func (f Field) ValueString(w io.Writer, shouldQuote func(s string) bool) error { break it } } + + if !iter.Next() { + break it + } + if _, err = w.Write(Comma); err != nil { break it } + } case UnknownType: diff --git a/field_test.go b/field_test.go index 29ec474..8ef7f6c 100644 --- a/field_test.go +++ b/field_test.go @@ -82,6 +82,61 @@ func TestFieldForAny(t *testing.T) { } } +func TestField_Array(t *testing.T) { + tests := []struct { + name string + field Field + wantW string + wantErr bool + }{ + {name: "nil", field: Array[[]any]("array", nil), wantW: "", wantErr: false}, + {name: "empty", field: Array("array", []string{}), wantW: "", wantErr: false}, + {name: "one elements", field: Array("array", []string{"foo"}), wantW: "foo", wantErr: false}, + {name: "two elements", field: Array("array", []string{"foo", "bar"}), wantW: "foo,bar", wantErr: false}, + {name: "three elements", field: Array("array", []string{"foo", "bar", "xyz"}), wantW: "foo,bar,xyz", wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := &bytes.Buffer{} + if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr { + t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotW := w.String(); gotW != tt.wantW { + t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW) + } + }) + } +} + +func TestField_Map(t *testing.T) { + tests := []struct { + name string + field Field + wantW string + wantErr bool + }{ + {name: "nil", field: Map[map[string]any]("map", nil), wantW: "", wantErr: false}, + + {name: "empty", field: Map("map", map[string]any{}), wantW: "", wantErr: false}, + {name: "one elements", field: Map("map", map[string]int{"foo": 0}), wantW: "foo=0", wantErr: false}, + {name: "two elements", field: Map("map", map[string]int{"foo": 0, "bar": 1}), wantW: "foo=0,bar=1", wantErr: false}, + {name: "three elements", field: Map("map", map[string]int{"foo": 0, "bar": 1, "xyz": 2}), wantW: "foo=0,bar=1,xyz=2", wantErr: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := &bytes.Buffer{} + if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr { + t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotW := w.String(); gotW != tt.wantW { + t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW) + } + }) + } +} + type testStringer struct { s string }