From 14900e97e20d41070e4d9a2918857e9b4e1c4032 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Sun, 27 Oct 2019 16:43:42 +0100 Subject: [PATCH] Add support for extending writers Go does not support inheritance, just composition. While composition with type embedding (i.e. forwarding method calls to the embedded type) can replace inheritance for most use cases this is not one of them. We really want to overwrite methods so that method calls from inside the base writer also use the custom methods ouf our extending writer - naive embedding does not work here as the this in this.WriteText refers to the embedded type rather than the outer extending type (see open recursion). A simple solution is to make a reference of the extending type available from the extended type and use that for nested method calls. We'll go with that one as it does not require huge code changes. Another solution would be to flatten the writing process and not use nested method calls - this is what blackfriday does. Assuming the current solution works I feel it's cleaner and keeps the ugliness of simulating inheritance with composition contained to a small portion of the code while blackfridays approach requires all write methods to be written in a flat style (i.e. not do nested calls to write by being called twice with entering / leaving). The current solution becomes ugly if we want to do multiple levels of extending but i don't expect that to be a valid use case - if it turns out to be one we can always adapt to it later. YAGNI. --- org/html_writer.go | 19 ++++++++++++++----- org/html_writer_test.go | 21 +++++++++++++++++++++ org/org_writer.go | 11 ++++++++++- org/org_writer_test.go | 21 +++++++++++++++++++++ org/writer.go | 3 +++ 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/org/html_writer.go b/org/html_writer.go index eb88b28..90a48c6 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -14,12 +14,14 @@ import ( // HTMLWriter exports an org document into a html document. type HTMLWriter struct { - strings.Builder - document *Document + ExtendingWriter Writer HighlightCodeBlock func(source, lang string) string - htmlEscape bool - log *log.Logger - footnotes *footnotes + + strings.Builder + document *Document + htmlEscape bool + log *log.Logger + footnotes *footnotes } type footnotes struct { @@ -79,6 +81,13 @@ func (w *HTMLWriter) nodesAsString(nodes ...Node) string { return tmp.String() } +func (w *HTMLWriter) WriterWithExtensions() Writer { + if w.ExtendingWriter != nil { + return w.ExtendingWriter + } + return w +} + func (w *HTMLWriter) Before(d *Document) { w.document = d w.log = d.Log diff --git a/org/html_writer_test.go b/org/html_writer_test.go index f1890d8..37f97a4 100644 --- a/org/html_writer_test.go +++ b/org/html_writer_test.go @@ -5,6 +5,16 @@ import ( "testing" ) +type ExtendedHTMLWriter struct { + *HTMLWriter + callCount int +} + +func (w *ExtendedHTMLWriter) WriteText(t Text) { + w.callCount++ + w.HTMLWriter.WriteText(t) +} + func TestHTMLWriter(t *testing.T) { for _, path := range orgTestFiles() { expected := fileString(path[:len(path)-len(".org")] + ".html") @@ -21,3 +31,14 @@ func TestHTMLWriter(t *testing.T) { } } } + +func TestExtendedHTMLWriter(t *testing.T) { + p := Paragraph{Children: []Node{Text{Content: "text"}, Text{Content: "more text"}}} + htmlWriter := NewHTMLWriter() + extendedWriter := &ExtendedHTMLWriter{htmlWriter, 0} + htmlWriter.ExtendingWriter = extendedWriter + WriteNodes(extendedWriter, p) + if extendedWriter.callCount != 2 { + t.Errorf("WriteText method of extending writer was not called: CallCount %d", extendedWriter.callCount) + } +} diff --git a/org/org_writer.go b/org/org_writer.go index 1abff16..d574cda 100644 --- a/org/org_writer.go +++ b/org/org_writer.go @@ -9,7 +9,9 @@ import ( // OrgWriter export an org document into pretty printed org document. type OrgWriter struct { - TagsColumn int + ExtendingWriter Writer + TagsColumn int + strings.Builder indent string } @@ -31,6 +33,13 @@ func NewOrgWriter() *OrgWriter { } } +func (w *OrgWriter) WriterWithExtensions() Writer { + if w.ExtendingWriter != nil { + return w.ExtendingWriter + } + return w +} + func (w *OrgWriter) Before(d *Document) {} func (w *OrgWriter) After(d *Document) {} diff --git a/org/org_writer_test.go b/org/org_writer_test.go index 6fe51af..9582152 100644 --- a/org/org_writer_test.go +++ b/org/org_writer_test.go @@ -10,6 +10,16 @@ import ( "github.com/pmezard/go-difflib/difflib" ) +type ExtendedOrgWriter struct { + *OrgWriter + callCount int +} + +func (w *ExtendedOrgWriter) WriteText(t Text) { + w.callCount++ + w.OrgWriter.WriteText(t) +} + func TestOrgWriter(t *testing.T) { for _, path := range orgTestFiles() { expected := fileString(path[:len(path)-len(".org")] + ".pretty_org") @@ -27,6 +37,17 @@ func TestOrgWriter(t *testing.T) { } } +func TestExtendedOrgWriter(t *testing.T) { + p := Paragraph{Children: []Node{Text{Content: "text"}, Text{Content: "more text"}}} + orgWriter := NewOrgWriter() + extendedWriter := &ExtendedOrgWriter{orgWriter, 0} + orgWriter.ExtendingWriter = extendedWriter + WriteNodes(extendedWriter, p) + if extendedWriter.callCount != 2 { + t.Errorf("WriteText method of extending writer was not called: CallCount %d", extendedWriter.callCount) + } +} + func orgTestFiles() []string { dir := "./testdata" files, err := ioutil.ReadDir(dir) diff --git a/org/writer.go b/org/writer.go index d3f622e..c4aebd6 100644 --- a/org/writer.go +++ b/org/writer.go @@ -8,6 +8,8 @@ type Writer interface { After(*Document) // After is called after all nodes have been passed to the writer. String() string // String is called at the very end to retrieve the final output. + WriterWithExtensions() Writer + WriteKeyword(Keyword) WriteInclude(Include) WriteComment(Comment) @@ -37,6 +39,7 @@ type Writer interface { } func WriteNodes(w Writer, nodes ...Node) { + w = w.WriterWithExtensions() for _, n := range nodes { switch n := n.(type) { case Keyword: