From 07a6af7c361921dcbd6272484edb1a97a30c816e Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Wed, 7 Feb 2018 10:01:45 -0800 Subject: [PATCH 1/2] Accepting prefixed topic named --- messaging/messaging.go | 15 ++++++++++++++- messaging/messaging_test.go | 11 ++++++++--- messaging/messaging_utils.go | 6 ++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/messaging/messaging.go b/messaging/messaging.go index d51c1662..41598f92 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -81,10 +81,23 @@ type Message struct { Webpush *WebpushConfig `json:"webpush,omitempty"` APNS *APNSConfig `json:"apns,omitempty"` Token string `json:"token,omitempty"` - Topic string `json:"topic,omitempty"` + Topic string `json:"-"` Condition string `json:"condition,omitempty"` } +// MarshalJSON marshals a Message into JSON (for internal use only). +func (m *Message) MarshalJSON() ([]byte, error) { + type messageInternal Message + s := &struct { + BareTopic string `json:"topic,omitempty"` + *messageInternal + }{ + BareTopic: strings.TrimPrefix(m.Topic, "/topics/"), + messageInternal: (*messageInternal)(m), + } + return json.Marshal(s) +} + // Notification is the basic notification template to use across all platforms. type Notification struct { Title string `json:"title,omitempty"` diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index fd06eac7..a41b04e8 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -63,6 +63,11 @@ var validMessages = []struct { req: &Message{Topic: "test-topic"}, want: map[string]interface{}{"topic": "test-topic"}, }, + { + name: "PrefixedTopicOnly", + req: &Message{Topic: "/topics/test-topic"}, + want: map[string]interface{}{"topic": "test-topic"}, + }, { name: "ConditionOnly", req: &Message{Condition: "test-condition"}, @@ -370,11 +375,11 @@ var invalidMessages = []struct { want: "exactly one of token, topic or condition must be specified", }, { - name: "InvalidTopicPrefix", + name: "InvalidPrefixedTopicName", req: &Message{ - Topic: "/topics/foo", + Topic: "/topics/", }, - want: "topic name must not contain the /topics/ prefix", + want: "malformed topic name", }, { name: "InvalidTopicName", diff --git a/messaging/messaging_utils.go b/messaging/messaging_utils.go index 2416f890..ffd4df95 100644 --- a/messaging/messaging_utils.go +++ b/messaging/messaging_utils.go @@ -37,10 +37,8 @@ func validateMessage(message *Message) error { // validate topic if message.Topic != "" { - if strings.HasPrefix(message.Topic, "/topics/") { - return fmt.Errorf("topic name must not contain the /topics/ prefix") - } - if !bareTopicNamePattern.MatchString(message.Topic) { + bt := strings.TrimPrefix(message.Topic, "/topics/") + if !bareTopicNamePattern.MatchString(bt) { return fmt.Errorf("malformed topic name") } } From 23280ed249df2cd97590794de0015568e0dce368 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Wed, 7 Feb 2018 11:08:49 -0800 Subject: [PATCH 2/2] Added a comment --- messaging/messaging.go | 1 + 1 file changed, 1 insertion(+) diff --git a/messaging/messaging.go b/messaging/messaging.go index 41598f92..527bae09 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -87,6 +87,7 @@ type Message struct { // MarshalJSON marshals a Message into JSON (for internal use only). func (m *Message) MarshalJSON() ([]byte, error) { + // Create a new type to prevent infinite recursion. type messageInternal Message s := &struct { BareTopic string `json:"topic,omitempty"`