diff options
author | clsr <clsr@clsr.net> | 2017-09-01 16:11:42 +0200 |
---|---|---|
committer | clsr <clsr@clsr.net> | 2017-09-01 16:11:42 +0200 |
commit | 7e1f90f27d876db67158ca4787420ec30c18f86b (patch) | |
tree | 4526e5924c926af4814968a49c2dd7875fdf0dd0 /message.go | |
parent | ee431f1e85a8ee7d3c6e069c8858c671da1c2acd (diff) | |
download | cnp-go-7e1f90f27d876db67158ca4787420ec30c18f86b.tar.gz cnp-go-7e1f90f27d876db67158ca4787420ec30c18f86b.zip |
Distinguish between response length=0 and no length parameter
Response.Length returns -1 when the parameter is not present and 0
when it's set to 0.
Response.SetLength will only unset the parameter when given -1.
Diffstat (limited to 'message.go')
-rw-r--r-- | message.go | 44 |
1 files changed, 23 insertions, 21 deletions
@@ -114,22 +114,14 @@ func (msg *Message) TryComputeLength() bool { return true } -// SetLength sets the length header parameter to n. -func (msg *Message) SetLength(n int64) { - if n == 0 { - msg.SetParam("length", "") - } else { - setInt(msg, "length", n) - } +// Intent retrieves the message header intent. +func (msg *Message) Intent() string { + return msg.Header.Intent } -// Length gets the length header parameter (or 0 if it's not set or invalid). -func (msg *Message) Length() int64 { - n, err := getInt(msg, "length") - if err != nil { - return 0 - } - return n +// SetIntent sets the message header intent. +func (msg *Message) SetIntent(s string) { + msg.Header.Intent = s } // Param retrieves a header parameter. It performs no value validation. @@ -147,19 +139,29 @@ func (msg *Message) SetParam(key, value string) { } } -// Intent retrieves the message header intent. -func (msg *Message) Intent() string { - return msg.Header.Intent +// Length gets the length header parameter (or 0 if it's not set or invalid). +func (msg *Message) Length() int64 { + n, err := getInt(msg, "length", 0) + if err != nil { + return 0 + } + return n } -// SetIntent sets the message header intent. -func (msg *Message) SetIntent(s string) { - msg.Header.Intent = s +// SetLength sets the length header parameter to n. +// +// If negative or zero, the parameter is unset. +func (msg *Message) SetLength(n int64) { + if n <= 0 { + msg.SetParam("length", "") + } else { + setInt(msg, "length", n) + } } // Validate validates the message header parameter value format (length). func (msg *Message) Validate() error { - _, err := getInt(msg, "length") + _, err := getInt(msg, "length", 0) return err } |