Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for nats_kv to be consistent in passing meta values along #215

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/impl/nats/integration_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ cache_resources:
require.NoError(t, err)

m := service.NewMessage([]byte("hello"))
m.MetaSetMut("inherited", "world")
return p.Process(context.Background(), m)
}

Expand All @@ -172,6 +173,12 @@ cache_resources:
bytes, err := m.AsBytes()
require.NoError(t, err)
assert.Equal(t, []byte("lawblog"), bytes)
if v, ok := m.MetaGetMut("inherited"); ok {
assert.Equal(t, "world", v)
} else {
t.Error("inherited metadata not found")
}

})

t.Run("get_revision operation", func(t *testing.T) {
Expand All @@ -193,6 +200,12 @@ cache_resources:
bytes, err := m.AsBytes()
require.NoError(t, err)
assert.Equal(t, []byte("lawblog"), bytes)
if v, ok := m.MetaGetMut("inherited"); ok {
assert.Equal(t, "world", v)
} else {
t.Error("inherited metadata not found")
}

})

t.Run("create operation (success)", func(t *testing.T) {
Expand All @@ -210,6 +223,12 @@ cache_resources:
bytes, err := m.AsBytes()
require.NoError(t, err)
assert.Equal(t, []byte("hello"), bytes)
if v, ok := m.MetaGetMut("inherited"); ok {
assert.Equal(t, "world", v)
} else {
t.Error("inherited metadata not found")
}

})

t.Run("create operation (error)", func(t *testing.T) {
Expand Down
19 changes: 17 additions & 2 deletions internal/impl/nats/processor_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ func (p *kvProcessor) Process(ctx context.Context, msg *service.Message) (servic
if err != nil {
return nil, err
}
return service.MessageBatch{newMessageFromKVEntry(entry)}, nil
m := msg.Copy()
p.addMetadataFromEntry(m, entry)
m.SetBytes(entry.Value())
return service.MessageBatch{m}, nil

case kvpOperationGetRevision:
revision, err := p.parseRevision(msg)
Expand All @@ -228,7 +231,10 @@ func (p *kvProcessor) Process(ctx context.Context, msg *service.Message) (servic
if err != nil {
return nil, err
}
return service.MessageBatch{newMessageFromKVEntry(entry)}, nil
m := msg.Copy()
p.addMetadataFromEntry(m, entry)
m.SetBytes(entry.Value())
return service.MessageBatch{m}, nil

case kvpOperationCreate:
revision, err := kv.Create(ctx, key, bytes)
Expand Down Expand Up @@ -359,6 +365,15 @@ func (p *kvProcessor) addMetadata(msg *service.Message, key string, revision uin
msg.MetaSetMut(metaKVOperation, operation.String())
}

func (p *kvProcessor) addMetadataFromEntry(msg *service.Message, entry jetstream.KeyValueEntry) {
msg.MetaSetMut(metaKVKey, entry.Key())
msg.MetaSetMut(metaKVBucket, p.bucket)
msg.MetaSetMut(metaKVRevision, entry.Revision())
msg.MetaSetMut(metaKVDelta, entry.Delta())
msg.MetaSetMut(metaKVOperation, entry.Operation().String())
msg.MetaSetMut(metaKVCreated, entry.Created())
}

func (p *kvProcessor) Connect(ctx context.Context) (err error) {
p.connMut.Lock()
defer p.connMut.Unlock()
Expand Down
Loading