From 64fe47161f06327328066634453811622f664583 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Sat, 13 Nov 2021 19:04:53 -0500 Subject: [PATCH] API: Encoder and Decoder options are chainable (#670) Fixes #583 --- marshaler.go | 12 ++++++++---- marshaler_test.go | 2 +- unmarshaler.go | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/marshaler.go b/marshaler.go index ac47344..648a665 100644 --- a/marshaler.go +++ b/marshaler.go @@ -54,8 +54,9 @@ func NewEncoder(w io.Writer) *Encoder { // inline tag: // // MyField `inline:"true"` -func (enc *Encoder) SetTablesInline(inline bool) { +func (enc *Encoder) SetTablesInline(inline bool) *Encoder { enc.tablesInline = inline + return enc } // SetArraysMultiline forces the encoder to emit all arrays with one element per @@ -64,20 +65,23 @@ func (enc *Encoder) SetTablesInline(inline bool) { // This behavior can be controlled on an individual struct field basis with the multiline tag: // // MyField `multiline:"true"` -func (enc *Encoder) SetArraysMultiline(multiline bool) { +func (enc *Encoder) SetArraysMultiline(multiline bool) *Encoder { enc.arraysMultiline = multiline + return enc } // SetIndentSymbol defines the string that should be used for indentation. The // provided string is repeated for each indentation level. Defaults to two // spaces. -func (enc *Encoder) SetIndentSymbol(s string) { +func (enc *Encoder) SetIndentSymbol(s string) *Encoder { enc.indentSymbol = s + return enc } // SetIndentTables forces the encoder to intent tables and array tables. -func (enc *Encoder) SetIndentTables(indent bool) { +func (enc *Encoder) SetIndentTables(indent bool) *Encoder { enc.indentTables = indent + return enc } // Encode writes a TOML representation of v to the stream. diff --git a/marshaler_test.go b/marshaler_test.go index 376858d..4e6c22a 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -551,7 +551,7 @@ K = 42`, type flagsSetters []struct { name string - f func(enc *toml.Encoder, flag bool) + f func(enc *toml.Encoder, flag bool) *toml.Encoder } var allFlags = flagsSetters{ diff --git a/unmarshaler.go b/unmarshaler.go index 11bac10..722e47d 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -48,8 +48,9 @@ func NewDecoder(r io.Reader) *Decoder { // that could not be set on the target value. In that case, the decoder returns // a StrictMissingError that can be used to retrieve the individual errors as // well as generate a human readable description of the missing fields. -func (d *Decoder) SetStrict(strict bool) { +func (d *Decoder) SetStrict(strict bool) *Decoder { d.strict = strict + return d } // Decode the whole content of r into v.