Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: service/mail/message.go

Issue 1525923002: Implement Mail service. (Closed) Base URL: https://github.com/luci/gae.git@filter_user
Patch Set: add tests Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
martiniss 2015/12/16 21:54:15 Couple simple tests for this? Just to get some bas
iannucci 2015/12/17 00:01:07 oh, right, forgot I had code here.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package mail
6
7 import (
8 "fmt"
9 net_mail "net/mail"
10 "reflect"
11
12 "google.golang.org/appengine/mail"
13 )
14
15 // Attachment is a mimic of https://godoc.org/google.golang.org/appengine/mail#A ttachment
16 //
17 // It's provided here for convenience, and is compile-time checked to be
18 // identical.
19 type Attachment struct {
20 // Name must be set to a valid file name.
21 Name string
22 Data []byte
23 ContentID string
24 }
25
26 var _ Attachment = (Attachment)(mail.Attachment{})
27
28 // Message is a mimic of https://godoc.org/google.golang.org/appengine/mail#Mess age
29 //
30 // It's provided here for convenience, and is init-time checked to be identical.
31 type Message struct {
32 Sender string
33 ReplyTo string
34 To, Cc, Bcc []string
35 Subject string
36 Body string
37 HTMLBody string
38 Attachments []Attachment
39 Headers net_mail.Header
40 }
41
42 func init() {
43 mt := reflect.TypeOf(mail.Message{})
44 ot := reflect.TypeOf(Message{})
45 if mt.NumField() != ot.NumField() {
46 panic(fmt.Errorf("mismatched number of fields %s v %s", mt, ot))
47 }
48
49 for i := 0; i < mt.NumField(); i++ {
50 mf := mt.Field(i)
51 of := mt.Field(i)
52 if mf.Name != of.Name {
53 panic(fmt.Errorf("mismatched names %s v %s", mf.Name, of .Name))
54 }
55 if mf.Name == "Attachments" {
56 if !mf.Type.Elem().ConvertibleTo(of.Type.Elem()) {
57 panic(fmt.Errorf("mismatched element type for At tachments %s v %s",
58 mf.Type, of.Type))
59 }
60 } else {
61 if mf.Type != of.Type {
62 panic(fmt.Errorf("mismatched type for field %s: %s v %s", mf.Name, mf.Type, of.Type))
63 }
64 }
65 }
66 }
67
68 func dupStrs(strs []string) []string {
69 if len(strs) > 0 {
70 ret := make([]string, len(strs))
71 copy(ret, strs)
72 return ret
73 }
74 return nil
75 }
76
77 // ToSDKMessage returns a copy of this Message that's compatible with the native
78 // SDK's Message type.
79 func (m *Message) ToSDKMessage() *mail.Message {
80 if m == nil {
81 return nil
82 }
83
84 m = m.Copy()
85
86 ret := &mail.Message{
87 Sender: m.Sender,
88 ReplyTo: m.ReplyTo,
89 Subject: m.Subject,
90 Body: m.Body,
91 HTMLBody: m.HTMLBody,
92 To: m.To,
93 Cc: m.Cc,
94 Bcc: m.Bcc,
95 Headers: m.Headers,
96 }
97
98 ret.Attachments = make([]mail.Attachment, len(m.Attachments))
99 for i := range m.Attachments {
100 ret.Attachments[i] = (mail.Attachment)(m.Attachments[i])
101 }
102 return ret
103 }
104
105 // Copy returns a duplicate Message
106 func (m *Message) Copy() *Message {
107 if m == nil {
108 return nil
109 }
110
111 ret := *m
112
113 ret.To = dupStrs(m.To)
114 ret.Cc = dupStrs(m.Cc)
115 ret.Bcc = dupStrs(m.Bcc)
116
117 if len(m.Headers) > 0 {
118 ret.Headers = make(net_mail.Header, len(m.Headers))
119 for k, vals := range m.Headers {
120 ret.Headers[k] = dupStrs(vals)
121 }
122 }
123
124 if len(m.Attachments) > 0 {
125 ret.Attachments = make([]Attachment, len(m.Attachments))
126 copy(ret.Attachments, m.Attachments)
127 }
128
129 return &ret
130 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698