Skip to content

Commit

Permalink
Applied few code refactors. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
diogorodriguesc authored Nov 2, 2024
1 parent 95373ea commit e8bf007
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 56 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
go test
9 changes: 2 additions & 7 deletions _examples/reader/reader.go → _examples/sitemaps/sitemaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ import (

func main() {
sitemap := &lutetiumgo.Sitemap{
Xml: lutetiumgo.Xml{
Location: "../../sitemaps.xml",
},
Path: "../../sitemaps.xml",
}

_, err := fmt.Fprintf(os.Stdout, "Location %s\n", sitemap.Location())
if err != nil {
return
}
fmt.Fprintf(os.Stdout, "Path %s\n", sitemap.Path)

UrlSet := sitemap.Read()

Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg=
31 changes: 0 additions & 31 deletions reader_test.go

This file was deleted.

11 changes: 7 additions & 4 deletions reader.go → sitemaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"os"
)

func (sitemap *Sitemap) Location() string { return sitemap.Xml.Location }

func (sitemap *Sitemap) Read() *UrlSet {
file, err := os.Open(sitemap.Location())
file, err := os.Open(sitemap.Path)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
}
defer file.Close()
defer func(file *os.File) {
err := file.Close()
if err != nil {

}
}(file)

decoder := xml.NewDecoder(file)

Expand Down
10 changes: 9 additions & 1 deletion sitemaps.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<loc>https://www.example.com/a</loc>
<lastmod>2024-10-19</lastmod>
</url>
<url>
<loc>https://www.example.com/b</loc>
<lastmod>2024-10-18</lastmod>
</url>
<url>
<loc>https://www.example.com/c</loc>
<lastmod>2024-10-17</lastmod>
</url>
</urlset>
45 changes: 45 additions & 0 deletions sitemaps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lutetiumgo

import (
"testing"
)

func TestSitemap_Creation(t *testing.T) {
sitemap := &Sitemap{
Path: "https://location/sitemap.xml",
}

if sitemap.Path != "https://location/sitemap.xml" {
t.Errorf("Expected is %s, got %s", "https://location/sitemap.xml", sitemap.Path)
}
}

func TestSitemap_Read(t *testing.T) {
sitemap := &Sitemap{
Path: "sitemaps.xml",
}

UrlSet := sitemap.Read()

if UrlSet.Xmlns != "http://www.sitemaps.org/schemas/sitemap/0.9" {
t.Errorf("Expected is %s, got %s", "http://www.sitemaps.org/schemas/sitemap/0.9", UrlSet.Xmlns)
}

if len(UrlSet.Urls) != 3 {
t.Errorf("Expected UrlSet.Urls count is 3, got %d", len(UrlSet.Urls))
}

var ExpectedUrls = [3]string{"https://www.example.com/a", "https://www.example.com/b", "https://www.example.com/c"}
var ExpectedLastMods = [3]string{"2024-10-19", "2024-10-18", "2024-10-17"}

i := 0
for _, Urls := range UrlSet.Urls {
if Urls.Loc != ExpectedUrls[i] {
t.Errorf("Expected is %s, got %s", ExpectedUrls[i], Urls.Loc)
}
if Urls.LastMod != ExpectedLastMods[i] {
t.Errorf("Expected is %s, got %s", ExpectedLastMods[i], Urls.LastMod)
}
i++
}
}
6 changes: 1 addition & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package lutetiumgo

import "encoding/xml"

type Xml struct {
Location string
}

type Sitemap struct {
Xml Xml
Path string
}

type UrlSet struct {
Expand Down

0 comments on commit e8bf007

Please sign in to comment.