github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/presentation/check-if-email-exist/dep.go (about) 1 package checkifemailexist 2 3 import ( 4 "github.com/go-email-validator/go-email-validator/pkg/ev" 5 "github.com/go-email-validator/go-email-validator/pkg/ev/evmail" 6 "github.com/go-email-validator/go-email-validator/pkg/presentation/converter" 7 ) 8 9 // TODO create transfer from DepPresentationForView to DepPresentation 10 //go:generate go run cmd/dep_test_generator/gen.go 11 12 // Name of https://github.com/reacherhq/check-if-email-exists or https://reacher.email/ converter 13 const Name converter.Name = "CheckIfEmailExist" 14 15 type miscPresentation struct { 16 disposablePresentation 17 rolePresentation 18 } 19 20 // DepPresentation is representation of https://github.com/amaurymartiny/check-if-email-exists or https://reacher.email 21 type DepPresentation struct { 22 Input string `json:"input"` 23 IsReachable Availability `json:"is_reachable"` 24 Misc miscPresentation `json:"misc"` 25 MX mxPresentation `json:"mx"` 26 SMTP SMTPPresentation `json:"smtp"` 27 Syntax SyntaxPresentation `json:"syntax"` 28 Error string `json:"error"` 29 } 30 31 // DepConverter converts ev.ValidationResult in check-if-email-exists or https://reacher.email/ presentation 32 type DepConverter struct { 33 converter converter.CompositeConverter 34 calculateAvailability FuncAvailability 35 } 36 37 // NewDepConverterDefault creates a default DepConverter 38 func NewDepConverterDefault() DepConverter { 39 return NewDepConverter( 40 converter.NewCompositeConverter(converter.MapConverters{ 41 ev.RoleValidatorName: roleConverter{}, 42 ev.DisposableValidatorName: disposableConverter{}, 43 ev.MXValidatorName: mxConverter{}, 44 ev.SMTPValidatorName: converter.NewSMTPConverter(), 45 ev.SyntaxValidatorName: SyntaxConverter{}, 46 }), 47 CalculateAvailability, 48 ) 49 } 50 51 // NewDepConverter creates DepConverter 52 func NewDepConverter(converter converter.CompositeConverter, calculateAvailability FuncAvailability) DepConverter { 53 return DepConverter{converter, calculateAvailability} 54 } 55 56 // Can ev.ValidationResult be converted in DepPresentation 57 func (DepConverter) Can(_ evmail.Address, result ev.ValidationResult, _ converter.Options) bool { 58 return result.ValidatorName() == ev.DepValidatorName 59 } 60 61 // Convert ev.ValidationResult in DepPresentation 62 func (s DepConverter) Convert(email evmail.Address, result ev.ValidationResult, opts converter.Options) interface{} { 63 depPresentation := DepPresentation{ 64 Input: email.String(), 65 Misc: miscPresentation{}, 66 } 67 68 for _, validatorResult := range result.(ev.DepValidationResult).GetResults() { 69 if !s.converter.Can(email, validatorResult, opts) { 70 continue 71 } 72 73 switch v := s.converter.Convert(email, validatorResult, opts).(type) { 74 case rolePresentation: 75 depPresentation.Misc.rolePresentation = v 76 case disposablePresentation: 77 depPresentation.Misc.disposablePresentation = v 78 case mxPresentation: 79 depPresentation.MX = v 80 case converter.SMTPPresentation: 81 depPresentation.SMTP = SMTPPresentation{ 82 CanConnectSMTP: v.CanConnectSMTP, 83 HasFullInbox: v.HasFullInbox, 84 IsCatchAll: v.IsCatchAll, 85 IsDeliverable: v.IsDeliverable, 86 IsDisabled: v.IsDisabled, 87 } 88 case SyntaxPresentation: 89 depPresentation.Syntax = v 90 } 91 } 92 depPresentation.IsReachable = s.calculateAvailability(depPresentation) 93 94 return depPresentation 95 } 96 97 // NewDepValidator is the default validator for https://github.com/reacherhq/check-if-email-exists 98 func NewDepValidator(smtpValidator ev.Validator) ev.Validator { 99 builder := ev.NewDepBuilder(nil) 100 if smtpValidator == nil { 101 smtpValidator = builder.Get(ev.SMTPValidatorName) 102 } 103 104 return builder.Set( 105 ev.SyntaxValidatorName, 106 ev.NewSyntaxRegexValidator(nil), 107 ).Set( 108 ev.SMTPValidatorName, 109 smtpValidator, 110 ).Build() 111 }