Skip to content

Commit

Permalink
Simplify GetPipeline, GetIntegration, GetNotifier, GetTrigger functio…
Browse files Browse the repository at this point in the history
…ns, make them generic.
  • Loading branch information
vhadianto committed Feb 16, 2024
1 parent 4a49bc5 commit 5d221fd
Showing 1 changed file with 33 additions and 53 deletions.
86 changes: 33 additions & 53 deletions internal/es/db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,48 @@ import (
"github.com/turbot/pipe-fittings/perr"
)

// Ristretto backed pipeline datatabase
func typeName[T any](t T) string {
return "item"
}

func GetPipeline(name string) (*modconfig.Pipeline, error) {
func GetCachedItem[T any](name string) (T, error) {
var defaultT T // default zero value for type T

// TODO: hack while we're transitioning to mod format
parts := strings.Split(name, ".")
if len(parts) != 3 {
name = "local.pipeline." + name
// Special handling for pipeline names
if _, ok := any(defaultT).(*modconfig.Pipeline); ok {
parts := strings.Split(name, ".")
if len(parts) != 3 {
name = "local.pipeline." + name
}
}

pipelineCached, found := cache.GetCache().Get(name)
cachedItem, found := cache.GetCache().Get(name)
if !found {
return nil, perr.NotFoundWithMessage("pipeline definition not found: " + name)
return defaultT, perr.NotFoundWithMessage(typeName(defaultT) + " definition not found: " + name)
}

pipeline, ok := pipelineCached.(*modconfig.Pipeline)
item, ok := cachedItem.(T)
if !ok {
return nil, perr.InternalWithMessage("invalid pipeline")
return defaultT, perr.InternalWithMessage("invalid " + typeName(defaultT))
}
return pipeline, nil

return item, nil
}

func GetNotifier(name string) (modconfig.Notifier, error) {
return GetCachedItem[modconfig.Notifier](name)
}

func GetIntegration(name string) (modconfig.Integration, error) {
return GetCachedItem[modconfig.Integration](name)
}

func GetPipeline(name string) (*modconfig.Pipeline, error) {
return GetCachedItem[*modconfig.Pipeline](name)
}

func GetTrigger(name string) (*modconfig.Trigger, error) {
return GetCachedItem[*modconfig.Trigger](name)
}

func ListAllPipelines() ([]*modconfig.Pipeline, error) {
Expand All @@ -54,20 +76,6 @@ func ListAllPipelines() ([]*modconfig.Pipeline, error) {
return pipelines, nil
}

func GetIntegration(name string) (modconfig.Integration, error) {

integrationCached, found := cache.GetCache().Get(name)
if !found {
return nil, perr.NotFoundWithMessage("integration definition not found: " + name)
}

integration, ok := integrationCached.(modconfig.Integration)
if !ok {
return nil, perr.InternalWithMessage("invalid integration")
}
return integration, nil
}

func ListAllIntegrations() ([]modconfig.Integration, error) {
integrationNamesCached, found := cache.GetCache().Get("#integration.names")
if !found {
Expand Down Expand Up @@ -114,34 +122,6 @@ func ListAllNotifiers() ([]modconfig.Notifier, error) {
return notifiers, nil
}

func GetTrigger(name string) (*modconfig.Trigger, error) {
triggerCached, found := cache.GetCache().Get(name)
if !found {
return nil, perr.NotFoundWithMessage("trigger definition not found: " + name)
}

trigger, ok := triggerCached.(*modconfig.Trigger)
if !ok {
return nil, perr.InternalWithMessage("invalid trigger")
}

return trigger, nil
}

func GetNotifier(name string) (modconfig.Notifier, error) {
notifierCached, found := cache.GetCache().Get(name)
if !found {
return nil, perr.NotFoundWithMessage("notifier definition not found: " + name)
}

notifier, ok := notifierCached.(modconfig.Notifier)
if !ok {
return nil, perr.InternalWithMessage("invalid notifier")
}

return notifier, nil
}

func ListAllTriggers() ([]modconfig.Trigger, error) {

triggerNamesCached, found := cache.GetCache().Get("#trigger.names")
Expand Down

0 comments on commit 5d221fd

Please sign in to comment.