|
| 1 | +/* |
| 2 | + Copyright 2024 The CloudEvents Authors |
| 3 | + SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package nats_jetstream |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "errors" |
| 12 | + "fmt" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/nats-io/nats.go/jetstream" |
| 16 | + |
| 17 | + "github.com/cloudevents/sdk-go/v2/binding" |
| 18 | + "github.com/cloudevents/sdk-go/v2/binding/format" |
| 19 | + "github.com/cloudevents/sdk-go/v2/binding/spec" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // see https://github.com/cloudevents/spec/blob/main/cloudevents/bindings/nats-protocol-binding.md |
| 24 | + prefix = "ce-" |
| 25 | + contentTypeHeader = "content-type" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + specs = spec.WithPrefix(prefix) |
| 30 | + |
| 31 | + // ErrNoVersion returned when no version header is found in the protocol header. |
| 32 | + ErrNoVersion = errors.New("message does not contain version header") |
| 33 | +) |
| 34 | + |
| 35 | +// Message implements binding.Message by wrapping an jetstream.Msg. |
| 36 | +// This message *can* be read several times safely |
| 37 | +type Message struct { |
| 38 | + Msg jetstream.Msg |
| 39 | + encoding binding.Encoding |
| 40 | +} |
| 41 | + |
| 42 | +// NewMessage wraps an *nats.Msg in a binding.Message. |
| 43 | +// The returned message *can* be read several times safely |
| 44 | +// The default encoding returned is EncodingStructured unless the NATS message contains a specversion header. |
| 45 | +func NewMessage(msg jetstream.Msg) *Message { |
| 46 | + encoding := binding.EncodingStructured |
| 47 | + if msg.Headers() != nil { |
| 48 | + if msg.Headers().Get(specs.PrefixedSpecVersionName()) != "" { |
| 49 | + encoding = binding.EncodingBinary |
| 50 | + } |
| 51 | + } |
| 52 | + return &Message{Msg: msg, encoding: encoding} |
| 53 | +} |
| 54 | + |
| 55 | +var _ binding.Message = (*Message)(nil) |
| 56 | + |
| 57 | +// ReadEncoding return the type of the message Encoding. |
| 58 | +func (m *Message) ReadEncoding() binding.Encoding { |
| 59 | + return m.encoding |
| 60 | +} |
| 61 | + |
| 62 | +// ReadStructured transfers a structured-mode event to a StructuredWriter. |
| 63 | +func (m *Message) ReadStructured(ctx context.Context, encoder binding.StructuredWriter) error { |
| 64 | + if m.encoding != binding.EncodingStructured { |
| 65 | + return binding.ErrNotStructured |
| 66 | + } |
| 67 | + return encoder.SetStructuredEvent(ctx, format.JSON, bytes.NewReader(m.Msg.Data())) |
| 68 | +} |
| 69 | + |
| 70 | +// ReadBinary transfers a binary-mode event to an BinaryWriter. |
| 71 | +func (m *Message) ReadBinary(ctx context.Context, encoder binding.BinaryWriter) error { |
| 72 | + if m.encoding != binding.EncodingBinary { |
| 73 | + return binding.ErrNotBinary |
| 74 | + } |
| 75 | + |
| 76 | + version := m.GetVersion() |
| 77 | + if version == nil { |
| 78 | + return ErrNoVersion |
| 79 | + } |
| 80 | + |
| 81 | + var err error |
| 82 | + for k, v := range m.Msg.Headers() { |
| 83 | + headerValue := v[0] |
| 84 | + if strings.HasPrefix(k, prefix) { |
| 85 | + attr := version.Attribute(k) |
| 86 | + if attr != nil { |
| 87 | + err = encoder.SetAttribute(attr, headerValue) |
| 88 | + } else { |
| 89 | + err = encoder.SetExtension(strings.TrimPrefix(k, prefix), headerValue) |
| 90 | + } |
| 91 | + } else if k == contentTypeHeader { |
| 92 | + err = encoder.SetAttribute(version.AttributeFromKind(spec.DataContentType), headerValue) |
| 93 | + } |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if m.Msg.Data() != nil { |
| 100 | + err = encoder.SetData(bytes.NewBuffer(m.Msg.Data())) |
| 101 | + } |
| 102 | + |
| 103 | + return err |
| 104 | +} |
| 105 | + |
| 106 | +// Finish *must* be called when message from a Receiver can be forgotten by the receiver. |
| 107 | +func (m *Message) Finish(err error) error { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// GetAttribute implements binding.MessageMetadataReader |
| 112 | +func (m *Message) GetAttribute(attributeKind spec.Kind) (spec.Attribute, interface{}) { |
| 113 | + key := withPrefix(attributeKind.String()) |
| 114 | + if m.Msg.Headers() != nil { |
| 115 | + version := m.GetVersion() |
| 116 | + headerValue := m.Msg.Headers().Get(key) |
| 117 | + if headerValue != "" { |
| 118 | + return version.Attribute(key), headerValue |
| 119 | + } |
| 120 | + return version.Attribute(key), nil |
| 121 | + } |
| 122 | + // if the headers are nil, the version is also nil. Therefore return nil. |
| 123 | + return nil, nil |
| 124 | +} |
| 125 | + |
| 126 | +// GetExtension implements binding.MessageMetadataReader |
| 127 | +func (m *Message) GetExtension(name string) interface{} { |
| 128 | + key := withPrefix(name) |
| 129 | + if m.Msg.Headers() != nil { |
| 130 | + headerValue := m.Msg.Headers().Get(key) |
| 131 | + if headerValue != "" { |
| 132 | + return headerValue |
| 133 | + } |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// GetVersion looks for specVersion header and returns a Version object |
| 139 | +func (m *Message) GetVersion() spec.Version { |
| 140 | + if m.Msg.Headers() == nil { |
| 141 | + return nil |
| 142 | + } |
| 143 | + versionValue := m.Msg.Headers().Get(specs.PrefixedSpecVersionName()) |
| 144 | + if versionValue == "" { |
| 145 | + return nil |
| 146 | + } |
| 147 | + return specs.Version(versionValue) |
| 148 | +} |
| 149 | + |
| 150 | +// withPrefix prepends the prefix to the attribute name |
| 151 | +func withPrefix(attributeName string) string { |
| 152 | + return fmt.Sprintf("%s%s", prefix, attributeName) |
| 153 | +} |
0 commit comments