...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/msgdata/msgdata.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/msgdata

     1  package msgdata
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  )
     9  
    10  type CommandRequest interface {
    11  	Data() RequestData
    12  	Attributes() RequestAttributes
    13  }
    14  
    15  type GeneralAttributes struct {
    16  	BannerID   string `json:"bannerId"`
    17  	StoreID    string `json:"storeId"`
    18  	TerminalID string `json:"terminalId"`
    19  	SessionID  string `json:"sessionId"`
    20  	Identity   string `json:"identity"`
    21  	Version    string `json:"version"`
    22  	Signature  string `json:"signature"`
    23  }
    24  
    25  func newGeneralAttributes(psAttr map[string]string) (attr *GeneralAttributes, err error) {
    26  	if psAttr == nil {
    27  		return nil, fmt.Errorf("received nil map")
    28  	}
    29  	bytes, err := json.Marshal(psAttr)
    30  	if err != nil {
    31  		return nil, fmt.Errorf("unmarshaling Attributes: %w", err)
    32  	}
    33  	err = json.Unmarshal(bytes, &attr)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("unmarshaling Attributes: %w", err)
    36  	}
    37  	return attr, nil
    38  }
    39  
    40  func (attr *GeneralAttributes) validate(errs []error) []error {
    41  	if attr.BannerID == "" {
    42  		errs = append(errs, errors.New("attributes missing banner ID"))
    43  	}
    44  	if attr.StoreID == "" {
    45  		errs = append(errs, errors.New("attributes missing store ID"))
    46  	}
    47  	if attr.TerminalID == "" {
    48  		errs = append(errs, errors.New("attributes missing terminal ID"))
    49  	}
    50  	if attr.SessionID == "" {
    51  		errs = append(errs, errors.New("attributes missing session ID"))
    52  	}
    53  	if attr.Identity == "" {
    54  		errs = append(errs, errors.New("attributes missing identity"))
    55  	}
    56  	return errs
    57  }
    58  
    59  type RequestData struct {
    60  	Command string `json:"command"`
    61  }
    62  
    63  type commandRequest struct {
    64  	data       RequestData
    65  	attributes RequestAttributes
    66  }
    67  
    68  func (req *commandRequest) Data() RequestData {
    69  	data := req.data
    70  	return data
    71  }
    72  
    73  func (req *commandRequest) Attributes() RequestAttributes {
    74  	attr := req.attributes
    75  	return attr
    76  }
    77  
    78  func NewCommandRequest(command string, attrmap map[string]string) (CommandRequest, error) {
    79  	data := RequestData{
    80  		Command: command,
    81  	}
    82  	attr, err := newGeneralAttributes(attrmap)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	req := &commandRequest{data: data, attributes: RequestAttributes{GeneralAttributes: *attr,
    87  		CommandID: attrmap["commandId"]}}
    88  	if err = req.validate(); err != nil {
    89  		return nil, err
    90  	}
    91  	return req, nil
    92  }
    93  
    94  type CommandRequestErr []error
    95  
    96  func (myerr CommandRequestErr) Error() string {
    97  	var msg []string
    98  	for _, err := range myerr {
    99  		msg = append(msg, err.Error())
   100  	}
   101  	return strings.Join(msg, ", ")
   102  }
   103  
   104  func (req *commandRequest) validate() error {
   105  	errs := CommandRequestErr{}
   106  
   107  	if req.data.Command == "" {
   108  		errs = append(errs, errors.New("request missing command"))
   109  	}
   110  	if req.attributes.CommandID == "" {
   111  		errs = append(errs, errors.New("request missing command id"))
   112  	}
   113  	errs = req.attributes.validate(errs)
   114  
   115  	if len(errs) != 0 {
   116  		return errs
   117  	}
   118  	return nil
   119  }
   120  

View as plain text