diff --git a/pkg/v2/col/col.go b/pkg/v2/col/col.go index e77c3e3f..4f47723f 100644 --- a/pkg/v2/col/col.go +++ b/pkg/v2/col/col.go @@ -7,19 +7,14 @@ import ( type col struct { size int - _type string - accept map[string]bool + _type v2.DocumentType components []v2.Component } func New(size int) *col { - accept := make(map[string]bool) - accept[v2.Image] = true - return &col{ - _type: v2.Col, - accept: accept, - size: size, + _type: v2.Col, + size: size, } } @@ -31,12 +26,12 @@ func (d *col) Render() { } func (d *col) GetType() string { - return d._type + return d._type.String() } func (d *col) Add(components ...v2.Component) { for _, component := range components { - if _, ok := d.accept[component.GetType()]; ok { + if d._type.Accept(component.GetType()) { d.components = append(d.components, component) } } diff --git a/pkg/v2/document.go b/pkg/v2/document.go index 7a758e6a..a053865c 100644 --- a/pkg/v2/document.go +++ b/pkg/v2/document.go @@ -4,19 +4,14 @@ import "fmt" type document struct { value string - _type string - accept map[string]bool + _type DocumentType components []Component } func NewDocument(value string) *document { - accept := make(map[string]bool) - accept[Row] = true - return &document{ - _type: Document, - accept: accept, - value: value, + _type: Document, + value: value, } } @@ -27,13 +22,17 @@ func (d *document) Render() { } } +func (d *document) IsDrawable() bool { + return false +} + func (d *document) GetType() string { - return d._type + return d._type.String() } func (d *document) Add(components ...Component) { for _, component := range components { - if _, ok := d.accept[component.GetType()]; ok { + if d._type.Accept(component.GetType()) { d.components = append(d.components, component) } } diff --git a/pkg/v2/image/image.go b/pkg/v2/image/image.go index 8f376a60..c21e6158 100644 --- a/pkg/v2/image/image.go +++ b/pkg/v2/image/image.go @@ -7,7 +7,7 @@ import ( type image struct { path string - _type string + _type v2.DocumentType components []v2.Component } @@ -26,7 +26,7 @@ func (d *image) Render() { } func (d *image) GetType() string { - return d._type + return d._type.String() } func (d *image) Add(_ ...v2.Component) { diff --git a/pkg/v2/row/row.go b/pkg/v2/row/row.go index 48a91a72..355013fe 100644 --- a/pkg/v2/row/row.go +++ b/pkg/v2/row/row.go @@ -7,18 +7,13 @@ import ( type row struct { height int - _type string - accept map[string]bool + _type v2.DocumentType components []v2.Component } func New(height int) *row { - accept := make(map[string]bool) - accept[v2.Col] = true - return &row{ _type: v2.Row, - accept: accept, height: height, } } @@ -31,12 +26,12 @@ func (d *row) Render() { } func (d *row) GetType() string { - return d._type + return d._type.String() } func (d *row) Add(components ...v2.Component) { for _, component := range components { - if _, ok := d.accept[component.GetType()]; ok { + if d._type.Accept(component.GetType()) { d.components = append(d.components, component) } } diff --git a/pkg/v2/text/text.go b/pkg/v2/text/text.go new file mode 100644 index 00000000..44fd8587 --- /dev/null +++ b/pkg/v2/text/text.go @@ -0,0 +1,34 @@ +package text + +import ( + "fmt" + v2 "github.com/johnfercher/maroto/pkg/v2" +) + +type text struct { + value string + _type v2.DocumentType + components []v2.Component +} + +func New(value string) *text { + return &text{ + _type: v2.Text, + value: value, + } +} + +func (d *text) Render() { + fmt.Println(d.value) + for _, component := range d.components { + component.Render() + } +} + +func (d *text) GetType() string { + return d._type.String() +} + +func (d *text) Add(_ ...v2.Component) { + return +} diff --git a/pkg/v2/types.go b/pkg/v2/types.go index 64aaad71..1ddc2d71 100644 --- a/pkg/v2/types.go +++ b/pkg/v2/types.go @@ -3,6 +3,28 @@ package v2 const ( Document = "document" Row = "row" + Page = "page" Col = "col" Image = "image" + Text = "text" ) + +type DocumentType string + +func (t DocumentType) String() string { + return string(t) +} + +func (t DocumentType) Accept(dt string) bool { + _, ok := buildAcceptedMap()[dt] + return ok +} + +func buildAcceptedMap() map[string][]string { + return map[string][]string{ + Document: {Row}, + Page: {Row}, + Row: {Col}, + Col: {Row, Image, Text}, + } +}