Skip to content

Commit

Permalink
refactor: be simple if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 27, 2023
1 parent a2ee185 commit bc39336
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
38 changes: 16 additions & 22 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ func (ld *Loader) LoadOne(ctx context.Context, id ulid.ULID) (node.Node, error)
namespace = spec.GetNamespace()
}

if sym, ok := ld.table.LookupByID(spec.GetID()); ok {
if reflect.DeepEqual(sym.Spec, spec) {
continue
}
if sym, ok := ld.table.LookupByID(spec.GetID()); ok && reflect.DeepEqual(sym.Spec, spec) {
continue
}

if n, err := ld.scheme.Decode(spec); err != nil {
Expand All @@ -115,22 +113,20 @@ func (ld *Loader) LoadOne(ctx context.Context, id ulid.ULID) (node.Node, error)
}

for key, exist := range exists {
if exist {
continue
}

id, ok := key.(ulid.ULID)
if !ok {
if name, ok := key.(string); ok {
if sym, ok := ld.table.LookupByName(namespace, name); ok {
id = sym.ID()
if !exist {
id, ok := key.(ulid.ULID)
if !ok {
if name, ok := key.(string); ok {
if sym, ok := ld.table.LookupByName(namespace, name); ok {
id = sym.ID()
}
}
}
}

if id != (ulid.ULID{}) {
if _, err := ld.table.Free(id); err != nil {
return nil, err
if id != (ulid.ULID{}) {
if _, err := ld.table.Free(id); err != nil {
return nil, err
}
}
}
}
Expand All @@ -157,11 +153,9 @@ func (ld *Loader) LoadAll(ctx context.Context) ([]node.Node, error) {

var nodes []node.Node
for _, spec := range specs {
if sym, ok := ld.table.LookupByID(spec.GetID()); ok {
if reflect.DeepEqual(sym.Spec, spec) {
nodes = append(nodes, sym.Node)
continue
}
if sym, ok := ld.table.LookupByID(spec.GetID()); ok && reflect.DeepEqual(sym.Spec, spec) {
nodes = append(nodes, sym.Node)
continue
}

if n, err := ld.scheme.Decode(spec); err != nil {
Expand Down
15 changes: 9 additions & 6 deletions pkg/symbol/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ func (t *Table) Free(id ulid.ULID) (bool, error) {
t.mu.Lock()
defer t.mu.Unlock()

if sym, err := t.free(id); err != nil {
sym, err := t.free(id)
if err != nil {
return false, err
} else if sym != nil {
return true, nil
}
return false, nil
return sym != nil, nil
}

// LookupByID retrieves a Symbol by its ID.
Expand Down Expand Up @@ -313,7 +312,7 @@ func (t *Table) free(id ulid.ULID) (*Symbol, error) {
}

func (t *Table) load(sym *Symbol) error {
if len(t.unlinks[sym.ID()]) > 0 {
if t.shouldSkipLink(sym) {
return nil
}
for _, hook := range t.loadHooks {
Expand All @@ -325,7 +324,7 @@ func (t *Table) load(sym *Symbol) error {
}

func (t *Table) unload(sym *Symbol) error {
if len(t.unlinks[sym.ID()]) > 0 {
if t.shouldSkipLink(sym) {
return nil
}
for _, hook := range t.unloadHooks {
Expand All @@ -335,3 +334,7 @@ func (t *Table) unload(sym *Symbol) error {
}
return nil
}

func (t *Table) shouldSkipLink(sym *Symbol) bool {
return len(t.unlinks[sym.ID()]) > 0
}

0 comments on commit bc39336

Please sign in to comment.