github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv2/query.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  )
     8  
     9  // QueryFilter is the type of filter a Query uses.
    10  type QueryFilter string
    11  
    12  // QueryOperator is the type of operation a Query uses.
    13  type QueryOperator string
    14  
    15  const (
    16  	// AppGUIDFilter is the name of the 'app_guid' filter.
    17  	AppGUIDFilter QueryFilter = "app_guid"
    18  	// DomainGUIDFilter is the name of the 'domain_guid' filter.
    19  	DomainGUIDFilter QueryFilter = "domain_guid"
    20  	// OrganizationGUIDFilter is the name of the 'organization_guid' filter.
    21  	OrganizationGUIDFilter QueryFilter = "organization_guid"
    22  	// RouteGUIDFilter is the name of the 'route_guid' filter.
    23  	RouteGUIDFilter QueryFilter = "route_guid"
    24  	// ServiceInstanceGUIDFilter is the name of the 'service_instance_guid' filter.
    25  	ServiceInstanceGUIDFilter QueryFilter = "service_instance_guid"
    26  	// SpaceGUIDFilter is the name of the 'space_guid' filter.
    27  	SpaceGUIDFilter QueryFilter = "space_guid"
    28  
    29  	// NameFilter is the name of the 'name' filter.
    30  	NameFilter QueryFilter = "name"
    31  	// HostFilter is the name of the 'host' filter.
    32  	HostFilter QueryFilter = "host"
    33  )
    34  
    35  const (
    36  	// EqualOperator is the query equal operator.
    37  	EqualOperator QueryOperator = ":"
    38  
    39  	// InOperator is the query "IN" operator.
    40  	InOperator QueryOperator = " IN "
    41  )
    42  
    43  // Query is a type of filter that can be passed to specific request to narrow
    44  // down the return set.
    45  type Query struct {
    46  	Filter   QueryFilter
    47  	Operator QueryOperator
    48  	Values   []string
    49  }
    50  
    51  func (query Query) format() string {
    52  	return fmt.Sprintf("%s%s%s", query.Filter, query.Operator, strings.Join(query.Values, ","))
    53  }
    54  
    55  // FormatQueryParameters converts a Query object into a collection that
    56  // cloudcontroller.Request can accept.
    57  func FormatQueryParameters(queries []Query) url.Values {
    58  	params := url.Values{"q": []string{}}
    59  	for _, query := range queries {
    60  		params["q"] = append(params["q"], query.format())
    61  	}
    62  
    63  	return params
    64  }