Skip to content

Commit

Permalink
Refactoring to types
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Feb 7, 2024
1 parent 2ff0613 commit 7765f6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
30 changes: 7 additions & 23 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func parserFilterLoopForBinaryPayloads(insights InsightsData, p string, h *Messa

func parserFilterLoopForPayloads(insights InsightsData, p PayloadInput, h *Messaging, apiClient graphql.Client, resource ResourceDestination) error {
for _, filter := range parserFilters {
var result []interface{}
var result []LagoonFact
var source string

json, err := json.Marshal(p)
Expand All @@ -353,7 +353,7 @@ func parserFilterLoopForPayloads(insights InsightsData, p PayloadInput, h *Messa
}

// processResultset will send results as facts to the lagoon api after processing via a parser filter
func processResultset(result []interface{}, err error, h *Messaging, apiClient graphql.Client, resource ResourceDestination, source string) error {
func processResultset(result []LagoonFact, err error, h *Messaging, apiClient graphql.Client, resource ResourceDestination, source string) error {
project, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource)
if apiErr != nil {
slog.Error(apiErr.Error())
Expand All @@ -368,28 +368,12 @@ func processResultset(result []interface{}, err error, h *Messaging, apiClient g
return apiErr
}

for _, r := range result {
if fact, ok := r.(LagoonFact); ok {
// Handle single fact
err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source)
if err != nil {
slog.Error("Error sending facts to Lagoon API", "error", err.Error())
return err
}
} else if facts, ok := r.([]LagoonFact); ok {
// Handle slice of facts
err = h.sendFactsToLagoonAPI(facts, apiClient, resource, source)
if err != nil {
slog.Error("Error sending facts to Lagoon API", "error", err.Error())
return err
}
} else {
// Unexpected type returned from filter()
err := fmt.Errorf("unexpected type returned from filter(): %T\n", r)
slog.Error(err.Error())
return err
}
err = h.sendFactsToLagoonAPI(result, apiClient, resource, source)
if err != nil {
slog.Error("Error sending facts to Lagoon API", "error", err.Error())
return err
}

return nil
}

Expand Down
20 changes: 5 additions & 15 deletions internal/handler/parserfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,16 @@ type parserFilter interface {
getFact() LagoonFact
}

type ParserFilterFunc[T any] func(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]T, string, error)
type ParserFilterFunc func(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error)

var parserFilters []ParserFilterFunc[interface{}]
var parserFilters []ParserFilterFunc

// Since Go does not allow type conversions between slices of different types, ([]T and []interface{}) are considered different types, and you cannot assign one to the other.
// Therefore this func will convert the []T slice to a []interface{} slice
func ToInterfaceSlice[T any](slice []T) []interface{} {
result := make([]interface{}, len(slice))
for i, v := range slice {
result[i] = v
}
return result
}

func RegisterParserFilter[T any](pf ParserFilterFunc[T]) {
parserFilters = append(parserFilters, func(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]interface{}, string, error) {
func RegisterParserFilter(pf ParserFilterFunc) {
parserFilters = append(parserFilters, func(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error) {
result, source, err := pf(h, insights, v, apiClient, resource)
if err != nil {
return nil, "", err
}
return ToInterfaceSlice(result), source, nil
return result, source, nil
})
}

0 comments on commit 7765f6d

Please sign in to comment.