From 16d52188f62342023b9b690289a2b3abf9fbaee5 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 1 Sep 2023 21:13:13 +0000 Subject: [PATCH] basic: implement Section.String() and Field.String() to ease development Signed-off-by: Alejandro Mery --- basic/write.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/basic/write.go b/basic/write.go index d4a74e9..d199d7f 100644 --- a/basic/write.go +++ b/basic/write.go @@ -45,6 +45,14 @@ func writeFieldsTo(w io.Writer, fields []Field, nl string) (int64, error) { return int64(written), nil } +// String generates a string output for "%s" +func (field Field) String() string { + var buf bytes.Buffer + + _, _ = writeFieldsTo(&buf, []Field{field}, WriteNewLine) + return buf.String() +} + func writeSectionToBuffer(w *bytes.Buffer, sec *Section, nl string) int { var written, n int @@ -74,6 +82,14 @@ func writeSectionToBuffer(w *bytes.Buffer, sec *Section, nl string) int { return written + int(n64) } +// String generates a string output for "%s" +func (sec *Section) String() string { + var buf bytes.Buffer + + _ = writeSectionToBuffer(&buf, sec, WriteNewLine) + return buf.String() +} + // WriteTo writes a INI representation of the document // onto the provided writer. func (doc *Document) WriteTo(w io.Writer) (int64, error) { @@ -81,7 +97,7 @@ func (doc *Document) WriteTo(w io.Writer) (int64, error) { return buf.WriteTo(w) } -// GoString generates a string output for "%s" +// String generates a string output for "%s" func (doc *Document) String() string { buf := doc.AsBuffer(WriteNewLine) return buf.String() -- 2.17.1