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

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: tests and words 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
« no previous file with comments | « service/mail/interface.go ('k') | service/mail/message_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
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 // (not statically because []Attachement prevents static casting).
32 type Message struct {
33 Sender string
34 ReplyTo string
35 To, Cc, Bcc []string
36 Subject string
37 Body string
38 HTMLBody string
39 Attachments []Attachment
40 Headers net_mail.Header
41 }
42
43 func init() {
44 mt := reflect.TypeOf(mail.Message{})
45 ot := reflect.TypeOf(Message{})
46 if mt.NumField() != ot.NumField() {
47 panic(fmt.Errorf("mismatched number of fields %s v %s", mt, ot))
48 }
49
50 for i := 0; i < mt.NumField(); i++ {
51 mf := mt.Field(i)
52 of := mt.Field(i)
53 if mf.Name != of.Name {
54 panic(fmt.Errorf("mismatched names %s v %s", mf.Name, of .Name))
55 }
56 if mf.Name == "Attachments" {
57 if !mf.Type.Elem().ConvertibleTo(of.Type.Elem()) {
58 panic(fmt.Errorf("mismatched element type for At tachments %s v %s",
59 mf.Type, of.Type))
60 }
61 } else {
62 if mf.Type != of.Type {
63 panic(fmt.Errorf("mismatched type for field %s: %s v %s", mf.Name, mf.Type, of.Type))
64 }
65 }
66 }
67 }
68
69 func dupStrs(strs []string) []string {
70 if len(strs) > 0 {
71 ret := make([]string, len(strs))
72 copy(ret, strs)
73 return ret
74 }
75 return nil
76 }
77
78 // ToSDKMessage returns a copy of this Message that's compatible with the native
79 // SDK's Message type. It only needs to be used by implementations (like
80 // "impl/prod") which need an SDK compatible object
81 func (m *Message) ToSDKMessage() *mail.Message {
82 if m == nil {
83 return nil
84 }
85
86 m = m.Copy()
87
88 ret := &mail.Message{
89 Sender: m.Sender,
90 ReplyTo: m.ReplyTo,
91 Subject: m.Subject,
92 Body: m.Body,
93 HTMLBody: m.HTMLBody,
94 To: m.To,
95 Cc: m.Cc,
96 Bcc: m.Bcc,
97 Headers: m.Headers,
98 }
99
100 ret.Attachments = make([]mail.Attachment, len(m.Attachments))
101 for i := range m.Attachments {
102 ret.Attachments[i] = (mail.Attachment)(m.Attachments[i])
103 }
104 return ret
105 }
106
107 // Copy returns a duplicate Message
108 func (m *Message) Copy() *Message {
109 if m == nil {
110 return nil
111 }
112
113 ret := *m
114
115 ret.To = dupStrs(m.To)
116 ret.Cc = dupStrs(m.Cc)
117 ret.Bcc = dupStrs(m.Bcc)
118
119 if len(m.Headers) > 0 {
120 ret.Headers = make(net_mail.Header, len(m.Headers))
121 for k, vals := range m.Headers {
122 ret.Headers[k] = dupStrs(vals)
123 }
124 }
125
126 if len(m.Attachments) > 0 {
127 ret.Attachments = make([]Attachment, len(m.Attachments))
128 copy(ret.Attachments, m.Attachments)
129 }
130
131 return &ret
132 }
OLDNEW
« no previous file with comments | « service/mail/interface.go ('k') | service/mail/message_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698