package msgdata import ( "encoding/json" "errors" "fmt" "strings" ) type CommandResponse interface { json.Marshaler Data() ResponseData Attributes() ResponseAttributes } type ResponseData struct { Type string `json:"type"` ExitCode int `json:"exitCode"` Output string `json:"output"` TimeStamp string `json:"timestamp"` Duration float64 `json:"duration"` } type ResponseAttributes struct { GeneralAttributes ReqMsgID string `json:"request-message-uuid"` } type RequestAttributes struct { GeneralAttributes CommandID string `json:"commandId"` } type commandResponse struct { data ResponseData attributes ResponseAttributes } func (resp *commandResponse) Data() ResponseData { return resp.data } func (resp *commandResponse) Attributes() ResponseAttributes { return resp.attributes } func (resp *commandResponse) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Data ResponseData `json:"data"` Attributes ResponseAttributes `json:"attributes"` }{ Data: resp.data, Attributes: resp.attributes, }) } func NewCommandResponse(databytes []byte, attrmap map[string]string) (CommandResponse, error) { var data ResponseData err := json.Unmarshal(databytes, &data) if err != nil { return nil, fmt.Errorf("unmarshalling ResponseData: %w", err) } genAttr, err := newGeneralAttributes(attrmap) if err != nil { return nil, err } attr := ResponseAttributes{ GeneralAttributes: *genAttr, ReqMsgID: attrmap["request-message-uuid"], } resp := &commandResponse{data: data, attributes: attr} if err = resp.validate(); err != nil { return nil, err } return resp, nil } type CommandResponseErr []error func (myerr CommandResponseErr) Error() string { var msg []string for _, err := range myerr { msg = append(msg, err.Error()) } return strings.Join(msg, ", ") } func (resp *commandResponse) validate() error { errs := CommandResponseErr{} if resp.data.Type == "" { errs = append(errs, errors.New("response missing type field")) } if resp.attributes.ReqMsgID == "" { errs = append(errs, errors.New("response missing reqmsgid field")) } errs = resp.attributes.validate(errs) if len(errs) != 0 { return errs } return nil }