github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/filter.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  )
    10  
    11  // Filter is a type of filter that can be passed to specific request to narrow
    12  // down the return set.
    13  type Filter struct {
    14  	// Type is the component that determines what the query is filtered by.
    15  	Type constant.FilterType
    16  
    17  	// Operator is the component that determines how the the query will be filtered.
    18  	Operator constant.FilterOperator
    19  
    20  	// Values is the component that determines what values are filtered.
    21  	Values []string
    22  }
    23  
    24  func (filter Filter) format() string {
    25  	return fmt.Sprintf("%s%s%s", filter.Type, filter.Operator, strings.Join(filter.Values, ","))
    26  }
    27  
    28  // ConvertFilterParameters converts a Filter object into a collection that
    29  // cloudcontroller.Request can accept.
    30  func ConvertFilterParameters(filters []Filter) url.Values {
    31  	params := url.Values{"q": []string{}}
    32  	for _, filter := range filters {
    33  		params["q"] = append(params["q"], filter.format())
    34  	}
    35  
    36  	return params
    37  }
    38  
    39  func FilterEqual(filterType constant.FilterType, value ...string) Filter {
    40  	return Filter{
    41  		Type:     filterType,
    42  		Values:   value,
    43  		Operator: constant.EqualOperator,
    44  	}
    45  }
    46  
    47  func FilterByName(value ...string) Filter {
    48  	return FilterEqual(constant.NameFilter, value...)
    49  }
    50  
    51  func FilterByOrg(value ...string) Filter {
    52  	return FilterEqual(constant.OrganizationGUIDFilter, value...)
    53  }
    54  
    55  func FilterBySpace(value ...string) Filter {
    56  	return FilterEqual(constant.SpaceGUIDFilter, value...)
    57  }