Repository: qpid-proton Updated Branches: refs/heads/master d58bd7154 -> be20a5186
PROTON-1826: [go] Add Messge.String() method for human-readable message Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/be20a518 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/be20a518 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/be20a518 Branch: refs/heads/master Commit: be20a5186790815372f06e9a855955e330863426 Parents: d58bd71 Author: Alan Conway <[email protected]> Authored: Wed Apr 11 12:00:59 2018 -0400 Committer: Alan Conway <[email protected]> Committed: Wed Apr 11 12:01:25 2018 -0400 ---------------------------------------------------------------------- go/src/qpid.apache.org/amqp/message.go | 11 +++++++- go/src/qpid.apache.org/amqp/message_test.go | 32 ++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/be20a518/go/src/qpid.apache.org/amqp/message.go ---------------------------------------------------------------------- diff --git a/go/src/qpid.apache.org/amqp/message.go b/go/src/qpid.apache.org/amqp/message.go index 389fa37..e514b26 100644 --- a/go/src/qpid.apache.org/amqp/message.go +++ b/go/src/qpid.apache.org/amqp/message.go @@ -38,6 +38,7 @@ import ( "fmt" "runtime" "time" + "unsafe" ) // Message is the interface to an AMQP message. @@ -174,6 +175,9 @@ type Message interface { // Deprecated: use ApplicationProperties() for a more type-safe interface Properties() map[string]interface{} SetProperties(v map[string]interface{}) + + // Human-readable string showing message contents and properties + String() string } type message struct{ pn *C.pn_message_t } @@ -378,7 +382,12 @@ func (m *message) Encode(buffer []byte) ([]byte, error) { // TODO aconway 2015-09-14: Multi-section messages. -// TODO aconway 2016-09-09: Message.String() use inspect. +func (m *message) String() string { + str := C.pn_string(C.CString("")) + defer C.pn_free(unsafe.Pointer(str)) + C.pn_inspect(unsafe.Pointer(m.pn), str) + return C.GoString(C.pn_string_get(str)) +} // ==== Deprecated functions func oldGetAnnotations(data *C.pn_data_t) (v map[string]interface{}) { http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/be20a518/go/src/qpid.apache.org/amqp/message_test.go ---------------------------------------------------------------------- diff --git a/go/src/qpid.apache.org/amqp/message_test.go b/go/src/qpid.apache.org/amqp/message_test.go index 3585dd8..663e82f 100644 --- a/go/src/qpid.apache.org/amqp/message_test.go +++ b/go/src/qpid.apache.org/amqp/message_test.go @@ -25,15 +25,15 @@ import ( ) func roundTrip(m Message) error { - buffer, err := m.Encode(nil) - if err != nil { - return err - } - m2, err := DecodeMessage(buffer) - if err != nil { - return err + var err error + if buffer, err := m.Encode(nil); err == nil { + if m2, err := DecodeMessage(buffer); err == nil { + if err = checkEqual(m, m2); err == nil { + err = checkEqual(m.String(), m2.String()) + } + } } - return checkEqual(m, m2) + return err } func TestDefaultMessage(t *testing.T) { @@ -72,6 +72,22 @@ func TestDefaultMessage(t *testing.T) { if err := roundTrip(m); err != nil { t.Error(err) } + if err := checkEqual("Message{}", m.String()); err != nil { + t.Error(err) + } +} + +func TestMessageString(t *testing.T) { + m := NewMessageWith("hello") + m.SetInferred(false) + m.SetUserId("user") + m.SetDeliveryAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("instructions"): "foo"}) + m.SetMessageAnnotations(map[AnnotationKey]interface{}{AnnotationKeySymbol("annotations"): "bar"}) + m.SetApplicationProperties(map[string]interface{}{"int": int32(32)}) + msgstr := `Message{user_id="user", instructions={:instructions="foo"}, annotations={:annotations="bar"}, properties={"int"=32}, body="hello"}` + if err := checkEqual(msgstr, m.String()); err != nil { + t.Error(err) + } } func TestMessageRoundTrip(t *testing.T) { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
