Skip to content

Commit

Permalink
document: return a flattened version with all bookmarks attached to t…
Browse files Browse the repository at this point in the history
…he root folder

Signed-off-by: VirtualTam <[email protected]>
  • Loading branch information
virtualtam committed Apr 30, 2022
1 parent daf67af commit 6a45712
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 100 deletions.
100 changes: 0 additions & 100 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,39 +249,6 @@ func TestDecodeFolder(t *testing.T) {
}
}

func assertFoldersEqual(t *testing.T, got Folder, want Folder) {
t.Helper()

assertDatesEqual(t, "creation", got.CreatedAt, want.CreatedAt)
assertDatesEqual(t, "update", got.UpdatedAt, want.UpdatedAt)

if got.Description != want.Description {
t.Errorf("want description %q, got %q", want.Description, got.Description)
}

if got.Name != want.Name {
t.Errorf("want name %q, got %q", want.Name, got.Name)
}

assertAttributesEqual(t, got.Attributes, want.Attributes)

if len(got.Bookmarks) != len(want.Bookmarks) {
t.Fatalf("want %d bookmarks, got %d", len(want.Bookmarks), len(got.Bookmarks))
}

for index, wantBookmark := range want.Bookmarks {
assertBookmarksEqual(t, got.Bookmarks[index], wantBookmark)
}

if len(got.Subfolders) != len(want.Subfolders) {
t.Fatalf("want %d subfolders, got %d", len(want.Subfolders), len(got.Subfolders))
}

for index, wantSubfolder := range want.Subfolders {
assertFoldersEqual(t, got.Subfolders[index], wantSubfolder)
}
}

func TestDecodeBookmark(t *testing.T) {
bookmarkCreatedAt := time.Date(2022, time.March, 1, 17, 11, 13, 0, time.UTC)
bookmarkUpdatedAt := time.Date(2022, time.March, 1, 22, 9, 46, 0, time.UTC)
Expand Down Expand Up @@ -404,39 +371,6 @@ func TestDecodeBookmark(t *testing.T) {
}
}

func assertBookmarksEqual(t *testing.T, got Bookmark, want Bookmark) {
assertDatesEqual(t, "creation", got.CreatedAt, want.CreatedAt)
assertDatesEqual(t, "update", got.UpdatedAt, want.UpdatedAt)

if got.Title != want.Title {
t.Errorf("want title %q, got %q", want.Title, got.Title)
}

if got.URL != want.URL {
t.Errorf("want URL string %q, got %q", want.URL, got.URL)
}

if got.Description != want.Description {
t.Errorf("want description %q, got %q", want.Description, got.Description)
}

if got.Private != want.Private {
t.Errorf("want private %t, got %t", want.Private, got.Private)
}

if len(got.Tags) != len(want.Tags) {
t.Fatalf("want %d tags, got %d", len(want.Tags), len(got.Tags))
}

for index, wantTag := range want.Tags {
if got.Tags[index] != wantTag {
t.Errorf("want tag %d value %q, got %q", index, wantTag, got.Tags[index])
}
}

assertAttributesEqual(t, got.Attributes, want.Attributes)
}

func TestDecodeDateTime(t *testing.T) {
cases := []struct {
tname string
Expand Down Expand Up @@ -506,37 +440,3 @@ func TestDecodeDateTime(t *testing.T) {
})
}
}

func assertAttributesEqual(t *testing.T, got map[string]string, want map[string]string) {
t.Helper()

if len(got) != len(want) {
t.Fatalf("want %d attributes, got %d", len(want), len(got))
}

for attr, wantValue := range want {
if got[attr] != wantValue {
t.Errorf("want attribute %q value %q, got %q", attr, wantValue, got[attr])
}
}
}

func assertDatesEqual(t *testing.T, name string, got *time.Time, want *time.Time) {
t.Helper()

if want == nil {
if got != nil {
t.Errorf("want %s datetime nil, got %q", name, got.String())
}
return
}

if got == nil {
t.Errorf("want %s datetime %q, got nil", name, want.String())
return
}

if got.String() != want.String() {
t.Errorf("want %s date %q, got %q", name, want.String(), got.String())
}
}
30 changes: 30 additions & 0 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ type Document struct {
Root Folder `json:"root"`
}

// Flatten returns a flat version of this Document, with all Bookmarks attached
// to the Root Folder.
func (d *Document) Flatten() *Document {
flattenedRoot := d.Root.flatten()

return &Document{
Title: d.Title,
Root: *flattenedRoot,
}
}

// A Folder represents a folder containing Netscape Bookmarks and child Folders.
type Folder struct {
CreatedAt *time.Time `json:"created_at,omitempty"`
Expand All @@ -24,6 +35,25 @@ type Folder struct {
Subfolders []Folder `json:"subfolders,omitempty"`
}

func (f *Folder) flatten() *Folder {
flattened := &Folder{
CreatedAt: f.CreatedAt,
UpdatedAt: f.UpdatedAt,
Description: f.Description,
Name: f.Name,
Attributes: f.Attributes,
}

flattened.Bookmarks = append(flattened.Bookmarks, f.Bookmarks...)

for _, subfolder := range f.Subfolders {
flattenedSubfolder := subfolder.flatten()
flattened.Bookmarks = append(flattened.Bookmarks, flattenedSubfolder.Bookmarks...)
}

return flattened
}

// A Bookmark represents a Netscape Bookmark.
type Bookmark struct {
CreatedAt *time.Time `json:"created_at,omitempty"`
Expand Down
Loading

0 comments on commit 6a45712

Please sign in to comment.