From commits-return-46053-archive-asf-public=cust-asf.ponee.io@qpid.apache.org Wed Jul 4 00:13:08 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 085B1180632 for ; Wed, 4 Jul 2018 00:13:04 +0200 (CEST) Received: (qmail 66556 invoked by uid 500); 3 Jul 2018 22:12:56 -0000 Mailing-List: contact commits-help@qpid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@qpid.apache.org Delivered-To: mailing list commits@qpid.apache.org Received: (qmail 63054 invoked by uid 99); 3 Jul 2018 22:12:52 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 03 Jul 2018 22:12:52 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7105AE11C2; Tue, 3 Jul 2018 22:12:51 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aconway@apache.org To: commits@qpid.apache.org Date: Tue, 03 Jul 2018 22:13:49 -0000 Message-Id: In-Reply-To: <506ba33a6fed44e08b20ccd012e637c7@git.apache.org> References: <506ba33a6fed44e08b20ccd012e637c7@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [60/89] [abbrv] qpid-proton git commit: PROTON-1826: [go] Add Messge.String() method for human-readable message 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/go1 Commit: be20a5186790815372f06e9a855955e330863426 Parents: d58bd71 Author: Alan Conway Authored: Wed Apr 11 12:00:59 2018 -0400 Committer: Alan Conway 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: commits-unsubscribe@qpid.apache.org For additional commands, e-mail: commits-help@qpid.apache.org