dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/systemevent.go (about)

     1  /*******************************************************************************
     2   * Copyright 2022 Intel Corp.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
     5   * in compliance with the License. You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software distributed under the License
    10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    11   * or implied. See the License for the specific language governing permissions and limitations under
    12   * the License.
    13   *******************************************************************************/
    14  
    15  package dtos
    16  
    17  import (
    18  	"encoding/json"
    19  	"errors"
    20  	"fmt"
    21  	"time"
    22  
    23  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    24  )
    25  
    26  // SystemEvent defines the data for a system event
    27  type SystemEvent struct {
    28  	common.Versionable `json:",inline"`
    29  	Type               string            `json:"type"`
    30  	Action             string            `json:"action"`
    31  	Source             string            `json:"source"`
    32  	Owner              string            `json:"owner"`
    33  	Tags               map[string]string `json:"tags"`
    34  	Details            any               `json:"details"`
    35  	Timestamp          int64             `json:"timestamp"`
    36  }
    37  
    38  // NewSystemEvent creates a new SystemEvent for the specified data
    39  func NewSystemEvent(eventType, action, source, owner string, tags map[string]string, details any) SystemEvent {
    40  	return SystemEvent{
    41  		Versionable: common.NewVersionable(),
    42  		Type:        eventType,
    43  		Action:      action,
    44  		Source:      source,
    45  		Owner:       owner,
    46  		Tags:        tags,
    47  		Details:     details,
    48  		Timestamp:   time.Now().UnixNano(),
    49  	}
    50  }
    51  
    52  // DecodeDetails decodes the details (any type) into the passed in object
    53  func (s *SystemEvent) DecodeDetails(details any) error {
    54  	if s.Details == nil {
    55  		return errors.New("unable to decode System Event details: Details are nil")
    56  	}
    57  
    58  	// Must encode the details to JSON since if the target SystemEvent was decoded from JSON the details are
    59  	// captured in a map[string]interface{}.
    60  	data, err := json.Marshal(s.Details)
    61  	if err != nil {
    62  		return fmt.Errorf("unable to encode System Event details to JSON: %s", err.Error())
    63  	}
    64  
    65  	err = json.Unmarshal(data, details)
    66  	if err != nil {
    67  		return fmt.Errorf("unable to decode System Event details from JSON: %s", err.Error())
    68  	}
    69  
    70  	return nil
    71  }