Skip to content

Commit be90bca

Browse files
committed
refactor: remove unnessary bool return
1 parent 8dd63a0 commit be90bca

File tree

7 files changed

+51
-65
lines changed

7 files changed

+51
-65
lines changed

pkg/runtime/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func (r *Runtime) Reconcile(ctx context.Context) error {
163163

164164
bounded := make(map[uuid.UUID]spec.Spec)
165165
for _, id := range r.symbolTable.Keys() {
166-
sb, ok := r.symbolTable.Lookup(id)
167-
if ok && r.scheme.IsBound(sb.Spec, secrets...) {
166+
sb := r.symbolTable.Lookup(id)
167+
if sb != nil && r.scheme.IsBound(sb.Spec, secrets...) {
168168
bounded[sb.Spec.GetID()] = sb.Spec
169169
}
170170
}

pkg/symbol/loader.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func (l *Loader) Load(ctx context.Context, specs ...spec.Spec) error {
8585
continue
8686
}
8787

88-
sb, ok := l.table.Lookup(decode.GetID())
89-
if !ok || !reflect.DeepEqual(sb.Spec, decode) {
88+
sb := l.table.Lookup(decode.GetID())
89+
if sb == nil || !reflect.DeepEqual(sb.Spec, decode) {
9090
n, err := l.scheme.Compile(decode)
9191
if err != nil {
9292
errs = append(errs, err)
@@ -108,8 +108,8 @@ func (l *Loader) Load(ctx context.Context, specs ...spec.Spec) error {
108108
}
109109

110110
for _, id := range l.table.Keys() {
111-
sb, ok := l.table.Lookup(id)
112-
if ok && len(resource.Match(sb.Spec, examples...)) > 0 {
111+
sb := l.table.Lookup(id)
112+
if sb != nil && len(resource.Match(sb.Spec, examples...)) > 0 {
113113
ok := false
114114
for _, s := range symbols {
115115
if s.ID() == id {

pkg/symbol/loader_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,9 @@ func TestLoader_Load(t *testing.T) {
7777

7878
err := loader.Load(ctx, meta1, meta2, meta3)
7979
assert.NoError(t, err)
80-
81-
_, ok := table.Lookup(meta1.GetID())
82-
assert.True(t, ok)
83-
84-
_, ok = table.Lookup(meta2.GetID())
85-
assert.True(t, ok)
86-
87-
_, ok = table.Lookup(meta3.GetID())
88-
assert.True(t, ok)
80+
assert.NotNil(t, table.Lookup(meta1.GetID()))
81+
assert.NotNil(t, table.Lookup(meta2.GetID()))
82+
assert.NotNil(t, table.Lookup(meta3.GetID()))
8983
})
9084

9185
t.Run("ReloadWithSameID", func(t *testing.T) {
@@ -166,9 +160,7 @@ func TestLoader_Load(t *testing.T) {
166160

167161
err = loader.Load(ctx, meta)
168162
assert.NoError(t, err)
169-
170-
_, ok := table.Lookup(meta.GetID())
171-
assert.False(t, ok)
163+
assert.Nil(t, table.Lookup(meta.GetID()))
172164
})
173165

174166
t.Run("LoadMultipleSecrets", func(t *testing.T) {
@@ -214,9 +206,7 @@ func TestLoader_Load(t *testing.T) {
214206

215207
err := loader.Load(ctx, meta)
216208
assert.NoError(t, err)
217-
218-
_, ok := table.Lookup(meta.GetID())
219-
assert.True(t, ok)
209+
assert.NotNil(t, table.Lookup(meta.GetID()))
220210
})
221211

222212
t.Run("LoadNonExistSecret", func(t *testing.T) {
@@ -252,8 +242,6 @@ func TestLoader_Load(t *testing.T) {
252242

253243
err := loader.Load(ctx, meta)
254244
assert.Error(t, err)
255-
256-
_, ok := table.Lookup(meta.GetID())
257-
assert.False(t, ok)
245+
assert.Nil(t, table.Lookup(meta.GetID()))
258246
})
259247
}

pkg/symbol/symbol.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import (
1111

1212
// Symbol represents a Node that is identifiable within a Spec.
1313
type Symbol struct {
14-
Spec spec.Spec
15-
Node node.Node
16-
inbounds map[string][]spec.Port
17-
ins map[string]*port.InPort
18-
outs map[string]*port.OutPort
19-
mu sync.RWMutex
14+
Spec spec.Spec
15+
Node node.Node
16+
ins map[string]*port.InPort
17+
outs map[string]*port.OutPort
18+
mu sync.RWMutex
2019
}
2120

2221
var _ node.Node = (*Symbol)(nil)
@@ -51,18 +50,6 @@ func (s *Symbol) Ports() map[string][]spec.Port {
5150
return s.Spec.GetPorts()
5251
}
5352

54-
// Links returns the references associated with the Symbol.
55-
func (s *Symbol) Links() map[string][]spec.Port {
56-
links := make(map[string][]spec.Port)
57-
for name, ports := range s.Spec.GetPorts() {
58-
links[name] = append(links[name], ports...)
59-
}
60-
for name, ports := range s.inbounds {
61-
links[name] = append(links[name], ports...)
62-
}
63-
return links
64-
}
65-
6653
// Env returns the environment variables associated with the Symbol.
6754
func (s *Symbol) Env() map[string][]spec.Secret {
6855
return s.Spec.GetEnv()

pkg/symbol/symbol_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func TestSymbol_Getter(t *testing.T) {
4444
assert.Equal(t, meta.GetName(), sb.Name())
4545
assert.Equal(t, meta.GetAnnotations(), sb.Annotations())
4646
assert.Equal(t, meta.GetPorts(), sb.Ports())
47-
assert.Equal(t, meta.GetPorts(), sb.Links())
4847
assert.Equal(t, meta.GetEnv(), sb.Env())
4948
assert.Equal(t, n.In(node.PortIn), sb.In(node.PortIn))
5049
assert.Equal(t, n.Out(node.PortOut), sb.Out(node.PortOut))

pkg/symbol/table.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type TableOptions struct {
2121
type Table struct {
2222
symbols map[uuid.UUID]*Symbol
2323
namespaces map[string]map[string]uuid.UUID
24+
refences map[uuid.UUID]map[string][]spec.Port
2425
loadHooks LoadHooks
2526
unloadHooks UnloadHooks
2627
mu sync.RWMutex
@@ -38,6 +39,7 @@ func NewTable(opts ...TableOptions) *Table {
3839
return &Table{
3940
symbols: make(map[uuid.UUID]*Symbol),
4041
namespaces: make(map[string]map[string]uuid.UUID),
42+
refences: make(map[uuid.UUID]map[string][]spec.Port),
4143
loadHooks: loadHooks,
4244
unloadHooks: unloadHooks,
4345
}
@@ -48,17 +50,10 @@ func (t *Table) Insert(sb *Symbol) error {
4850
t.mu.Lock()
4951
defer t.mu.Unlock()
5052

51-
if sb.inbounds == nil {
52-
sb.inbounds = make(map[string][]spec.Port)
53-
}
54-
5553
if _, err := t.free(sb.ID()); err != nil {
5654
return err
5755
}
58-
if err := t.insert(sb); err != nil {
59-
return err
60-
}
61-
return nil
56+
return t.insert(sb)
6257
}
6358

6459
// Free removes a symbol from the table by its ID.
@@ -74,12 +69,11 @@ func (t *Table) Free(id uuid.UUID) (bool, error) {
7469
}
7570

7671
// Lookup retrieves a symbol from the table by its ID.
77-
func (t *Table) Lookup(id uuid.UUID) (*Symbol, bool) {
72+
func (t *Table) Lookup(id uuid.UUID) *Symbol {
7873
t.mu.RLock()
7974
defer t.mu.RUnlock()
8075

81-
sb, ok := t.symbols[id]
82-
return sb, ok
76+
return t.symbols[id]
8377
}
8478

8579
// Keys returns all IDs of symbols in the table.
@@ -198,7 +192,13 @@ func (t *Table) links(sb *Symbol) {
198192
}
199193
}
200194

201-
ref.inbounds[port.Port] = append(ref.inbounds[port.Port], spec.Port{
195+
refences := t.refences[ref.ID()]
196+
if refences == nil {
197+
refences = make(map[string][]spec.Port)
198+
t.refences[ref.ID()] = refences
199+
}
200+
201+
refences[port.Port] = append(refences[port.Port], spec.Port{
202202
ID: sb.ID(),
203203
Name: port.Name,
204204
Port: name,
@@ -224,7 +224,13 @@ func (t *Table) links(sb *Symbol) {
224224
}
225225
}
226226

227-
sb.inbounds[port.Port] = append(sb.inbounds[port.Port], spec.Port{
227+
refences := t.refences[sb.ID()]
228+
if refences == nil {
229+
refences = make(map[string][]spec.Port)
230+
t.refences[sb.ID()] = refences
231+
}
232+
233+
refences[port.Port] = append(refences[port.Port], spec.Port{
228234
ID: ref.ID(),
229235
Name: port.Name,
230236
Port: name,
@@ -248,20 +254,28 @@ func (t *Table) unlinks(sb *Symbol) {
248254
continue
249255
}
250256

257+
refences := t.refences[ref.ID()]
258+
if refences == nil {
259+
refences = make(map[string][]spec.Port)
260+
t.refences[ref.ID()] = refences
261+
}
262+
251263
var ports []spec.Port
252-
for _, port := range ref.inbounds[port.Port] {
264+
for _, port := range refences[port.Port] {
253265
if port.ID != sb.ID() && port.Port != name {
254266
ports = append(ports, port)
255267
}
256268
}
257269

258270
if len(ports) > 0 {
259-
ref.inbounds[port.Port] = ports
271+
refences[port.Port] = ports
260272
} else {
261-
delete(ref.inbounds, port.Port)
273+
delete(refences, port.Port)
262274
}
263275
}
264276
}
277+
278+
delete(t.refences, sb.ID())
265279
}
266280

267281
func (t *Table) linked(sb *Symbol) []*Symbol {
@@ -271,7 +285,7 @@ func (t *Table) linked(sb *Symbol) []*Symbol {
271285
for len(nexts) > 0 {
272286
sb := nexts[len(nexts)-1]
273287
ok := true
274-
for _, ports := range sb.inbounds {
288+
for _, ports := range t.refences[sb.ID()] {
275289
for _, port := range ports {
276290
next := t.symbols[port.ID]
277291
ok = slices.Contains(nexts, next) || slices.Contains(linked, next)

pkg/symbol/table_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func TestTable_Free(t *testing.T) {
383383
assert.Equal(t, 0, p3.Links())
384384
}
385385

386-
func TestTable_LookupByID(t *testing.T) {
386+
func TestTable_Lookup(t *testing.T) {
387387
kind := faker.UUIDHyphenated()
388388

389389
tb := NewTable()
@@ -396,12 +396,10 @@ func TestTable_LookupByID(t *testing.T) {
396396
}
397397

398398
sb := &Symbol{Spec: meta, Node: node.NewOneToOneNode(nil)}
399+
399400
err := tb.Insert(sb)
400401
assert.NoError(t, err)
401-
402-
r, ok := tb.Lookup(sb.ID())
403-
assert.True(t, ok)
404-
assert.Equal(t, sb, r)
402+
assert.Equal(t, sb, tb.Lookup(sb.ID()))
405403
}
406404

407405
func TestTable_Keys(t *testing.T) {

0 commit comments

Comments
 (0)