summaryrefslogtreecommitdiffstats
path: root/response.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-25 17:04:42 +0200
committerclsr <clsr@clsr.net>2017-08-25 17:04:44 +0200
commit9ca364d8753a5f2c7529c5b3dd7178bfd51effc6 (patch)
treedc5ab072ffbbd37da910d440e05b5bc56dc3f6da /response.go
parentba217ddeef9c6cd502a6f150bf59cf3ce329cc61 (diff)
downloadcnp-go-9ca364d8753a5f2c7529c5b3dd7178bfd51effc6.tar.gz
cnp-go-9ca364d8753a5f2c7529c5b3dd7178bfd51effc6.zip
Add support for the draft/cnp-select "select" request/response parameterv0.1.2
Diffstat (limited to 'response.go')
-rw-r--r--response.go37
1 files changed, 28 insertions, 9 deletions
diff --git a/response.go b/response.go
index bf23f20..3ecb961 100644
--- a/response.go
+++ b/response.go
@@ -76,7 +76,7 @@ func (r *Response) Name() string {
// SetName sets the name response parameter.
//
-// An error is raised if the name includes characters not valid in a filename
+// Returns an error if the name includes characters not valid in a filename
// (slash, null byte).
func (r *Response) SetName(name string) error {
return setFilename(&r.Message, "name", name)
@@ -93,7 +93,7 @@ func (r *Response) Type() string {
// SetType sets the type response parameter.
//
-// An error is raised if typ is not a valid format for a MIME type.
+// Returns an error if typ is not a valid format for a MIME type.
func (r *Response) SetType(typ string) error {
return setType(&r.Message, "type", typ)
}
@@ -131,11 +131,11 @@ func (r *Response) Modified() time.Time {
// SetModified sets the modified response parameter.
//
-// If the time response parameter is empty, it's set to the current time.
-// If t is the zero time value, the modified parameter is unset.
+// If t is the zero time value, the modified parameter is unset. Otherwise, if
+// the time response parameter is empty, it's set to the current time.
func (r *Response) SetModified(t time.Time) {
setTime(&r.Message, "modified", t)
- if r.Time().IsZero() {
+ if !t.IsZero() && r.Time().IsZero() {
r.SetTime(time.Now())
}
}
@@ -159,14 +159,15 @@ func (r *Response) Location() (host, path string, err error) {
// SetLocation sets the location response parameter to host and path.
//
-// If the host or path are invalid
+// Returns an error if the host or path are invalid.
func (r *Response) SetLocation(host, path string) error {
+ err := ErrorInvalid{"invalid response: invalid location parameter"}
if strings.ContainsRune(host, '/') {
- return ErrorInvalid{"invalid response: invalid location parameter"}
+ return err
}
l := host + path
if err := validateRequestIntent(l); err != nil {
- return ErrorInvalid{"invalid response: invalid location parameter"}
+ return err
}
r.SetParam("location", l)
return nil
@@ -208,8 +209,23 @@ func (r *Response) SetReason(reason string) error {
return nil
}
+// Select retrieves the select response parameter.
+//
+// If the parameter isn't a valid selector, empty strings are returned.
+func (r *Response) Select() (selector, query string) {
+ selector, query, _ = getSelect(&r.Message, "select")
+ return
+}
+
+// SetSelect sets the select response parameter.
+//
+// If the selector name is empty, the select parameter is unset.
+func (r *Response) SetSelect(selector, query string) error {
+ return setSelect(&r.Message, "select", selector, query)
+}
+
// Validate validates the response intent and header parameter value format
-// (length, name, type, time, modified, location, reason)
+// (length, name, type, time, modified, location, reason, select)
func (r *Response) Validate() error {
if !responseIntents[r.Intent()] {
return ErrorInvalid{"invalid response: unknown response intent"}
@@ -237,6 +253,9 @@ func (r *Response) Validate() error {
if !responseErrorReasons[r.Param("reason")] {
return ErrorInvalid{"invalid response: unknown error reason"}
}
+ if _, _, err := getSelect(&r.Message, "select"); err != nil {
+ return err
+ }
return nil
}