forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
139 lines (103 loc) · 2.45 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2014 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package model
import (
"fmt"
"sort"
"strings"
"github.com/andreaskoch/allmark/common/route"
"github.com/andreaskoch/allmark/dataaccess"
)
type ItemType int
func (itemType ItemType) String() string {
switch itemType {
case TypeDocument:
return "document"
case TypePresentation:
return "presentation"
case TypeRepository:
return "repository"
default:
return "unknown"
}
panic("Unreachable")
}
const (
TypeDocument ItemType = iota
TypePresentation
TypeRepository
TypeUnknown
)
// An Item represents a single document.
type Item struct {
route route.Route
files []*File
sourceType dataaccess.ItemType
Type ItemType
Title string
Description string
Content string
Markdown string
Hash string
MetaData MetaData
}
func NewItem(route route.Route, files []*File, sourceType dataaccess.ItemType) *Item {
return &Item{
route: route,
files: files,
sourceType: sourceType,
}
}
func (item *Item) String() string {
return fmt.Sprintf("%s", item.route)
}
func (item *Item) FolderName() string {
return item.route.LastComponentName()
}
func (item *Item) Route() route.Route {
return item.route
}
func (item *Item) Files() []*File {
return item.files
}
// Get the file which matches the supplied route. Returns nil if there is no matching file.
func (item *Item) GetFile(fileRoute route.Route) *File {
for _, file := range item.Files() {
if !strings.HasSuffix(fileRoute.Value(), file.Route().Value()) {
continue
}
return file
}
return nil
}
func (item *Item) IsPhysical() bool {
return item.sourceType == dataaccess.TypePhysical
}
func (item *Item) IsVirtual() bool {
return item.sourceType == dataaccess.TypeVirtual
}
func (item *Item) IsFileCollection() bool {
return item.sourceType == dataaccess.TypeFileCollection
}
type SortItemsBy func(item1, item2 *Item) bool
func (by SortItemsBy) Sort(items []*Item) {
sorter := &modelSorter{
items: items,
by: by,
}
sort.Sort(sorter)
}
type modelSorter struct {
items []*Item
by SortItemsBy
}
func (sorter *modelSorter) Len() int {
return len(sorter.items)
}
func (sorter *modelSorter) Swap(i, j int) {
sorter.items[i], sorter.items[j] = sorter.items[j], sorter.items[i]
}
func (sorter *modelSorter) Less(i, j int) bool {
return sorter.by(sorter.items[i], sorter.items[j])
}