Files
edgevpn/pkg/hub/message.go
Ettore Di Giacinto f5ea51d82e 📝 Switch to Apachev2
As EdgeVPN can be used as library and it turns out to be quite useful, change license for follow up versions
to Apache so can be used as a library easilier.
2022-08-27 21:55:21 +00:00

64 lines
1.5 KiB
Go

/*
Copyright © 2021-2022 Ettore Di Giacinto <mudler@mocaccino.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hub
import "encoding/json"
// Message gets converted to/from JSON and sent in the body of pubsub messages.
type Message struct {
Message string
SenderID string
Annotations map[string]interface{}
}
type MessageOption func(cfg *Message) error
// Apply applies the given options to the config, returning the first error
// encountered (if any).
func (m *Message) Apply(opts ...MessageOption) error {
for _, opt := range opts {
if opt == nil {
continue
}
if err := opt(m); err != nil {
return err
}
}
return nil
}
func NewMessage(s string) *Message {
return &Message{Message: s}
}
func (m *Message) Copy() *Message {
copy := *m
return &copy
}
func (m *Message) WithMessage(s string) *Message {
copy := m.Copy()
copy.Message = s
return copy
}
func (m *Message) AnnotationsToObj(v interface{}) error {
blob, err := json.Marshal(m.Annotations)
if err != nil {
return err
}
return json.Unmarshal(blob, v)
}