github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/presentation/mailboxvalidator/dep_view.go (about) 1 package mailboxvalidator 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/go-email-validator/go-email-validator/pkg/ev" 7 "github.com/go-email-validator/go-email-validator/pkg/ev/evmail" 8 "github.com/go-email-validator/go-email-validator/pkg/presentation/converter" 9 "time" 10 ) 11 12 // DepPresentationForView is the DepPresentation but all fields are string 13 type DepPresentationForView struct { 14 EmailAddress string `json:"email_address"` 15 Domain string `json:"domain"` 16 IsFree string `json:"is_free"` 17 IsSyntax string `json:"is_syntax"` 18 IsDomain string `json:"is_domain"` 19 IsSMTP string `json:"is_smtp"` 20 IsVerified string `json:"is_verified"` 21 IsServerDown string `json:"is_server_down"` 22 IsGreylisted string `json:"is_greylisted"` 23 IsDisposable string `json:"is_disposable"` 24 IsSuppressed string `json:"is_suppressed"` 25 IsRole string `json:"is_role"` 26 IsHighRisk string `json:"is_high_risk"` 27 IsCatchall string `json:"is_catchall"` 28 MailboxvalidatorScore string `json:"mailboxvalidator_score"` 29 TimeTaken string `json:"time_taken"` 30 Status string `json:"status"` 31 CreditsAvailable uint32 `json:"credits_available"` 32 ErrorCode string `json:"error_code"` 33 ErrorMessage string `json:"error_message"` 34 } 35 36 // mailbox return time_taken equals 0 for empty email 37 type jsonAlias DepPresentationForView 38 type timeTakenFloat struct { 39 TimeTaken float64 `json:"time_taken"` 40 } 41 42 // UnmarshalJSON is an implementation of Unmarshaler.UnmarshalJSON 43 func (d *DepPresentationForView) UnmarshalJSON(data []byte) error { 44 var err error 45 46 aux := (*jsonAlias)(d) 47 if err = json.Unmarshal(data, &aux); err == nil { 48 return nil 49 } 50 51 if errType := err.(*json.UnmarshalTypeError); errType.Field != "time_taken" { 52 return err 53 } 54 55 timeTakenStruct := timeTakenFloat{} 56 57 if err = json.Unmarshal(data, &timeTakenStruct); err != nil { 58 return err 59 } 60 d.TimeTaken = fmt.Sprint(timeTakenStruct.TimeTaken) 61 62 return nil 63 } 64 65 // NewDepConverterForViewDefault creates default DepConverterForView 66 func NewDepConverterForViewDefault() DepConverterForView { 67 return NewDepConverterForView(NewDepConverterDefault()) 68 } 69 70 // NewDepConverterForView creates DepConverterForView 71 func NewDepConverterForView(depConverter DepConverter) DepConverterForView { 72 return DepConverterForView{depConverter} 73 } 74 75 // DepConverterForView is the converter for mailbox 76 type DepConverterForView struct { 77 d DepConverter 78 } 79 80 // Can be used to convert ev.ValidationResult in DepConverterForView 81 func (d DepConverterForView) Can(email evmail.Address, result ev.ValidationResult, opts converter.Options) bool { 82 return d.d.Can(email, result, opts) 83 } 84 85 /* 86 Convert converts the result in mailboxvalidator presentation 87 TODO add processing of "-" in mailbox validator, for example zxczxczxc@joycasinoru 88 */ 89 func (d DepConverterForView) Convert(email evmail.Address, resultInterface ev.ValidationResult, opts converter.Options) interface{} { 90 depPresentation := d.d.Convert(email, resultInterface, opts).(DepPresentation) 91 92 return DepPresentationForView{ 93 EmailAddress: depPresentation.EmailAddress, 94 Domain: depPresentation.Domain, 95 IsFree: depPresentation.IsFree.ToString(), 96 IsSyntax: depPresentation.IsSyntax.ToString(), 97 IsDomain: depPresentation.IsDomain.ToString(), 98 IsSMTP: depPresentation.IsSMTP.ToString(), 99 IsVerified: depPresentation.IsVerified.ToString(), 100 IsServerDown: depPresentation.IsServerDown.ToString(), 101 IsGreylisted: depPresentation.IsGreylisted.ToString(), 102 IsDisposable: depPresentation.IsDisposable.ToString(), 103 IsSuppressed: depPresentation.IsSuppressed.ToString(), 104 IsRole: depPresentation.IsRole.ToString(), 105 IsHighRisk: depPresentation.IsHighRisk.ToString(), 106 IsCatchall: depPresentation.IsCatchall.ToString(), 107 MailboxvalidatorScore: fmt.Sprint(depPresentation.MailboxvalidatorScore), 108 TimeTaken: fmt.Sprint(depPresentation.TimeTaken.Round(time.Microsecond).Seconds()), 109 Status: depPresentation.Status.ToString(), 110 CreditsAvailable: depPresentation.CreditsAvailable, 111 ErrorCode: depPresentation.ErrorCode, 112 ErrorMessage: depPresentation.ErrorMessage, 113 } 114 }