Skip to content

Commit

Permalink
add typing on document type, add text interface
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Amaral committed Sep 12, 2023
1 parent 8081d66 commit 0dd544c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
15 changes: 5 additions & 10 deletions pkg/v2/col/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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)
}
}
Expand Down
19 changes: 9 additions & 10 deletions pkg/v2/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/v2/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type image struct {
path string
_type string
_type v2.DocumentType
components []v2.Component
}

Expand All @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions pkg/v2/row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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)
}
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/v2/text/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package text

import (
"fmt"

Check failure on line 4 in pkg/v2/text/text.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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
}
22 changes: 22 additions & 0 deletions pkg/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
}

0 comments on commit 0dd544c

Please sign in to comment.