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