-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
154 lines (128 loc) · 4.47 KB
/
data.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2023 The bpmn Authors
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
*/
package data
import (
"context"
"github.com/olive-io/bpmn/schema"
)
// IItem is an abstract interface for a piece of data
type IItem interface{}
// IIteratorStopper stops Collection iterator and releases resources
// associated with it
type IIteratorStopper interface {
// Stop does the actual stopping
Stop()
}
type channelIteratorStopper struct {
ch chan struct{}
}
func makeChannelIteratorStopper() channelIteratorStopper {
return channelIteratorStopper{ch: make(chan struct{})}
}
func (c channelIteratorStopper) close() {
close(c.ch)
}
func (c channelIteratorStopper) Stop() {
c.ch <- struct{}{}
}
type ICollection interface {
IItem
// ItemIterator returns a channel that iterates over collection's
// items and an IteratorStopper that must be used if iterator was
// not exhausted, otherwise there'll be a memory leak in a form
// of a goroutine that does nothing.
//
// The iterator will also clean itself up and terminate upon
// context termination.
ItemIterator(ctx context.Context) (chan IItem, IIteratorStopper)
}
type SliceIterator []IItem
func (s *SliceIterator) ItemIterator(ctx context.Context) (items chan IItem, stop IIteratorStopper) {
items = make(chan IItem)
stopper := makeChannelIteratorStopper()
stop = stopper
go func() {
loop:
for i := range *s {
select {
case <-ctx.Done():
break loop
case <-stopper.ch:
break loop
case items <- (*s)[i]:
}
}
close(items)
stopper.close()
}()
return
}
// ItemOrCollection will return nil if no items given,
// the same item if only one item is given and SliceIterator
// if more than one item is given. SliceIterator implements
// Collection and, therefore, also implements Item.
func ItemOrCollection(items ...IItem) (item IItem) {
switch len(items) {
case 0:
case 1:
item = items[0]
default:
item = SliceIterator(items)
}
return
}
// IItemAware provides basic interface of accessing data items
type IItemAware interface {
// Get returns a channel that will eventually return the data item
Get() IItem
// Put puts to update the item
Put(item IItem)
}
// ILocatorCloner provides basic interface of cloning data items
type ILocatorCloner interface {
CloneFor(target ILocatorCloner)
}
// IItemAwareLocator interface describes a way to find and put IItemAware
type IItemAwareLocator interface {
// FindItemAwareById finds ItemAware by its schema.Id
FindItemAwareById(id schema.IdRef) (itemAware IItemAware, found bool)
// FindItemAwareByName finds ItemAware by its name (where applicable)
FindItemAwareByName(name string) (itemAware IItemAware, found bool)
// PutItemAwareById puts ItemAware by its schema.Id
PutItemAwareById(id schema.IdRef, itemAware IItemAware)
// PutItemAwareByName puts ItemAware by its name (where applicable)
PutItemAwareByName(name string, itemAware IItemAware)
// Clone clones all IItem to the specified target
Clone() map[string]any
}
type DefaultItemAwareLocator struct{}
func (d DefaultItemAwareLocator) FindItemAwareById(id schema.IdRef) (itemAware IItemAware, found bool) {
return
}
func (d DefaultItemAwareLocator) FindItemAwareByName(name string) (itemAware IItemAware, found bool) {
return
}
func (d DefaultItemAwareLocator) Clone() map[string]any { return map[string]any{} }
func (d DefaultItemAwareLocator) PutItemAwareById(id schema.IdRef, itemAware IItemAware) {}
func (d DefaultItemAwareLocator) PutItemAwareByName(name string, itemAware IItemAware) {}
// IFlowDataLocator interface describes a way to find and put IItemAwareLocator and Variables
type IFlowDataLocator interface {
FindIItemAwareLocator(name string) (locator IItemAwareLocator, found bool)
PutIItemAwareLocator(name string, locator IItemAwareLocator)
CloneItems(name string) map[string]any
GetVariable(name string) (value any, found bool)
SetVariable(name string, value any)
CloneVariables() map[string]any
Merge(other IFlowDataLocator)
}