-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathchange_list.go
More file actions
113 lines (93 loc) · 2.49 KB
/
change_list.go
File metadata and controls
113 lines (93 loc) · 2.49 KB
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
package provider
import (
"iter"
"github.com/libdns/libdns"
)
type ChangeState uint8
const (
NoChange ChangeState = 1 << iota
Delete
Create
)
type ChangeRecord struct {
record *libdns.RR
state ChangeState
}
type ChangeList interface {
// Iterate wil return an iterator that returns records that
// match the given state. For example, when called like
// `Iterate(Delete)` will only return records marked for
// removal. The ChangeState can be combined to iterate
// multiple states like `Iterate(Delete|Create)` which
// will return all records that are marked delete or
// as created.
Iterate(state ChangeState) iter.Seq[*libdns.RR]
// Creates will return a slice of records that are
// marked for creating
Creates() []*libdns.RR
// Deletes will return a slice of records that are
// marked for deleting
Deletes() []*libdns.RR
// GetList will return a slice of records that
// represents the new dns list which can be used
// to update the whole set for a zone
GetList() []*libdns.RR
// Has wil check if this list has records for
// given state
Has(state ChangeState) bool
// addRecord is not exported because the record
// list is immutable
addRecord(record *libdns.RR, state ChangeState)
}
type changes struct {
records []*ChangeRecord
state ChangeState
}
func NewChangeList(size ...int) ChangeList {
var records []*ChangeRecord
switch len(size) {
case 1:
records = make([]*ChangeRecord, size[0])
case 2:
records = make([]*ChangeRecord, size[0], size[1])
default:
records = make([]*ChangeRecord, 0)
}
return &changes{
records: records,
}
}
func (c *changes) addRecord(record *libdns.RR, state ChangeState) {
c.records = append(c.records, &ChangeRecord{record: record, state: state})
c.state |= state
}
func (c *changes) Has(state ChangeState) bool {
return 0 != (c.state & state)
}
func (c *changes) Iterate(state ChangeState) iter.Seq[*libdns.RR] {
return func(yield func(*libdns.RR) bool) {
for i, x := 0, len(c.records); i < x; i++ {
if c.records[i].state == (c.records[i].state & state) {
if false == yield(c.records[i].record) {
return
}
}
}
}
}
func (c *changes) Creates() []*libdns.RR {
return c.list(Create)
}
func (c *changes) Deletes() []*libdns.RR {
return c.list(Delete)
}
func (c *changes) GetList() []*libdns.RR {
return c.list(Create | NoChange)
}
func (c *changes) list(state ChangeState) []*libdns.RR {
var items = make([]*libdns.RR, 0)
for record := range c.Iterate(state) {
items = append(items, record)
}
return items
}