github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/filters/strat.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package filters contains core implementations of the ComponentFilterStrategy interface.
     5  package filters
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/Racer159/jackal/src/types"
    11  )
    12  
    13  // ComponentFilterStrategy is a strategy interface for filtering components.
    14  type ComponentFilterStrategy interface {
    15  	Apply(types.JackalPackage) ([]types.JackalComponent, error)
    16  }
    17  
    18  // comboFilter is a filter that applies a sequence of filters.
    19  type comboFilter struct {
    20  	filters []ComponentFilterStrategy
    21  }
    22  
    23  // Apply applies the filter.
    24  func (f *comboFilter) Apply(pkg types.JackalPackage) ([]types.JackalComponent, error) {
    25  	result := pkg
    26  
    27  	for _, filter := range f.filters {
    28  		components, err := filter.Apply(result)
    29  		if err != nil {
    30  			return nil, fmt.Errorf("error applying filter %T: %w", filter, err)
    31  		}
    32  		result.Components = components
    33  	}
    34  
    35  	return result.Components, nil
    36  }
    37  
    38  // Combine creates a new filter that applies a sequence of filters.
    39  func Combine(filters ...ComponentFilterStrategy) ComponentFilterStrategy {
    40  	return &comboFilter{filters}
    41  }