Skip to content

Commit

Permalink
Refactor resource and module reader lookup methods for improved reada…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
kasugamirai committed Dec 18, 2024
1 parent bcae61c commit 46940d5
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions pkl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,7 @@ func (e *evaluator) handleReadResource(msg *msgapi.ReadResource) {
e.manager.impl.outChan() <- response
return
}
var reader ResourceReader
for _, r := range e.resourceReaders {
if r.Scheme() == u.Scheme {
reader = r
break
}
}
reader := e.findResourceReader(u.Scheme)
if reader == nil {
response.Error = fmt.Sprintf("No resource reader found for scheme `%s`", u.Scheme)
e.manager.impl.outChan() <- response
Expand Down Expand Up @@ -258,15 +252,6 @@ func (e *evaluator) handleListResources(msg *msgapi.ListResources) {
e.manager.impl.outChan() <- response
}

func (e *evaluator) findResourceReader(scheme string) ResourceReader {
for _, r := range e.resourceReaders {
if r.Scheme() == scheme {
return r
}
}
return nil
}

func (e *evaluator) handleListModules(msg *msgapi.ListModules) {
response := &msgapi.ListModulesResponse{EvaluatorId: e.evaluatorId, RequestId: msg.RequestId}
u, err := url.Parse(msg.Uri)
Expand All @@ -275,13 +260,7 @@ func (e *evaluator) handleListModules(msg *msgapi.ListModules) {
e.manager.impl.outChan() <- response
return
}
var reader ModuleReader
for _, r := range e.moduleReaders {
if r.Scheme() == u.Scheme {
reader = r
break
}
}
reader := e.findModuleReader(u.Scheme)
if reader == nil {
response.Error = fmt.Sprintf("No module reader found for scheme `%s`", u.Scheme)
e.manager.impl.outChan() <- response
Expand All @@ -301,6 +280,24 @@ func (e *evaluator) handleListModules(msg *msgapi.ListModules) {
e.manager.impl.outChan() <- response
}

func (e *evaluator) findModuleReader(scheme string) ModuleReader {
for _, r := range e.moduleReaders {
if r.Scheme() == scheme {
return r
}
}
return nil
}

func (e *evaluator) findResourceReader(scheme string) ResourceReader {
for _, r := range e.resourceReaders {
if r.Scheme() == scheme {
return r
}
}
return nil
}

type simpleEvaluator struct {
Evaluator
manager EvaluatorManager
Expand Down

0 comments on commit 46940d5

Please sign in to comment.