MCPcopy
hub / github.com/nats-io/nats.go / StreamNameBySubject

Method StreamNameBySubject

jsm.go:1736–1767  ·  view source on GitHub ↗

StreamNameBySubject returns a stream name that matches the subject.

(subj string, opts ...JSOpt)

Source from the content-addressed store, hash-verified

1734
1735// StreamNameBySubject returns a stream name that matches the subject.
1736func (jsc *js) StreamNameBySubject(subj string, opts ...JSOpt) (string, error) {
1737 o, cancel, err := getJSContextOpts(jsc.opts, opts...)
1738 if err != nil {
1739 return "", err
1740 }
1741 if cancel != nil {
1742 defer cancel()
1743 }
1744
1745 var slr streamNamesResponse
1746 req := &streamRequest{subj}
1747 j, err := json.Marshal(req)
1748 if err != nil {
1749 return _EMPTY_, err
1750 }
1751
1752 resp, err := jsc.apiRequestWithContext(o.ctx, jsc.apiSubj(apiStreams), j)
1753 if err != nil {
1754 if errors.Is(err, ErrNoResponders) {
1755 err = ErrJetStreamNotEnabled
1756 }
1757 return _EMPTY_, err
1758 }
1759 if err := json.Unmarshal(resp.Data, &slr); err != nil {
1760 return _EMPTY_, err
1761 }
1762
1763 if slr.Error != nil || len(slr.Streams) != 1 {
1764 return _EMPTY_, ErrNoMatchingStream
1765 }
1766 return slr.Streams[0], nil
1767}
1768
1769func getJSContextOpts(defs *jsOpts, opts ...JSOpt) (*jsOpts, context.CancelFunc, error) {
1770 var o jsOpts

Callers 1

subscribeMethod · 0.95

Calls 4

apiRequestWithContextMethod · 0.95
apiSubjMethod · 0.95
getJSContextOptsFunction · 0.85
IsMethod · 0.45

Tested by

no test coverage detected