diff --git a/.golangci.yml b/.golangci.yml index 6459d64f436b2d415f3bf293d4748f3a10d1bba0..c7dc4a6f94da9338a05f15f453872277a53d4a0d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -356,6 +356,8 @@ issues: - path: "_middleware\\.go" linters: - nonamedreturns + - revive + - stylecheck - path: "test/.*\\.go" linters: - mnd diff --git a/pkg/items/middleware/access_logging_middleware.go b/pkg/items/middleware/access_logging_middleware.go index b12ac020a52c4209f67eec5d0f4284ebdca01204..a2659ff0f31280363a5a7a7943816c92d6d5916d 100644 --- a/pkg/items/middleware/access_logging_middleware.go +++ b/pkg/items/middleware/access_logging_middleware.go @@ -97,27 +97,28 @@ func (m *accessLoggingMiddleware) Archive(ctx context.Context, item *items.Item, return err } -func (m *accessLoggingMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { +func (m *accessLoggingMiddleware) CheckoutRevision(ctx context.Context, spaceID string, envID string, collectionID string, id string, revID string, options ...*items.CheckoutRevisionsOptions) (storedRevID string, err error) { begin := time.Now() m.logger.Debug("CheckoutRevision.Request", zap.Reflect("principal", auth.GetPrincipal(ctx)), - zap.Reflect("spaceId", spaceId), - zap.Reflect("envId", envId), - zap.Reflect("collId", collId), + zap.Reflect("spaceID", spaceID), + zap.Reflect("envID", envID), + zap.Reflect("collectionID", collectionID), zap.Reflect("id", id), - zap.Reflect("revId", revId), + zap.Reflect("revID", revID), + zap.Reflect("options", options), ) - storedRevId, err = m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) + storedRevID, err = m.next.CheckoutRevision(ctx, spaceID, envID, collectionID, id, revID, options...) m.logger.Debug("CheckoutRevision.Response", zap.Duration("time", time.Since(begin)), - zap.Reflect("storedRevId", storedRevId), + zap.Reflect("storedRevID", storedRevID), zap.Error(err), ) - return storedRevId, err + return storedRevID, err } func (m *accessLoggingMiddleware) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) { @@ -254,19 +255,19 @@ func (m *accessLoggingMiddleware) Get(ctx context.Context, spaceId string, envId return item, err } -func (m *accessLoggingMiddleware) GetArchived(ctx context.Context, spaceId string, envId string, collectionId string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { +func (m *accessLoggingMiddleware) GetArchived(ctx context.Context, spaceID string, envID string, collectionID string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { begin := time.Now() m.logger.Debug("GetArchived.Request", zap.Reflect("principal", auth.GetPrincipal(ctx)), - zap.Reflect("spaceId", spaceId), - zap.Reflect("envId", envId), - zap.Reflect("collectionId", collectionId), + zap.Reflect("spaceID", spaceID), + zap.Reflect("envID", envID), + zap.Reflect("collectionID", collectionID), zap.Reflect("id", id), zap.Reflect("options", options), ) - item, err = m.next.GetArchived(ctx, spaceId, envId, collectionId, id, options...) + item, err = m.next.GetArchived(ctx, spaceID, envID, collectionID, id, options...) m.logger.Debug("GetArchived.Response", zap.Duration("time", time.Since(begin)), diff --git a/pkg/items/middleware/caching_middleware.go b/pkg/items/middleware/caching_middleware.go index be9690a8d68292990189758cc8c41f3d69381794..f2d9bce6de7153c99bc9d116a538f59547b915b3 100644 --- a/pkg/items/middleware/caching_middleware.go +++ b/pkg/items/middleware/caching_middleware.go @@ -165,8 +165,16 @@ func (m cachingMiddleware) GetPublished(ctx context.Context, spaceId, envId, col return nil, err } -func (m cachingMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { - storedRevId, err = m.Items.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) +func (m cachingMiddleware) CheckoutRevision( + ctx context.Context, + spaceId string, + envId string, + collId string, + id string, + revId string, + options ...*service.CheckoutRevisionsOptions, +) (storedRevId string, err error) { + storedRevId, err = m.Items.CheckoutRevision(ctx, spaceId, envId, collId, id, revId, options...) if err != nil { return "", err } diff --git a/pkg/items/middleware/client_encode_middleware.go b/pkg/items/middleware/client_encode_middleware.go index 315d6cd91c946a85bd265faec2677f507574f106..324c648ea398efab8295077fd6e49f897cf86f93 100644 --- a/pkg/items/middleware/client_encode_middleware.go +++ b/pkg/items/middleware/client_encode_middleware.go @@ -298,8 +298,16 @@ func (m *encodeDecodeMiddleware) Undelete(ctx context.Context, item *items.Item, return m.next.Undelete(ctx, item, options...) } -func (m *encodeDecodeMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { - return m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) +func (m *encodeDecodeMiddleware) CheckoutRevision( + ctx context.Context, + spaceId string, + envId string, + collId string, + id string, + revId string, + options ...*items.CheckoutRevisionsOptions, +) (storedRevId string, err error) { + return m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId, options...) } func (m *encodeDecodeMiddleware) Aggregate(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.AggregateOptions) (result map[string]interface{}, err error) { diff --git a/pkg/items/middleware/logging_middleware.go b/pkg/items/middleware/logging_middleware.go index 6f0933d3abde50da1d84c6be1c5602ced7d96d0b..037eeba3dfb75055992d315c87ed4b11b033ef2c 100644 --- a/pkg/items/middleware/logging_middleware.go +++ b/pkg/items/middleware/logging_middleware.go @@ -24,14 +24,22 @@ func LoggingMiddleware(logger *zap.Logger) Middleware { } } -func (m *loggingMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { +func (m *loggingMiddleware) CheckoutRevision( + ctx context.Context, + spaceId string, + envId string, + collId string, + id string, + revId string, + options ...*items.CheckoutRevisionsOptions, +) (storedRevId string, err error) { logger := m.logger.With( logzap.Caller(ctx, logzap.WithSpace(spaceId)), logzap.Event(items.EventCheckoutRevision), logzap.Object(pkgId.NewItemId(spaceId, envId, collId, id)), ) - storedRevId, err = m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) + storedRevId, err = m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId, options...) if err != nil { logger.Error("Failed to checkout revision", zap.Error(err)) return diff --git a/pkg/items/middleware/recovering_middleware.go b/pkg/items/middleware/recovering_middleware.go index a542633a537e685fe71d70987483779e1e5d7a05..1583944a0639012adff4d4b8ec479539c6daae89 100644 --- a/pkg/items/middleware/recovering_middleware.go +++ b/pkg/items/middleware/recovering_middleware.go @@ -67,7 +67,7 @@ func (m *recoveringMiddleware) Archive(ctx context.Context, item *items.Item, op return m.next.Archive(ctx, item, options...) } -func (m *recoveringMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { +func (m *recoveringMiddleware) CheckoutRevision(ctx context.Context, spaceID string, envID string, collectionID string, id string, revID string, options ...*items.CheckoutRevisionsOptions) (storedRevID string, err error) { logger := m.logger defer func() { if r := recover(); r != nil { @@ -76,7 +76,7 @@ func (m *recoveringMiddleware) CheckoutRevision(ctx context.Context, spaceId str } }() - return m.next.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) + return m.next.CheckoutRevision(ctx, spaceID, envID, collectionID, id, revID, options...) } func (m *recoveringMiddleware) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) { @@ -151,7 +151,7 @@ func (m *recoveringMiddleware) Get(ctx context.Context, spaceId string, envId st return m.next.Get(ctx, spaceId, envId, collectionId, id, options...) } -func (m *recoveringMiddleware) GetArchived(ctx context.Context, spaceId string, envId string, collectionId string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { +func (m *recoveringMiddleware) GetArchived(ctx context.Context, spaceID string, envID string, collectionID string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { logger := m.logger defer func() { if r := recover(); r != nil { @@ -160,7 +160,7 @@ func (m *recoveringMiddleware) GetArchived(ctx context.Context, spaceId string, } }() - return m.next.GetArchived(ctx, spaceId, envId, collectionId, id, options...) + return m.next.GetArchived(ctx, spaceID, envID, collectionID, id, options...) } func (m *recoveringMiddleware) GetPublished(ctx context.Context, spaceId string, envId string, collectionId string, id string, options ...*items.GetPublishedOptions) (item *items.Item, err error) { diff --git a/pkg/items/middleware/telemetry_middleware.go b/pkg/items/middleware/telemetry_middleware.go index 8d8bfbb907447168b2632de45fe5b6a749e4ec06..e9f8e6a99d85e05ec3a219299a5c04f63706d4ca 100644 --- a/pkg/items/middleware/telemetry_middleware.go +++ b/pkg/items/middleware/telemetry_middleware.go @@ -208,7 +208,7 @@ func (_d telemetryMiddleware) Archive(ctx context.Context, item *items.Item, opt } // CheckoutRevision implements items.Items -func (_d telemetryMiddleware) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (storedRevId string, err error) { +func (_d telemetryMiddleware) CheckoutRevision(ctx context.Context, spaceID string, envID string, collectionID string, id string, revID string, options ...*items.CheckoutRevisionsOptions) (storedRevID string, err error) { var att = []attribute.KeyValue{ attribute.String("service", "Items"), attribute.String("method", "CheckoutRevision"), @@ -219,12 +219,23 @@ func (_d telemetryMiddleware) CheckoutRevision(ctx context.Context, spaceId stri ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Items.CheckoutRevision") defer _span.End() - storedRevId, err = _d.Items.CheckoutRevision(ctx, spaceId, envId, collId, id, revId) + storedRevID, err = _d.Items.CheckoutRevision(ctx, spaceID, envID, collectionID, id, revID, options...) _d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes) var spID string - spID = spaceId + params := []interface{}{ctx, spaceID, envID, collectionID, id, revID, options, storedRevID, err} + for _, p := range params { + if p == nil { + continue + } + if sg, ok := p.(spaceGetter); ok { + spID = sg.GetSpaceID() + if spID != "" { + break + } + } + } if spID != "" { att = append(att, attribute.String("spaceID", spID)) } @@ -237,13 +248,14 @@ func (_d telemetryMiddleware) CheckoutRevision(ctx context.Context, spaceId stri if _d._spanDecorator != nil { _d._spanDecorator(_span, map[string]interface{}{ - "ctx": ctx, - "spaceId": spaceId, - "envId": envId, - "collId": collId, - "id": id, - "revId": revId}, map[string]interface{}{ - "storedRevId": storedRevId, + "ctx": ctx, + "spaceID": spaceID, + "envID": envID, + "collectionID": collectionID, + "id": id, + "revID": revID, + "options": options}, map[string]interface{}{ + "storedRevID": storedRevID, "err": err}) } else if err != nil { _d.requestMetrics.FailedTotal.Add(ctx, 1, attributes) @@ -253,7 +265,7 @@ func (_d telemetryMiddleware) CheckoutRevision(ctx context.Context, spaceId stri _span.SetAttributes(attribute.String("message", err.Error())) } - return storedRevId, err + return storedRevID, err } // Create implements items.Items @@ -569,7 +581,7 @@ func (_d telemetryMiddleware) Get(ctx context.Context, spaceId string, envId str } // GetArchived implements items.Items -func (_d telemetryMiddleware) GetArchived(ctx context.Context, spaceId string, envId string, collectionId string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { +func (_d telemetryMiddleware) GetArchived(ctx context.Context, spaceID string, envID string, collectionID string, id string, options ...*items.GetArchivedOptions) (item *items.Item, err error) { var att = []attribute.KeyValue{ attribute.String("service", "Items"), attribute.String("method", "GetArchived"), @@ -580,12 +592,23 @@ func (_d telemetryMiddleware) GetArchived(ctx context.Context, spaceId string, e ctx, _span := otel.Tracer(_d._instance).Start(ctx, "Items.GetArchived") defer _span.End() - item, err = _d.Items.GetArchived(ctx, spaceId, envId, collectionId, id, options...) + item, err = _d.Items.GetArchived(ctx, spaceID, envID, collectionID, id, options...) _d.requestMetrics.DurationMilliseconds.Record(ctx, time.Since(start).Milliseconds(), attributes) var spID string - spID = spaceId + params := []interface{}{ctx, spaceID, envID, collectionID, id, options, item, err} + for _, p := range params { + if p == nil { + continue + } + if sg, ok := p.(spaceGetter); ok { + spID = sg.GetSpaceID() + if spID != "" { + break + } + } + } if spID != "" { att = append(att, attribute.String("spaceID", spID)) } @@ -599,9 +622,9 @@ func (_d telemetryMiddleware) GetArchived(ctx context.Context, spaceId string, e if _d._spanDecorator != nil { _d._spanDecorator(_span, map[string]interface{}{ "ctx": ctx, - "spaceId": spaceId, - "envId": envId, - "collectionId": collectionId, + "spaceID": spaceID, + "envID": envID, + "collectionID": collectionID, "id": id, "options": options}, map[string]interface{}{ "item": item, diff --git a/pkg/items/mocks/Items.go b/pkg/items/mocks/Items.go index 1c34c474cb92e044d7dcd99d46f688ffd80f0281..4bb7c278b9843db26dbe7b5cf4662dc75f39ffc0 100644 --- a/pkg/items/mocks/Items.go +++ b/pkg/items/mocks/Items.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.49.0. DO NOT EDIT. +// Code generated by mockery v2.50.4. DO NOT EDIT. package mocks @@ -115,9 +115,16 @@ func (_m *Items) Archive(ctx context.Context, item *items.Item, options ...*item return r0 } -// CheckoutRevision provides a mock function with given fields: ctx, spaceId, envId, collId, id, revId -func (_m *Items) CheckoutRevision(ctx context.Context, spaceId string, envId string, collId string, id string, revId string) (string, error) { - ret := _m.Called(ctx, spaceId, envId, collId, id, revId) +// CheckoutRevision provides a mock function with given fields: ctx, spaceID, envID, collectionID, id, revID, options +func (_m *Items) CheckoutRevision(ctx context.Context, spaceID string, envID string, collectionID string, id string, revID string, options ...*items.CheckoutRevisionsOptions) (string, error) { + _va := make([]interface{}, len(options)) + for _i := range options { + _va[_i] = options[_i] + } + var _ca []interface{} + _ca = append(_ca, ctx, spaceID, envID, collectionID, id, revID) + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) if len(ret) == 0 { panic("no return value specified for CheckoutRevision") @@ -125,17 +132,17 @@ func (_m *Items) CheckoutRevision(ctx context.Context, spaceId string, envId str var r0 string var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, string) (string, error)); ok { - return rf(ctx, spaceId, envId, collId, id, revId) + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, string, ...*items.CheckoutRevisionsOptions) (string, error)); ok { + return rf(ctx, spaceID, envID, collectionID, id, revID, options...) } - if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, string) string); ok { - r0 = rf(ctx, spaceId, envId, collId, id, revId) + if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, string, ...*items.CheckoutRevisionsOptions) string); ok { + r0 = rf(ctx, spaceID, envID, collectionID, id, revID, options...) } else { r0 = ret.Get(0).(string) } - if rf, ok := ret.Get(1).(func(context.Context, string, string, string, string, string) error); ok { - r1 = rf(ctx, spaceId, envId, collId, id, revId) + if rf, ok := ret.Get(1).(func(context.Context, string, string, string, string, string, ...*items.CheckoutRevisionsOptions) error); ok { + r1 = rf(ctx, spaceID, envID, collectionID, id, revID, options...) } else { r1 = ret.Error(1) } @@ -374,14 +381,14 @@ func (_m *Items) Get(ctx context.Context, spaceId string, envId string, collecti return r0, r1 } -// GetArchived provides a mock function with given fields: ctx, spaceId, envId, collectionId, id, options -func (_m *Items) GetArchived(ctx context.Context, spaceId string, envId string, collectionId string, id string, options ...*items.GetArchivedOptions) (*items.Item, error) { +// GetArchived provides a mock function with given fields: ctx, spaceID, envID, collectionID, id, options +func (_m *Items) GetArchived(ctx context.Context, spaceID string, envID string, collectionID string, id string, options ...*items.GetArchivedOptions) (*items.Item, error) { _va := make([]interface{}, len(options)) for _i := range options { _va[_i] = options[_i] } var _ca []interface{} - _ca = append(_ca, ctx, spaceId, envId, collectionId, id) + _ca = append(_ca, ctx, spaceID, envID, collectionID, id) _ca = append(_ca, _va...) ret := _m.Called(_ca...) @@ -392,10 +399,10 @@ func (_m *Items) GetArchived(ctx context.Context, spaceId string, envId string, var r0 *items.Item var r1 error if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, ...*items.GetArchivedOptions) (*items.Item, error)); ok { - return rf(ctx, spaceId, envId, collectionId, id, options...) + return rf(ctx, spaceID, envID, collectionID, id, options...) } if rf, ok := ret.Get(0).(func(context.Context, string, string, string, string, ...*items.GetArchivedOptions) *items.Item); ok { - r0 = rf(ctx, spaceId, envId, collectionId, id, options...) + r0 = rf(ctx, spaceID, envID, collectionID, id, options...) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*items.Item) @@ -403,7 +410,7 @@ func (_m *Items) GetArchived(ctx context.Context, spaceId string, envId string, } if rf, ok := ret.Get(1).(func(context.Context, string, string, string, string, ...*items.GetArchivedOptions) error); ok { - r1 = rf(ctx, spaceId, envId, collectionId, id, options...) + r1 = rf(ctx, spaceID, envID, collectionID, id, options...) } else { r1 = ret.Error(1) } diff --git a/pkg/items/options.go b/pkg/items/options.go index fa00e273318acd036cb5d99938f53a813b06890f..b46d5e370255070ca0475c69b5ba01d8488d8eac 100644 --- a/pkg/items/options.go +++ b/pkg/items/options.go @@ -834,3 +834,18 @@ func MergeAggregatePublishedOptions(opts ...*AggregatePublishedOptions) *Aggrega merged := MergeAggregateOptions(ao...) return (*AggregatePublishedOptions)(merged) } + +type CheckoutRevisionsOptions struct { + Options +} + +func MergeCheckoutRevisionsOptions(opts ...*CheckoutRevisionsOptions) *CheckoutRevisionsOptions { + o := &CheckoutRevisionsOptions{} + for _, opt := range opts { + if opt == nil { + continue + } + o.Options = MergeOptions(o.Options, opt.Options) + } + return o +} diff --git a/pkg/items/service.go b/pkg/items/service.go index 361efff3005cf417b138b19a6bc5871be5c082af..8ec77a1a3c50839e6f6318002f0231978dbeb5c7 100644 --- a/pkg/items/service.go +++ b/pkg/items/service.go @@ -36,7 +36,7 @@ type Items interface { GetRevision(ctx context.Context, spaceId, envId, collectionId, id, revisionId string, options ...*GetRevisionOptions) (item *Item, err error) ListRevisions(ctx context.Context, spaceId, envId, collectionId, id string, options ...*ListRevisionsOptions) (items []*Item, total int, err error) CheckoutRevision(ctx context.Context, spaceID string, envID string, collectionID string, id string, revID string, - ) (storedRevID string, err error) + options ...*CheckoutRevisionsOptions) (storedRevID string, err error) Archive(ctx context.Context, item *Item, options ...*ArchiveOptions) (err error) FindArchived(ctx context.Context, spaceId, envId, collectionId string, filter *Filter, options ...*FindArchivedOptions) (items []*Item, total int, err error) diff --git a/pkg/items/transport/client.go b/pkg/items/transport/client.go index f0fc26c8a35454ad050f99720b223a633bccb99c..0fbf6de05ca82835764b2099a1e0cd07c69c2350 100644 --- a/pkg/items/transport/client.go +++ b/pkg/items/transport/client.go @@ -235,7 +235,7 @@ func (set EndpointsSet) Unarchive(arg0 context.Context, arg1 *items.Item, arg2 . return res0 } -func (set EndpointsSet) CheckoutRevision(arg0 context.Context, arg1 string, arg2 string, arg3 string, arg4 string, arg5 string) (res0 string, res1 error) { +func (set EndpointsSet) CheckoutRevision(arg0 context.Context, arg1 string, arg2 string, arg3 string, arg4 string, arg5 string, arg6 ...*items.CheckoutRevisionsOptions) (res0 string, res1 error) { request := CheckoutRevisionRequest{ CollId: arg3, EnvId: arg2,