Skip to content

Commit 43c0325

Browse files
committed
table: Implement email_localpart_optional module
1 parent 8083ffa commit 43c0325

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# Email local part
22

3-
The module 'table.email\_localpart' extracts and unescaped local ("username") part
3+
The module 'table.email\_localpart' extracts and unescapes local ("username") part
44
of the email address.
55

66
E.g.
77
test@example.org => test
88
"test @ a"@example.org => test @ a
99

10+
Mappings for invalid emails are not defined (will be treated as non-existing
11+
values).
12+
1013
```
1114
table.email_localpart { }
1215
```
16+
17+
`table.email_localpart_optional` works the same, but returns non-email strings
18+
as is. This can be used if you want to accept both `user@example.org` and
19+
`user` somewhere and treat it the same.

internal/table/email_localpart.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ import (
2727
)
2828

2929
type EmailLocalpart struct {
30-
modName string
31-
instName string
30+
modName string
31+
instName string
32+
allowNonEmail bool
3233
}
3334

3435
func NewEmailLocalpart(modName, instName string, _, _ []string) (module.Module, error) {
3536
return &EmailLocalpart{
36-
modName: modName,
37-
instName: instName,
37+
modName: modName,
38+
instName: instName,
39+
allowNonEmail: modName == "table.email_localpart_optional",
3840
}, nil
3941
}
4042

@@ -53,6 +55,9 @@ func (s *EmailLocalpart) InstanceName() string {
5355
func (s *EmailLocalpart) Lookup(ctx context.Context, key string) (string, bool, error) {
5456
mbox, _, err := address.Split(key)
5557
if err != nil {
58+
if s.allowNonEmail {
59+
return key, true, nil
60+
}
5661
// Invalid email, no local part mapping.
5762
return "", false, nil
5863
}
@@ -61,4 +66,5 @@ func (s *EmailLocalpart) Lookup(ctx context.Context, key string) (string, bool,
6166

6267
func init() {
6368
module.Register("table.email_localpart", NewEmailLocalpart)
69+
module.Register("table.email_localpart_optional", NewEmailLocalpart)
6470
}

0 commit comments

Comments
 (0)