gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/SimpleRequest.go (about)

     1  package coredynamic
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  	"gitlab.com/evatix-go/core/errcore"
     9  	"gitlab.com/evatix-go/core/internal/strutilinternal"
    10  	"gitlab.com/evatix-go/core/issetter"
    11  )
    12  
    13  type SimpleRequest struct {
    14  	Dynamic
    15  	message string
    16  	err     error
    17  }
    18  
    19  func InvalidSimpleRequestNoMessage() *SimpleRequest {
    20  	return &SimpleRequest{
    21  		Dynamic: NewDynamic(nil, false),
    22  		message: constants.EmptyString,
    23  	}
    24  }
    25  
    26  func InvalidSimpleRequest(
    27  	message string,
    28  ) *SimpleRequest {
    29  	return &SimpleRequest{
    30  		Dynamic: NewDynamic(nil, false),
    31  		message: message,
    32  	}
    33  }
    34  
    35  func NewSimpleRequest(
    36  	request interface{},
    37  	isValid bool,
    38  	message string,
    39  ) *SimpleRequest {
    40  	return &SimpleRequest{
    41  		Dynamic: NewDynamic(request, isValid),
    42  		message: message,
    43  	}
    44  }
    45  
    46  func NewSimpleRequestValid(
    47  	request interface{},
    48  ) *SimpleRequest {
    49  	return &SimpleRequest{
    50  		Dynamic: NewDynamic(request, true),
    51  		message: constants.EmptyString,
    52  	}
    53  }
    54  
    55  func (receiver *SimpleRequest) Message() string {
    56  	return receiver.message
    57  }
    58  
    59  func (receiver *SimpleRequest) Request() interface{} {
    60  	return receiver.Dynamic.Data()
    61  }
    62  
    63  func (receiver *SimpleRequest) Value() interface{} {
    64  	return receiver.Dynamic.Data()
    65  }
    66  
    67  func (receiver *SimpleRequest) GetErrorOnTypeMismatch(
    68  	typeMatch reflect.Type,
    69  	isIncludeInvalidMessage bool,
    70  ) error {
    71  	if receiver.IsReflectTypeOf(typeMatch) {
    72  		return nil
    73  	}
    74  
    75  	typeMismatchMessage := errcore.CombineWithMsgType(
    76  		errcore.TypeMismatchType,
    77  		"Current type - ["+receiver.ReflectTypeName()+"], expected type",
    78  		typeMatch) + constants.NewLineUnix
    79  
    80  	if !isIncludeInvalidMessage {
    81  		return errors.New(typeMismatchMessage)
    82  	}
    83  
    84  	return errors.New(typeMismatchMessage + receiver.message)
    85  }
    86  
    87  func (receiver *SimpleRequest) IsReflectKind(checkingKind reflect.Kind) bool {
    88  	return receiver.ReflectKind() == checkingKind
    89  }
    90  
    91  func (receiver *SimpleRequest) IsPointer() bool {
    92  	if receiver.isPointer.IsUninitialized() {
    93  		receiver.isPointer = issetter.GetBool(
    94  			receiver.IsReflectKind(reflect.Ptr))
    95  	}
    96  
    97  	return receiver.isPointer.IsTrue()
    98  }
    99  
   100  func (receiver *SimpleRequest) InvalidError() error {
   101  	if receiver.err != nil {
   102  		return receiver.err
   103  	}
   104  
   105  	if strutilinternal.IsEmptyOrWhitespace(receiver.message) {
   106  		return nil
   107  	}
   108  
   109  	if receiver.err == nil {
   110  		receiver.err = errors.New(receiver.message)
   111  	}
   112  
   113  	return receiver.err
   114  }