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

Method AddStream

jsm.go:879–955  ·  view source on GitHub ↗
(cfg *StreamConfig, opts ...JSOpt)

Source from the content-addressed store, hash-verified

877}
878
879func (js *js) AddStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) {
880 if cfg == nil {
881 return nil, ErrStreamConfigRequired
882 }
883 if err := checkStreamName(cfg.Name); err != nil {
884 return nil, err
885 }
886 o, cancel, err := getJSContextOpts(js.opts, opts...)
887 if err != nil {
888 return nil, err
889 }
890 if cancel != nil {
891 defer cancel()
892 }
893
894 // In case we need to change anything, copy so we do not change the caller's version.
895 ncfg := *cfg
896
897 // If we have a mirror and an external domain, convert to ext.APIPrefix.
898 if cfg.Mirror != nil && cfg.Mirror.Domain != _EMPTY_ {
899 // Copy so we do not change the caller's version.
900 ncfg.Mirror = ncfg.Mirror.copy()
901 if err := ncfg.Mirror.convertDomain(); err != nil {
902 return nil, err
903 }
904 }
905 // Check sources for the same.
906 if len(ncfg.Sources) > 0 {
907 ncfg.Sources = append([]*StreamSource(nil), ncfg.Sources...)
908 for i, ss := range ncfg.Sources {
909 if ss.Domain != _EMPTY_ {
910 ncfg.Sources[i] = ss.copy()
911 if err := ncfg.Sources[i].convertDomain(); err != nil {
912 return nil, err
913 }
914 }
915 }
916 }
917
918 req, err := json.Marshal(&ncfg)
919 if err != nil {
920 return nil, err
921 }
922
923 csSubj := js.apiSubj(fmt.Sprintf(apiStreamCreateT, cfg.Name))
924 r, err := js.apiRequestWithContext(o.ctx, csSubj, req)
925 if err != nil {
926 return nil, err
927 }
928 var resp streamCreateResponse
929 if err := json.Unmarshal(r.Data, &resp); err != nil {
930 return nil, err
931 }
932 if resp.Error != nil {
933 if errors.Is(resp.Error, ErrStreamNameAlreadyInUse) {
934 return nil, ErrStreamNameAlreadyInUse
935 }
936 return nil, resp.Error

Callers 2

CreateKeyValueMethod · 0.95
CreateObjectStoreMethod · 0.95

Calls 7

apiSubjMethod · 0.95
apiRequestWithContextMethod · 0.95
checkStreamNameFunction · 0.85
getJSContextOptsFunction · 0.85
copyMethod · 0.45
convertDomainMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected