/
types.go
159 lines (126 loc) · 3.81 KB
/
types.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
155
156
157
158
159
/*
Copyright 2024 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package graph
import (
eventingv1beta3 "knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
type Graph struct {
vertices map[comparableDestination]*Vertex
}
type Vertex struct {
self *duckv1.Destination
inEdges []*Edge
outEdges []*Edge
visited bool
}
type Edge struct {
transform TransformFunction
self *duckv1.Destination
from *Vertex
to *Vertex
}
// comparableDestination is a modified version of duckv1.Destination that is comparable (no pointers).
// It also omits some fields not needed for event lineage.
type comparableDestination struct {
// Ref points to an Addressable.
// +optional
Ref duckv1.KReference `json:"ref,omitempty"`
// URI can be an absolute URL(non-empty scheme and non-empty host) pointing to the target or a relative URI. Relative URIs will be resolved using the base URI retrieved from Ref.
// +optional
URI apis.URL `json:"uri,omitempty"`
}
type TransformFunction func(et *eventingv1beta3.EventType, tfc TransformFunctionContext) (*eventingv1beta3.EventType, TransformFunctionContext)
// TODO(cali0707): flesh this out more, know we need it, not sure what needs to be in it yet
type TransformFunctionContext struct{}
func (t TransformFunctionContext) DeepCopy() TransformFunctionContext { return t } // TODO(cali0707) implement this once we have fleshed out the transform function context struct
func NewGraph() *Graph {
return &Graph{
vertices: make(map[comparableDestination]*Vertex),
}
}
func (g *Graph) Vertices() []*Vertex {
vertices := make([]*Vertex, len(g.vertices))
for _, v := range g.vertices {
vertices = append(vertices, v)
}
return vertices
}
func (g *Graph) UnvisitAll() {
for _, v := range g.vertices {
v.visited = false
}
}
func (v *Vertex) InDegree() int {
return len(v.inEdges)
}
func (v *Vertex) OutDegree() int {
return len(v.outEdges)
}
func (v *Vertex) Reference() *duckv1.Destination {
return v.self
}
func (v *Vertex) InEdges() []*Edge {
return v.inEdges
}
func (v *Vertex) OutEdges() []*Edge {
return v.outEdges
}
func (v *Vertex) Visit() {
v.visited = true
}
func (v *Vertex) Unvisit() {
v.visited = false
}
func (v *Vertex) Visited() bool {
return v.visited
}
func (v *Vertex) NewWithSameRef() *Vertex {
return &Vertex{
self: v.self,
}
}
func (v *Vertex) AddEdge(to *Vertex, edgeRef *duckv1.Destination, transform TransformFunction) {
edge := &Edge{from: v, to: to, transform: transform, self: edgeRef}
v.outEdges = append(v.outEdges, edge)
to.inEdges = append(to.inEdges, edge)
}
func (e *Edge) Transform(et *eventingv1beta3.EventType, tfc TransformFunctionContext) (*eventingv1beta3.EventType, TransformFunctionContext) {
if et == nil {
return nil, tfc
}
return e.transform(et, tfc)
}
func (e *Edge) From() *Vertex {
return e.from
}
func (e *Edge) To() *Vertex {
return e.to
}
func (e *Edge) Reference() *duckv1.Destination {
return e.self
}
func NoTransform(et *eventingv1beta3.EventType, tfc TransformFunctionContext) (*eventingv1beta3.EventType, TransformFunctionContext) {
return et, tfc
}
func makeComparableDestination(dest *duckv1.Destination) comparableDestination {
res := comparableDestination{}
if dest.Ref != nil {
res.Ref = *dest.Ref
}
if dest.URI != nil {
res.URI = *dest.URI
}
return res
}