github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/expressionwatcher/state_notification.go (about)

     1  // Copyright (c) 2024, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package expressionwatcher
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/choria-io/go-choria/aagent/watchers/event"
    12  	cloudevents "github.com/cloudevents/sdk-go/v2"
    13  )
    14  
    15  // StateNotification describes the current state of the watcher
    16  // described by io.choria.machine.watcher.exec.v1.state
    17  type StateNotification struct {
    18  	event.Event
    19  
    20  	PreviousOutcome string `json:"previous_outcome"`
    21  }
    22  
    23  // CloudEvent creates a CloudEvent from the state notification
    24  func (s *StateNotification) CloudEvent() cloudevents.Event {
    25  	return s.Event.CloudEvent(s)
    26  }
    27  
    28  // JSON creates a JSON representation of the notification
    29  func (s *StateNotification) JSON() ([]byte, error) {
    30  	return json.Marshal(s.CloudEvent())
    31  }
    32  
    33  // String is a string representation of the notification suitable for printing
    34  func (s *StateNotification) String() string {
    35  	return fmt.Sprintf("%s %s#%s previous: %s", s.Identity, s.Machine, s.Name, s.PreviousOutcome)
    36  }