/
namer.go
73 lines (63 loc) · 1.36 KB
/
namer.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
package stm
import (
"fmt"
"log"
)
// NewNamer returns created the Namer's pointer
func NewNamer(opts *NOpts) *Namer {
if opts.extension == "" {
opts.extension = ".xml.gz"
}
namer := &Namer{opts: opts}
namer.Reset()
return namer
}
// NOpts Namer's option
type NOpts struct {
base string // filename base
zero int
extension string
start int
}
// Namer provides sitemap's filename as a file number counter.
type Namer struct {
count int
opts *NOpts
}
// String returns that combines filename base and file extension.
func (n *Namer) String() string {
ext := n.opts.extension
if n.count == 0 {
return fmt.Sprintf("%s%s", n.opts.base, ext)
}
return fmt.Sprintf("%s%d%s", n.opts.base, n.count, ext)
}
// Reset will initialize to zero value on Namer's counter.
func (n *Namer) Reset() {
n.count = n.opts.zero
}
// IsStart confirms that this struct has zero value.
func (n *Namer) IsStart() bool {
return n.count == n.opts.zero
}
// Next is going to go to next index for filename.
func (n *Namer) Next() *Namer {
if n.IsStart() {
n.count = n.opts.start
} else {
n.count++
}
return n
}
// Previous is going to go to previous index for filename.
func (n *Namer) Previous() *Namer {
if n.IsStart() {
log.Fatal("[F] Already at the start of the series")
}
if n.count <= n.opts.start {
n.count = n.opts.zero
} else {
n.count--
}
return n
}