Skip to content

Commit

Permalink
refactor: remove unnessary bool return
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 3, 2024
1 parent 8dd63a0 commit b0feb7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func (r *Runtime) Reconcile(ctx context.Context) error {

bounded := make(map[uuid.UUID]spec.Spec)
for _, id := range r.symbolTable.Keys() {
sb, ok := r.symbolTable.Lookup(id)
if ok && r.scheme.IsBound(sb.Spec, secrets...) {
sb := r.symbolTable.Lookup(id)
if sb != nil && r.scheme.IsBound(sb.Spec, secrets...) {
bounded[sb.Spec.GetID()] = sb.Spec
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/symbol/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (l *Loader) Load(ctx context.Context, specs ...spec.Spec) error {
continue
}

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

for _, id := range l.table.Keys() {
sb, ok := l.table.Lookup(id)
if ok && len(resource.Match(sb.Spec, examples...)) > 0 {
sb := l.table.Lookup(id)
if sb != nil && len(resource.Match(sb.Spec, examples...)) > 0 {
ok := false
for _, s := range symbols {
if s.ID() == id {
Expand Down
24 changes: 6 additions & 18 deletions pkg/symbol/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,9 @@ func TestLoader_Load(t *testing.T) {

err := loader.Load(ctx, meta1, meta2, meta3)
assert.NoError(t, err)

_, ok := table.Lookup(meta1.GetID())
assert.True(t, ok)

_, ok = table.Lookup(meta2.GetID())
assert.True(t, ok)

_, ok = table.Lookup(meta3.GetID())
assert.True(t, ok)
assert.NotNil(t, table.Lookup(meta1.GetID()))
assert.NotNil(t, table.Lookup(meta2.GetID()))
assert.NotNil(t, table.Lookup(meta3.GetID()))
})

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

err = loader.Load(ctx, meta)
assert.NoError(t, err)

_, ok := table.Lookup(meta.GetID())
assert.False(t, ok)
assert.NotNil(t, table.Lookup(meta.GetID()))
})

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

err := loader.Load(ctx, meta)
assert.NoError(t, err)

_, ok := table.Lookup(meta.GetID())
assert.True(t, ok)
assert.NotNil(t, table.Lookup(meta.GetID()))
})

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

err := loader.Load(ctx, meta)
assert.Error(t, err)

_, ok := table.Lookup(meta.GetID())
assert.False(t, ok)
assert.NotNil(t, table.Lookup(meta.GetID()))
})
}
10 changes: 3 additions & 7 deletions pkg/symbol/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func (t *Table) Insert(sb *Symbol) error {
if _, err := t.free(sb.ID()); err != nil {
return err
}
if err := t.insert(sb); err != nil {
return err
}
return nil
return t.insert(sb)
}

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

// Lookup retrieves a symbol from the table by its ID.
func (t *Table) Lookup(id uuid.UUID) (*Symbol, bool) {
func (t *Table) Lookup(id uuid.UUID) *Symbol {
t.mu.RLock()
defer t.mu.RUnlock()

sb, ok := t.symbols[id]
return sb, ok
return t.symbols[id]
}

// Keys returns all IDs of symbols in the table.
Expand Down
6 changes: 2 additions & 4 deletions pkg/symbol/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,10 @@ func TestTable_LookupByID(t *testing.T) {
}

sb := &Symbol{Spec: meta, Node: node.NewOneToOneNode(nil)}

err := tb.Insert(sb)
assert.NoError(t, err)

r, ok := tb.Lookup(sb.ID())
assert.True(t, ok)
assert.Equal(t, sb, r)
assert.Equal(t, sb, tb.Lookup(sb.ID()))
}

func TestTable_Keys(t *testing.T) {
Expand Down

0 comments on commit b0feb7d

Please sign in to comment.