From 4ea38de81c4c18c93702593d7cb3a8664a5ad814 Mon Sep 17 00:00:00 2001 From: Christoph Borgolte Date: Mon, 14 Aug 2017 15:31:20 +0200 Subject: [PATCH] Keep up backward compatibility. --- composition/content_merge.go | 35 ++++++++++++++++++------- composition/html_content_parser_test.go | 5 +++- composition/interface_mocks_test.go | 14 ++++++++-- composition/interfaces.go | 13 ++++++--- composition/memory_content.go | 7 ++++- 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/composition/content_merge.go b/composition/content_merge.go index 8b0b5d3..79339ba 100644 --- a/composition/content_merge.go +++ b/composition/content_merge.go @@ -7,6 +7,8 @@ import ( "io" "strings" + "github.com/tarent/lib-compose/logging" + "golang.org/x/net/html" ) @@ -19,9 +21,10 @@ const ( // ContentMerge is a helper type for creation of a combined html document // out of multiple Content pages. type ContentMerge struct { - MetaJSON map[string]interface{} - Head []Fragment - BodyAttrs [][]html.Attribute + MetaJSON map[string]interface{} + Head []Fragment + BodyAttrs []Fragment + BodyAttrsArray [][]html.Attribute // Aggregator for the Body Fragments of the results. // Each fragment is insertes twice with full name and local name, @@ -98,8 +101,9 @@ func generateExecutionFunction(cntx *ContentMerge, w io.Writer) (executeFragment func collectBodyAttrs(bodyAttrs [][]html.Attribute) string { var result map[string]string = make(map[string]string) - for _, attrs := range bodyAttrs { - for _, attr := range attrs { + for i := range bodyAttrs { + for j := range bodyAttrs[i] { + attr := &bodyAttrs[i][j] val, exists := result[attr.Key] if strings.ToLower(attr.Key) == "class" { // aggregate all class attributes @@ -145,8 +149,7 @@ func (cntx *ContentMerge) GetHtml() ([]byte, error) { // open body tag body := bytes.NewBuffer(make([]byte, 0, DefaultBufferSize)) io.WriteString(body, "\n \n ") startFragmentName := "" @@ -199,7 +202,12 @@ func (cntx *ContentMerge) GetBodyFragmentByName(name string) (Fragment, bool) { func (cntx *ContentMerge) AddContent(c Content, priority int) { cntx.addHead(c.Head()) - cntx.addBodyAttributes(c.BodyAttributes()) + content2, ok := c.(Content2) + if ok { + cntx.addBodyAttributesArray(content2.BodyAttributesArray()) + } else { + cntx.addBodyAttributes(c.BodyAttributes()) + } cntx.addBody(c) cntx.addTail(c.Tail()) if priority > 0 { @@ -213,9 +221,16 @@ func (cntx *ContentMerge) addHead(f Fragment) { } } -func (cntx *ContentMerge) addBodyAttributes(a []html.Attribute) { +func (cntx *ContentMerge) addBodyAttributes(f Fragment) { + logging.Logger.Warnf("Called deprecated function addBodyAttributes(). This content will not be rendered.") + if f != nil { + cntx.BodyAttrs = append(cntx.BodyAttrs, f) + } +} + +func (cntx *ContentMerge) addBodyAttributesArray(a []html.Attribute) { if a != nil { - cntx.BodyAttrs = append(cntx.BodyAttrs, a) + cntx.BodyAttrsArray = append(cntx.BodyAttrsArray, a) } } diff --git a/composition/html_content_parser_test.go b/composition/html_content_parser_test.go index 26694b5..241684c 100644 --- a/composition/html_content_parser_test.go +++ b/composition/html_content_parser_test.go @@ -449,7 +449,10 @@ func Test_HtmlContentParser_parseBody(t *testing.T) { `§[> local]§`, c.Body()["content"]) eqFragment(t, "§[> example.com/tail]§", c.Tail()) - a.Equal(`some="attribute"`, joinAttrs(c.BodyAttributes())) + a.Equal(`some="attribute"`, joinAttrs(c.BodyAttributesArray())) + + // check deprecated BodyAttributes() method + eqFragment(t, `some="attribute"`, c.BodyAttributes()) a.Equal(5, len(c.Dependencies())) a.Equal(c.Dependencies()["example.com/xyz"], Params{"foo": "bar", "bazz": "buzz"}) diff --git a/composition/interface_mocks_test.go b/composition/interface_mocks_test.go index 69b06c4..e447496 100644 --- a/composition/interface_mocks_test.go +++ b/composition/interface_mocks_test.go @@ -125,9 +125,9 @@ func (_mr *_MockContentRecorder) Body() *gomock.Call { return _mr.mock.ctrl.RecordCall(_mr.mock, "Body") } -func (_m *MockContent) BodyAttributes() []html.Attribute { +func (_m *MockContent) BodyAttributes() Fragment { ret := _m.ctrl.Call(_m, "BodyAttributes") - ret0, _ := ret[0].([]html.Attribute) + ret0, _ := ret[0].(Fragment) return ret0 } @@ -135,6 +135,16 @@ func (_mr *_MockContentRecorder) BodyAttributes() *gomock.Call { return _mr.mock.ctrl.RecordCall(_mr.mock, "BodyAttributes") } +func (_m *MockContent) BodyAttributesArray() []html.Attribute { + ret := _m.ctrl.Call(_m, "BodyAttributesArray") + ret0, _ := ret[0].([]html.Attribute) + return ret0 +} + +func (_mr *_MockContentRecorder) BodyAttributesArray() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "BodyAttributesArray") +} + func (_m *MockContent) Dependencies() map[string]Params { ret := _m.ctrl.Call(_m, "Dependencies") ret0, _ := ret[0].(map[string]Params) diff --git a/composition/interfaces.go b/composition/interfaces.go index fac7262..a2341a6 100644 --- a/composition/interfaces.go +++ b/composition/interfaces.go @@ -52,7 +52,7 @@ type CacheStrategy interface { // Params is a value type for a parameter map type Params map[string]string -// Vontent is the abstration over includable data. +// Content is the abstraction over includable data. // Content may be parsed of it may contain a stream represented by a non nil Reader(), not both. type Content interface { @@ -82,8 +82,8 @@ type Content interface { // e.g. a script to load after rendering. Tail() Fragment - // The attributes for the body element - BodyAttributes() []html.Attribute + // Deprecated: Return the attributes for the body element as Fragment. + BodyAttributes() Fragment // Reader returns the stream with the content, of any. // If Reader() == nil, no stream is available an it contains parsed data, only. @@ -99,6 +99,13 @@ type Content interface { MemorySize() int } +type Content2 interface { + Content + + // Return the attributes for the body element as array. + BodyAttributesArray() []html.Attribute +} + type ContentMerger interface { // Add content to the merger AddContent(c Content, priority int) diff --git a/composition/memory_content.go b/composition/memory_content.go index 94b62eb..666404d 100644 --- a/composition/memory_content.go +++ b/composition/memory_content.go @@ -79,7 +79,12 @@ func (c *MemoryContent) Tail() Fragment { return c.tail } -func (c *MemoryContent) BodyAttributes() []html.Attribute { +// Deprecated: This method is deprecated +func (c *MemoryContent) BodyAttributes() Fragment { + return NewStringFragment(joinAttrs(c.bodyAttributes)) +} + +func (c *MemoryContent) BodyAttributesArray() []html.Attribute { return c.bodyAttributes }