fix: Function params involve heavy amount of copying (#622)

This commit is contained in:
Bo-Yi Wu
2021-08-02 14:07:30 +08:00
committed by GitHub
parent 6d65c1ea6e
commit 349c0c8c1d
21 changed files with 87 additions and 88 deletions

View File

@@ -142,7 +142,7 @@ func (p *PushNotification) IsTopic() bool {
}
// CheckMessage for check request message
func CheckMessage(req PushNotification) error {
func CheckMessage(req *PushNotification) error {
var msg string
// ignore send topic mesaage from FCM
@@ -195,7 +195,7 @@ func SetProxy(proxy string) error {
}
// CheckPushConf provide check your yml config.
func CheckPushConf(cfg config.ConfYaml) error {
func CheckPushConf(cfg *config.ConfYaml) error {
if !cfg.Ios.Enabled && !cfg.Android.Enabled && !cfg.Huawei.Enabled {
return errors.New("Please enable iOS, Android or Huawei config in yml config")
}
@@ -233,7 +233,7 @@ func CheckPushConf(cfg config.ConfYaml) error {
}
// SendNotification send notification
func SendNotification(req queue.QueuedMessage, cfg config.ConfYaml) (resp *ResponsePush, err error) {
func SendNotification(req queue.QueuedMessage, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
v, ok := req.(*PushNotification)
if !ok {
if err = json.Unmarshal(req.Bytes(), &v); err != nil {
@@ -243,11 +243,11 @@ func SendNotification(req queue.QueuedMessage, cfg config.ConfYaml) (resp *Respo
switch v.Platform {
case core.PlatFormIos:
resp, err = PushToIOS(*v, cfg)
resp, err = PushToIOS(v, cfg)
case core.PlatFormAndroid:
resp, err = PushToAndroid(*v, cfg)
resp, err = PushToAndroid(v, cfg)
case core.PlatFormHuawei:
resp, err = PushToHuawei(*v, cfg)
resp, err = PushToHuawei(v, cfg)
}
if cfg.Core.FeedbackURL != "" {
@@ -263,7 +263,7 @@ func SendNotification(req queue.QueuedMessage, cfg config.ConfYaml) (resp *Respo
}
// Run send notification
var Run = func(cfg config.ConfYaml) func(ctx context.Context, msg queue.QueuedMessage) error {
var Run = func(cfg *config.ConfYaml) func(ctx context.Context, msg queue.QueuedMessage) error {
return func(ctx context.Context, msg queue.QueuedMessage) error {
_, err := SendNotification(msg, cfg)
return err

View File

@@ -52,7 +52,7 @@ type Sound struct {
}
// InitAPNSClient use for initialize APNs Client.
func InitAPNSClient(cfg config.ConfYaml) error {
func InitAPNSClient(cfg *config.ConfYaml) error {
if cfg.Ios.Enabled {
var err error
var authKey *ecdsa.PrivateKey
@@ -141,7 +141,7 @@ func InitAPNSClient(cfg config.ConfYaml) error {
return nil
}
func newApnsClient(cfg config.ConfYaml, certificate tls.Certificate) (*apns2.Client, error) {
func newApnsClient(cfg *config.ConfYaml, certificate tls.Certificate) (*apns2.Client, error) {
var client *apns2.Client
if cfg.Ios.Production {
@@ -181,7 +181,7 @@ func newApnsClient(cfg config.ConfYaml, certificate tls.Certificate) (*apns2.Cli
return client, nil
}
func newApnsTokenClient(cfg config.ConfYaml, token *token.Token) (*apns2.Client, error) {
func newApnsTokenClient(cfg *config.ConfYaml, token *token.Token) (*apns2.Client, error) {
var client *apns2.Client
if cfg.Ios.Production {
@@ -217,7 +217,7 @@ func configureHTTP2ConnHealthCheck(h2Transport *http2.Transport) {
h2Transport.PingTimeout = 1 * time.Second
}
func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload.Payload {
func iosAlertDictionary(payload *payload.Payload, req *PushNotification) *payload.Payload {
// Alert dictionary
if len(req.Title) > 0 {
@@ -288,7 +288,7 @@ func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload
// GetIOSNotification use for define iOS notification.
// The iOS Notification Payload
// ref: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
func GetIOSNotification(req PushNotification) *apns2.Notification {
func GetIOSNotification(req *PushNotification) *apns2.Notification {
notification := &apns2.Notification{
ApnsID: req.ApnsID,
Topic: req.Topic,
@@ -371,7 +371,7 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
return notification
}
func getApnsClient(cfg config.ConfYaml, req PushNotification) (client *apns2.Client) {
func getApnsClient(cfg *config.ConfYaml, req *PushNotification) (client *apns2.Client) {
if req.Production {
client = ApnsClient.Production()
} else if req.Development {
@@ -387,7 +387,7 @@ func getApnsClient(cfg config.ConfYaml, req PushNotification) (client *apns2.Cli
}
// PushToIOS provide send notification to APNs server.
func PushToIOS(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
func PushToIOS(req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
logx.LogAccess.Debug("Start push notification for iOS")
var (

View File

@@ -59,7 +59,7 @@ func TestIOSNotificationStructure(t *testing.T) {
expectBadge := 0
message := "Welcome notification Server"
expiration := int64(time.Now().Unix())
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Expiration: &expiration,
@@ -124,7 +124,7 @@ func TestIOSSoundAndVolume(t *testing.T) {
test := "test"
message := "Welcome notification Server"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -175,7 +175,7 @@ func TestIOSSoundAndVolume(t *testing.T) {
assert.Equal(t, int64(1), soundCritical)
assert.Equal(t, "foobar", soundName)
req = PushNotification{
req = &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -202,7 +202,7 @@ func TestIOSSoundAndVolume(t *testing.T) {
assert.Equal(t, int64(3), soundCritical)
assert.Equal(t, "test", soundName)
req = PushNotification{
req = &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -227,7 +227,7 @@ func TestIOSSummaryArg(t *testing.T) {
test := "test"
message := "Welcome notification Server"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -261,7 +261,7 @@ func TestSendZeroValueForBadgeKey(t *testing.T) {
test := "test"
message := "Welcome notification Server"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -334,7 +334,7 @@ func TestCheckSilentNotification(t *testing.T) {
var dat map[string]interface{}
test := "test"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
CollapseID: test,
@@ -381,7 +381,7 @@ func TestAlertStringExample2ForIos(t *testing.T) {
title := "Game Request"
body := "Bob wants to play poker"
actionLocKey := "PLAY"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -424,7 +424,7 @@ func TestAlertStringExample3ForIos(t *testing.T) {
test := "test"
badge := 9
sound := "bingbong.aiff"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -455,7 +455,7 @@ func TestMessageAndTitle(t *testing.T) {
test := "test"
message := "Welcome notification Server"
title := "Welcome notification Server title"
req := PushNotification{
req := &PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
@@ -509,7 +509,7 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
var dat map[string]interface{}
test := "test"
req := PushNotification{
req := &PushNotification{
Message: "Welcome",
Title: test,
Alert: Alert{
@@ -725,7 +725,7 @@ func TestPushToIOS(t *testing.T) {
err = status.InitAppStatus(cfg)
assert.Nil(t, err)
req := PushNotification{
req := &PushNotification{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7", "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef1"},
Platform: 1,
Message: "Welcome",
@@ -745,19 +745,19 @@ func TestApnsHostFromRequest(t *testing.T) {
err = status.InitAppStatus(cfg)
assert.Nil(t, err)
req := PushNotification{
req := &PushNotification{
Production: true,
}
client := getApnsClient(cfg, req)
assert.Equal(t, apns2.HostProduction, client.Host)
req = PushNotification{
req = &PushNotification{
Development: true,
}
client = getApnsClient(cfg, req)
assert.Equal(t, apns2.HostDevelopment, client.Host)
req = PushNotification{}
req = &PushNotification{}
cfg.Ios.Production = true
client = getApnsClient(cfg, req)
assert.Equal(t, apns2.HostProduction, client.Host)

View File

@@ -13,7 +13,7 @@ import (
)
// InitFCMClient use for initialize FCM Client.
func InitFCMClient(cfg config.ConfYaml, key string) (*fcm.Client, error) {
func InitFCMClient(cfg *config.ConfYaml, key string) (*fcm.Client, error) {
var err error
if key == "" && cfg.Android.APIKey == "" {
@@ -35,7 +35,7 @@ func InitFCMClient(cfg config.ConfYaml, key string) (*fcm.Client, error) {
// GetAndroidNotification use for define Android notification.
// HTTP Connection Server Reference for Android
// https://firebase.google.com/docs/cloud-messaging/http-server-ref
func GetAndroidNotification(req PushNotification) *fcm.Message {
func GetAndroidNotification(req *PushNotification) *fcm.Message {
notification := &fcm.Message{
To: req.To,
Condition: req.Condition,
@@ -105,7 +105,7 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
}
// PushToAndroid provide send notification to Android server.
func PushToAndroid(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
func PushToAndroid(req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
logx.LogAccess.Debug("Start push notification for Android")
var (
@@ -231,7 +231,7 @@ Retry:
return
}
func logPush(cfg config.ConfYaml, status, token string, req PushNotification, err error) logx.LogPushEntry {
func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry {
return logx.LogPush(&logx.InputLog{
ID: req.ID,
Status: status,

View File

@@ -39,7 +39,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {
cfg.Android.Enabled = true
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
req := PushNotification{
req := &PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome",
@@ -59,7 +59,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
req := &PushNotification{
Tokens: []string{androidToken},
Platform: core.PlatFormAndroid,
Message: "Welcome",
@@ -76,7 +76,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
req := &PushNotification{
Tokens: []string{androidToken},
Platform: core.PlatFormAndroid,
Message: "Welcome",
@@ -94,7 +94,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
req := &PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome",
@@ -110,11 +110,10 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
}
func TestFCMMessage(t *testing.T) {
var req PushNotification
var err error
// the message must specify at least one registration ID
req = PushNotification{
req := &PushNotification{
Message: "Test",
Tokens: []string{},
}
@@ -123,7 +122,7 @@ func TestFCMMessage(t *testing.T) {
assert.Error(t, err)
// the token must not be empty
req = PushNotification{
req = &PushNotification{
Message: "Test",
Tokens: []string{""},
}
@@ -132,7 +131,7 @@ func TestFCMMessage(t *testing.T) {
assert.Error(t, err)
// ignore check token length if send topic message
req = PushNotification{
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
To: "/topics/foo-bar",
@@ -142,7 +141,7 @@ func TestFCMMessage(t *testing.T) {
assert.NoError(t, err)
// "condition": "'dogs' in topics || 'cats' in topics",
req = PushNotification{
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Condition: "'dogs' in topics || 'cats' in topics",
@@ -152,7 +151,7 @@ func TestFCMMessage(t *testing.T) {
assert.NoError(t, err)
// the message may specify at most 1000 registration IDs
req = PushNotification{
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Tokens: make([]string, 1001),
@@ -164,7 +163,7 @@ func TestFCMMessage(t *testing.T) {
// the message's TimeToLive field must be an integer
// between 0 and 2419200 (4 weeks)
timeToLive := uint(2419201)
req = PushNotification{
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
@@ -176,7 +175,7 @@ func TestFCMMessage(t *testing.T) {
// Pass
timeToLive = uint(86400)
req = PushNotification{
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
@@ -194,7 +193,7 @@ func TestCheckAndroidMessage(t *testing.T) {
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
timeToLive := uint(2419201)
req := PushNotification{
req := &PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome",
@@ -207,7 +206,7 @@ func TestCheckAndroidMessage(t *testing.T) {
func TestAndroidNotificationStructure(t *testing.T) {
test := "test"
timeToLive := uint(100)
req := PushNotification{
req := &PushNotification{
Tokens: []string{"a", "b"},
Message: "Welcome",
To: test,
@@ -250,7 +249,7 @@ func TestAndroidNotificationStructure(t *testing.T) {
assert.Equal(t, 2, notification.Data["b"])
// test empty body
req = PushNotification{
req = &PushNotification{
Tokens: []string{"a", "b"},
To: test,
Notification: &fcm.Notification{

View File

@@ -36,7 +36,7 @@ func GetPushClient(conf *c.Config) (*client.HMSClient, error) {
}
// InitHMSClient use for initialize HMS Client.
func InitHMSClient(cfg config.ConfYaml, appSecret, appID string) (*client.HMSClient, error) {
func InitHMSClient(cfg *config.ConfYaml, appSecret, appID string) (*client.HMSClient, error) {
if appSecret == "" {
return nil, errors.New("Missing Huawei App Secret")
}
@@ -66,7 +66,7 @@ func InitHMSClient(cfg config.ConfYaml, appSecret, appID string) (*client.HMSCli
// GetHuaweiNotification use for define HMS notification.
// HTTP Connection Server Reference for HMS
// https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-sendapi
func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error) {
func GetHuaweiNotification(req *PushNotification) (*model.MessageRequest, error) {
msgRequest := model.NewNotificationMsgRequest()
msgRequest.Message.Android = model.GetDefaultAndroid()
@@ -166,7 +166,7 @@ func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error)
}
// PushToHuawei provide send notification to Android server.
func PushToHuawei(req PushNotification, cfg config.ConfYaml) (resp *ResponsePush, err error) {
func PushToHuawei(req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
logx.LogAccess.Debug("Start push notification for Huawei")
var (