Skip to content

Commit

Permalink
Add StreamBySubject to the jsm interface
Browse files Browse the repository at this point in the history
Signed-off-by: Jarema <melgaer@gmail.com>
  • Loading branch information
Jarema committed Oct 28, 2022
1 parent e4df87d commit e34d8e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions js.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ func (js *js) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync,

// Find the stream mapped to the subject if not bound to a stream already.
if o.stream == _EMPTY_ {
stream, err = js.LookupStreamBySubject(subj)
stream, err = js.StreamNameBySubject(subj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2144,8 +2144,8 @@ type streamNamesResponse struct {
Streams []string `json:"streams"`
}

// LookupStreamBySubject returns a stream name that matches the subject.
func (js *js) LookupStreamBySubject(subj string) (string, error) {
// StreamNameBySubject returns a stream name that matches the subject.
func (js *js) StreamNameBySubject(subj string) (string, error) {
var slr streamNamesResponse
req := &streamRequest{subj}
j, err := json.Marshal(req)
Expand Down
31 changes: 31 additions & 0 deletions js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,3 +1230,34 @@ func TestJetStreamStreamInfoWithSubjectDetails(t *testing.T) {
t.Fatalf("expected 0 subjects details from StreamInfo, but got %d instead", len(result.State.Subjects))
}
}

func StreamNameBySubject(t *testing.T) {

s := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, s)

nc, js := jsClient(t, s)
defer nc.Close()

var err error

_, err = js.AddStream(&StreamConfig{
Name: "TEST",
Subjects: []string{"test.*"},
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

stream, err := js.StreamNameBySubject("test.*")
if err != nil {
t.Fatalf("lookup stream should succeed for %s", "test.*")
}
if stream != "TEST" {
t.Fatalf("returned stream should be 'TEST'")
}

if _, err := js.StreamNameBySubject("bad"); err == nil {
t.Fatalf("error should be nil, no stream with name 'bad'")
}
}
3 changes: 3 additions & 0 deletions jsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type JetStreamManager interface {

// AccountInfo retrieves info about the JetStream usage from an account.
AccountInfo(opts ...JSOpt) (*AccountInfo, error)

// Returns a stream matching given subject.
StreamNameBySubject(string) (string, error)
}

// StreamConfig will determine the properties for a stream.
Expand Down

0 comments on commit e34d8e8

Please sign in to comment.