github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/query.go (about)

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