🏷️ Added custom JSON (un)marshler for Email domain
This commit is contained in:
parent
f5888c3056
commit
6d5e90b852
|
@ -1,6 +1,8 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -37,6 +39,27 @@ func ParseEmail(src string) (*Email, error) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements custom unmarshler for JSON.
|
||||||
|
func (e *Email) UnmarshalJSON(v []byte) error {
|
||||||
|
src, err := strconv.Unquote(string(v))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Email: UnmarshalJSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
email, err := ParseEmail(src)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Email: UnmarshalJSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
*e = *email
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Email) MarshalJSON() ([]byte, error) {
|
||||||
|
return []byte(strconv.Quote(e.String())), nil
|
||||||
|
}
|
||||||
|
|
||||||
// TestEmail returns valid random generated email identifier.
|
// TestEmail returns valid random generated email identifier.
|
||||||
func TestEmail(tb testing.TB) *Email {
|
func TestEmail(tb testing.TB) *Email {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
Loading…
Reference in New Issue