Skip to content

Commit

Permalink
limit backward compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
wdbaruni committed Dec 11, 2024
1 parent f57cd36 commit 1f094d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
32 changes: 12 additions & 20 deletions pkg/lib/envelope/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
// Metadata keys
const (
HeaderPrefix = "Bacalhau-"
KeyMessageType = "Type"
KeyPayloadEncoding = "PayloadEncoding"
KeyMessageType = "Bacalhau-Type"
KeyPayloadEncoding = "Bacalhau-PayloadEncoding"
)

// Metadata contains metadata about the message
Expand All @@ -25,27 +25,11 @@ func (m Metadata) ToMap() map[string]string {
func (m Metadata) ToHeaders() map[string][]string {
headers := make(map[string][]string, len(m))
for k, v := range m {
headers[HeaderPrefix+k] = []string{v}
headers[k] = []string{v}
}
return headers
}

// FromHeaders creates a new Metadata object from a map[string][]string
func FromHeaders(headers map[string][]string) Metadata {
metadata := make(Metadata, len(headers))
for k, v := range headers {
if len(v) == 0 {
continue
}
key := k
if strings.HasPrefix(k, HeaderPrefix) && len(k) > len(HeaderPrefix) {
key = k[len(HeaderPrefix):]
}
metadata[key] = v[0]
}
return metadata
}

// NewMetadataFromMap creates a new shallow copy Metadata object from a map.
// Changes to the map will be reflected in the Metadata object, but more efficient than NewMetadataFromMapCopy
func NewMetadataFromMap(m map[string]string) *Metadata {
Expand All @@ -68,7 +52,15 @@ func NewMetadataFromMapCopy(m map[string]string) *Metadata {

// Get returns the value for a given key, or an empty string if the key doesn't exist
func (m Metadata) Get(key string) string {
return m[key]
if v, ok := m[key]; ok {
return v
}
// backward compatible with old headers
if key == KeyMessageType || key == KeyPayloadEncoding {
// return keys excluding header prefix
return m[strings.TrimPrefix(key, HeaderPrefix)]
}
return ""
}

// Has checks if a key exists in the metadata
Expand Down
10 changes: 5 additions & 5 deletions pkg/lib/ncl/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package ncl

// Metadata keys
const (
KeySource = "Source"
KeyEventTime = "EventTime"
KeyMessageUUID = "MessageUUID"
KeyMessageID = "MessageID"
KeySubject = "Subject"
KeySource = "Bacalhau-Source"
KeyEventTime = "Bacalhau-EventTime"
KeyMessageUUID = "Bacalhau-MessageUUID"
KeyMessageID = "Bacalhau-MessageID"
KeySubject = "Bacalhau-Subject"
)
2 changes: 1 addition & 1 deletion pkg/lib/ncl/error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
ErrorMessageType = "ncl.ErrorResponse"

// KeyStatusCode is the key for the status code
KeyStatusCode = "StatusCode"
KeyStatusCode = "Bacalhau-StatusCode"

// StatusBadRequest is the status code for a bad request
StatusBadRequest = 400
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/nclprotocol/dispatcher/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dispatcher

const (
KeySeqNum = "SeqNum"
KeySeqNum = "Bacalhau-SeqNum"
)
2 changes: 1 addition & 1 deletion pkg/transport/nclprotocol/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ConnectionHealth struct {
}

const (
KeySeqNum = "SeqNum"
KeySeqNum = "Bacalhau-SeqNum"
)

// MessageCreator defines how events from the watcher are converted into
Expand Down

0 comments on commit 1f094d5

Please sign in to comment.