Skip to content

Commit

Permalink
refactor(files): when in doubt, use a pointer receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Nov 15, 2024
1 parent 86123de commit a7cd3da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions pkg/files/documents/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Pdf struct {
}

// NewPdf returns a pointer to Pdf.
func NewPdf(filename string) Pdf {
func NewPdf(filename string) *Pdf {
p := Pdf{
filename: filename,
compatibleFormats: map[string][]string{
Expand Down Expand Up @@ -75,25 +75,25 @@ func NewPdf(filename string) Pdf {
},
}

return p
return &p
}

// SupportedFormats returns a map witht the compatible formats that Pdf is
// compatible to be converted to.
func (p Pdf) SupportedFormats() map[string][]string {
func (p *Pdf) SupportedFormats() map[string][]string {
return p.compatibleFormats
}

// SupportedMIMETypes returns a map witht the compatible MIME types that Pdf is
// compatible to be converted to.
func (p Pdf) SupportedMIMETypes() map[string][]string {
func (p *Pdf) SupportedMIMETypes() map[string][]string {
return p.compatibleMIMETypes
}

// ConvertTo converts the current PDF file to another given format.
// This method receives the file type, the sub-type and the file as an slice of bytes.
// Returns the converted file as an slice of bytes, if something wrong happens, an error is returned.
func (p Pdf) ConvertTo(fileType, subType string, file io.Reader) (io.Reader, error) {
func (p *Pdf) ConvertTo(fileType, subType string, file io.Reader) (io.Reader, error) {
// These are guard clauses that check if the target file type is valid.
compatibleFormats, ok := p.SupportedFormats()[fileType]
if !ok {
Expand Down Expand Up @@ -411,6 +411,6 @@ func (p Pdf) ConvertTo(fileType, subType string, file io.Reader) (io.Reader, err
}

// DocumentType returns the type of ducument of Pdf.
func (p Pdf) DocumentType() string {
func (p *Pdf) DocumentType() string {
return PDF
}
12 changes: 6 additions & 6 deletions pkg/files/ebooks/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Epub struct {
compatibleMIMETypes map[string][]string
}

func NewEpub(filename string) Epub {
func NewEpub(filename string) *Epub {
e := Epub{
filename: filename,
compatibleFormats: map[string][]string{
Expand All @@ -40,22 +40,22 @@ func NewEpub(filename string) Epub {
},
}

return e
return &e
}

// SupportedFormats returns a map witht the compatible formats that Pdf is
// compatible to be converted to.
func (e Epub) SupportedFormats() map[string][]string {
func (e *Epub) SupportedFormats() map[string][]string {
return e.compatibleFormats
}

// SupportedMIMETypes returns a map witht the compatible MIME types that Pdf is
// compatible to be converted to.
func (e Epub) SupportedMIMETypes() map[string][]string {
func (e *Epub) SupportedMIMETypes() map[string][]string {
return e.compatibleMIMETypes
}

func (e Epub) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, error) {
func (e *Epub) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, error) {
// These are guard clauses that check if the target file type is valid.
compatibleFormats, ok := e.SupportedFormats()[fileType]
if !ok {
Expand Down Expand Up @@ -93,6 +93,6 @@ func (e Epub) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, er
}

// EbookType returns the Ebook type which is Epub in this case.
func (e Epub) EbookType() string {
func (e *Epub) EbookType() string {
return EPUB
}
12 changes: 6 additions & 6 deletions pkg/files/ebooks/mobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Mobi struct {
compatibleMIMETypes map[string][]string
}

func NewMobi(filename string) Mobi {
func NewMobi(filename string) *Mobi {
m := Mobi{
filename: filename,
compatibleFormats: map[string][]string{
Expand All @@ -39,22 +39,22 @@ func NewMobi(filename string) Mobi {
},
}

return m
return &m
}

// SupportedFormats returns a map witht the compatible formats that MOBI is
// compatible to be converted to.
func (m Mobi) SupportedFormats() map[string][]string {
func (m *Mobi) SupportedFormats() map[string][]string {
return m.compatibleFormats
}

// SupportedMIMETypes returns a map witht the compatible MIME types that MOBI is
// compatible to be converted to.
func (m Mobi) SupportedMIMETypes() map[string][]string {
func (m *Mobi) SupportedMIMETypes() map[string][]string {
return m.compatibleMIMETypes
}

func (m Mobi) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, error) {
func (m *Mobi) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, error) {
// These are guard clauses that check if the target file type is valid.
compatibleFormats, ok := m.SupportedFormats()[fileType]
if !ok {
Expand Down Expand Up @@ -92,6 +92,6 @@ func (m Mobi) ConvertTo(fileType, subtype string, file io.Reader) (io.Reader, er
}

// EbookType returns the Ebook type which is MOBI in this case.
func (m Mobi) EbookType() string {
func (m *Mobi) EbookType() string {
return EPUB
}

0 comments on commit a7cd3da

Please sign in to comment.