github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/validator_input.go (about)

     1  package ev
     2  
     3  import (
     4  	"github.com/go-email-validator/go-email-validator/pkg/ev/evmail"
     5  )
     6  
     7  // ValidatorName is type to represent validator name
     8  type ValidatorName string
     9  
    10  func (v ValidatorName) String() string {
    11  	return string(v)
    12  }
    13  
    14  // Input consists of input data for Validator.Validate
    15  type Input interface {
    16  	Email() evmail.Address
    17  	Option(name ValidatorName) interface{}
    18  }
    19  
    20  // NewInput create Input from evmail.Address and KVOption list
    21  func NewInput(email evmail.Address, kvOptions ...KVOption) Input {
    22  	var options = make(map[ValidatorName]interface{})
    23  
    24  	for _, kvOption := range kvOptions {
    25  		options[kvOption.Name] = kvOption.Option
    26  	}
    27  
    28  	return NewInputFromMap(email, options)
    29  }
    30  
    31  // NewInputFromMap create Input from evmail.Address and options
    32  func NewInputFromMap(email evmail.Address, options map[ValidatorName]interface{}) Input {
    33  	return &input{
    34  		email:   email,
    35  		options: options,
    36  	}
    37  }
    38  
    39  type input struct {
    40  	email   evmail.Address
    41  	options map[ValidatorName]interface{}
    42  }
    43  
    44  func (i *input) Email() evmail.Address {
    45  	return i.email
    46  }
    47  
    48  func (i *input) Option(name ValidatorName) interface{} {
    49  	return i.options[name]
    50  }
    51  
    52  // NewKVOption instantiates KVOption
    53  func NewKVOption(name ValidatorName, option interface{}) KVOption {
    54  	return KVOption{
    55  		Name:   name,
    56  		Option: option,
    57  	}
    58  }
    59  
    60  // KVOption needs to form options in Input
    61  type KVOption struct {
    62  	Name   ValidatorName
    63  	Option interface{}
    64  }