Skip to content

Commit 6656062

Browse files
committed
feat(deps): replace yaml.v3 with goccy/go-yaml library
Replace yaml.v3 with goccy/go-yaml, as the former is now unmaintained. Also upgrade minimum Go version to 1.21.0 and update testify to v1.10.0. Add support for configuring encoder options in YAML renderer to provide more flexibility in YAML output formatting. Include new options for sequence indentation and automatic integer conversion. Implement support for both yaml.InterfaceMarshaler and yaml.BytesMarshaler interfaces with appropriate test cases. Rename mock implementation to clarify interface implementation.
1 parent 3d82cdc commit 6656062

8 files changed

Lines changed: 412 additions & 181 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ jobs:
5858
fail-fast: false
5959
matrix:
6060
go_version:
61-
- "1.20"
6261
- "1.21"
6362
- "1.22"
6463
- "1.23"

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module github.com/jimeh/go-render
22

3-
go 1.20
3+
go 1.21.0
44

55
require (
6-
github.com/stretchr/testify v1.9.0
7-
gopkg.in/yaml.v3 v3.0.1
6+
github.com/goccy/go-yaml v1.18.0
7+
github.com/stretchr/testify v1.10.0
88
)
99

1010
require (
1111
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
1314
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
4+
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
8+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
79
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
911
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

mise.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

render_test.go

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14+
// mockWriter is a mock implementation of io.Writer.
1415
type mockWriter struct {
1516
WriteErr error
1617
buf bytes.Buffer
@@ -30,6 +31,7 @@ func (mw *mockWriter) String() string {
3031
return mw.buf.String()
3132
}
3233

34+
// mockHandler is a mock implementation of Handler.
3335
type mockHandler struct {
3436
output string
3537
formats []string
@@ -55,6 +57,7 @@ func (mh *mockHandler) Formats() []string {
5557
return mh.formats
5658
}
5759

60+
// mockPrettyHandler is a mock implementation of PrettyHandler.
5861
type mockPrettyHandler struct {
5962
output string
6063
prettyOutput string
@@ -92,6 +95,7 @@ func (mph *mockPrettyHandler) Formats() []string {
9295
return mph.formats
9396
}
9497

98+
// mockFormatsHandler is a mock implementation of FormatsHandler.
9599
type mockFormatsHandler struct {
96100
output string
97101
formats []string
@@ -128,7 +132,6 @@ type renderFormatTestCase struct {
128132
wantCompact string
129133
wantErr string
130134
wantErrIs []error
131-
wantPanic string
132135
}
133136

134137
// "binary" format.
@@ -492,15 +495,38 @@ var yamlFormatTestCases = []renderFormatTestCase{
492495
want: "user:\n age: 30\n name: John Doe\n",
493496
},
494497
{
495-
name: "yaml format with yaml.Marshaler",
498+
name: "yaml format with sequences",
499+
value: map[string]any{
500+
"books": []string{
501+
"The Great Gatsby",
502+
"1984",
503+
},
504+
},
505+
want: "books:\n - The Great Gatsby\n - \"1984\"\n",
506+
},
507+
{
508+
name: "yaml format with yaml.InterfaceMarshaler",
496509
formats: []string{"yaml", "yml"},
497-
value: &mockYAMLMarshaler{val: map[string]int{"age": 30}},
510+
value: &mockYAMLInterfaceMarshaler{val: map[string]int{"age": 30}},
498511
want: "age: 30\n",
499512
},
500513
{
501-
name: "yaml format with error from yaml.Marshaler",
514+
name: "yaml format with error from yaml.InterfaceMarshaler",
502515
formats: []string{"yaml", "yml"},
503-
value: &mockYAMLMarshaler{err: errors.New("mock error")},
516+
value: &mockYAMLInterfaceMarshaler{err: errors.New("mock error")},
517+
wantErr: "render: failed: mock error",
518+
wantErrIs: []error{Err, ErrFailed},
519+
},
520+
{
521+
name: "yaml format with yaml.BytesMarshaler",
522+
formats: []string{"yaml", "yml"},
523+
value: &mockYAMLBytesMarshaler{val: []byte("age: 30\n")},
524+
want: "age: 30\n",
525+
},
526+
{
527+
name: "yaml format with error from yaml.BytesMarshaler",
528+
formats: []string{"yaml", "yml"},
529+
value: &mockYAMLBytesMarshaler{err: errors.New("mock error")},
504530
wantErr: "render: failed: mock error",
505531
wantErrIs: []error{Err, ErrFailed},
506532
},
@@ -516,7 +542,8 @@ var yamlFormatTestCases = []renderFormatTestCase{
516542
name: "yaml format with invalid type",
517543
formats: []string{"yaml", "yml"},
518544
value: make(chan int),
519-
wantPanic: "cannot marshal type: chan int",
545+
wantErr: "render: failed: unknown value type chan int",
546+
wantErrIs: []error{Err, ErrFailed},
520547
},
521548
}
522549

@@ -542,28 +569,15 @@ func TestRender(t *testing.T) {
542569
value = tt.valueFunc()
543570
}
544571

545-
var err error
546-
var panicRes any
547-
func() {
548-
defer func() {
549-
if r := recover(); r != nil {
550-
panicRes = r
551-
}
552-
}()
553-
err = Render(w, format, pretty, value)
554-
}()
555-
556-
got := w.String()
557572
want := tt.want
558573
if pretty && tt.wantPretty != "" {
559574
want = tt.wantPretty
560575
} else if tt.wantCompact != "" {
561576
want = tt.wantCompact
562577
}
563578

564-
if tt.wantPanic != "" {
565-
assert.Equal(t, tt.wantPanic, panicRes)
566-
}
579+
err := Render(w, format, pretty, value)
580+
got := w.String()
567581

568582
if tt.wantErr != "" {
569583
wantErr := strings.ReplaceAll(
@@ -575,8 +589,7 @@ func TestRender(t *testing.T) {
575589
assert.ErrorIs(t, err, e)
576590
}
577591

578-
if tt.wantPanic == "" &&
579-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
592+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
580593
assert.NoError(t, err)
581594
assert.Equal(t, want, got)
582595
}
@@ -602,28 +615,15 @@ func TestPretty(t *testing.T) {
602615
value = tt.valueFunc()
603616
}
604617

605-
var err error
606-
var panicRes any
607-
func() {
608-
defer func() {
609-
if r := recover(); r != nil {
610-
panicRes = r
611-
}
612-
}()
613-
err = Pretty(w, format, value)
614-
}()
615-
616-
got := w.String()
617-
var want string
618-
if tt.wantPretty == "" && tt.wantCompact == "" {
619-
want = tt.want
620-
} else {
618+
want := tt.want
619+
if tt.wantPretty != "" {
621620
want = tt.wantPretty
621+
} else if tt.wantCompact != "" {
622+
want = tt.wantCompact
622623
}
623624

624-
if tt.wantPanic != "" {
625-
assert.Equal(t, tt.wantPanic, panicRes)
626-
}
625+
err := Pretty(w, format, value)
626+
got := w.String()
627627

628628
if tt.wantErr != "" {
629629
wantErr := strings.ReplaceAll(
@@ -635,8 +635,7 @@ func TestPretty(t *testing.T) {
635635
assert.ErrorIs(t, err, e)
636636
}
637637

638-
if tt.wantPanic == "" &&
639-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
638+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
640639
assert.NoError(t, err)
641640
assert.Equal(t, want, got)
642641
}
@@ -661,28 +660,15 @@ func TestCompact(t *testing.T) {
661660
value = tt.valueFunc()
662661
}
663662

664-
var err error
665-
var panicRes any
666-
func() {
667-
defer func() {
668-
if r := recover(); r != nil {
669-
panicRes = r
670-
}
671-
}()
672-
err = Compact(w, format, value)
673-
}()
674-
675-
got := w.String()
676-
var want string
677-
if tt.wantPretty == "" && tt.wantCompact == "" {
678-
want = tt.want
679-
} else {
663+
want := tt.want
664+
if tt.wantPretty != "" {
665+
want = tt.wantPretty
666+
} else if tt.wantCompact != "" {
680667
want = tt.wantCompact
681668
}
682669

683-
if tt.wantPanic != "" {
684-
assert.Equal(t, tt.wantPanic, panicRes)
685-
}
670+
err := Compact(w, format, value)
671+
got := w.String()
686672

687673
if tt.wantErr != "" {
688674
wantErr := strings.ReplaceAll(
@@ -694,8 +680,7 @@ func TestCompact(t *testing.T) {
694680
assert.ErrorIs(t, err, e)
695681
}
696682

697-
if tt.wantPanic == "" &&
698-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
683+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
699684
assert.NoError(t, err)
700685
assert.Equal(t, want, got)
701686
}

renderer_test.go

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -493,29 +493,15 @@ func TestRenderer_RenderAllFormats(t *testing.T) {
493493
value = tt.valueFunc()
494494
}
495495

496-
var err error
497-
var panicRes any
498-
499-
func() {
500-
defer func() {
501-
if r := recover(); r != nil {
502-
panicRes = r
503-
}
504-
}()
505-
err = Base.Render(w, format, pretty, value)
506-
}()
507-
508-
got := w.String()
509496
want := tt.want
510497
if pretty && tt.wantPretty != "" {
511498
want = tt.wantPretty
512499
} else if tt.wantCompact != "" {
513500
want = tt.wantCompact
514501
}
515502

516-
if tt.wantPanic != "" {
517-
assert.Equal(t, tt.wantPanic, panicRes)
518-
}
503+
err := Base.Render(w, format, pretty, value)
504+
got := w.String()
519505

520506
if tt.wantErr != "" {
521507
wantErr := strings.ReplaceAll(
@@ -527,8 +513,7 @@ func TestRenderer_RenderAllFormats(t *testing.T) {
527513
assert.ErrorIs(t, err, e)
528514
}
529515

530-
if tt.wantPanic == "" &&
531-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
516+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
532517
assert.NoError(t, err)
533518
assert.Equal(t, want, got)
534519
}
@@ -556,29 +541,15 @@ func TestRenderer_CompactAllFormats(t *testing.T) {
556541
value = tt.valueFunc()
557542
}
558543

559-
var err error
560-
var panicRes any
561-
562-
func() {
563-
defer func() {
564-
if r := recover(); r != nil {
565-
panicRes = r
566-
}
567-
}()
568-
err = Base.Compact(w, format, value)
569-
}()
570-
571-
got := w.String()
572544
var want string
573545
if tt.wantPretty == "" && tt.wantCompact == "" {
574546
want = tt.want
575547
} else {
576548
want = tt.wantCompact
577549
}
578550

579-
if tt.wantPanic != "" {
580-
assert.Equal(t, tt.wantPanic, panicRes)
581-
}
551+
err := Base.Compact(w, format, value)
552+
got := w.String()
582553

583554
if tt.wantErr != "" {
584555
wantErr := strings.ReplaceAll(
@@ -590,8 +561,7 @@ func TestRenderer_CompactAllFormats(t *testing.T) {
590561
assert.ErrorIs(t, err, e)
591562
}
592563

593-
if tt.wantPanic == "" &&
594-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
564+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
595565
assert.NoError(t, err)
596566
assert.Equal(t, want, got)
597567
}
@@ -618,29 +588,15 @@ func TestRenderer_PrettyAllFormats(t *testing.T) {
618588
value = tt.valueFunc()
619589
}
620590

621-
var err error
622-
var panicRes any
623-
624-
func() {
625-
defer func() {
626-
if r := recover(); r != nil {
627-
panicRes = r
628-
}
629-
}()
630-
err = Base.Pretty(w, format, value)
631-
}()
632-
633-
got := w.String()
634591
var want string
635592
if tt.wantPretty == "" && tt.wantCompact == "" {
636593
want = tt.want
637594
} else {
638595
want = tt.wantPretty
639596
}
640597

641-
if tt.wantPanic != "" {
642-
assert.Equal(t, tt.wantPanic, panicRes)
643-
}
598+
err := Base.Pretty(w, format, value)
599+
got := w.String()
644600

645601
if tt.wantErr != "" {
646602
wantErr := strings.ReplaceAll(
@@ -652,8 +608,7 @@ func TestRenderer_PrettyAllFormats(t *testing.T) {
652608
assert.ErrorIs(t, err, e)
653609
}
654610

655-
if tt.wantPanic == "" &&
656-
tt.wantErr == "" && len(tt.wantErrIs) == 0 {
611+
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
657612
assert.NoError(t, err)
658613
assert.Equal(t, want, got)
659614
}

0 commit comments

Comments
 (0)