Skip to content

Commit ee1fa3f

Browse files
style: resolve code style review comments
1 parent 833100f commit ee1fa3f

8 files changed

Lines changed: 134 additions & 54 deletions

File tree

api/v3/handlers/governance/mapping.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,87 @@ import (
44
"github.com/oapi-codegen/nullable"
55
"github.com/samber/lo"
66

7-
api "github.com/openmeterio/openmeter/api/v3"
7+
apiv3 "github.com/openmeterio/openmeter/api/v3"
88
customershandler "github.com/openmeterio/openmeter/api/v3/handlers/customers"
99
"github.com/openmeterio/openmeter/openmeter/governance"
1010
)
1111

1212
// ToAPIGovernanceQueryResponse maps a domain QueryResult to the API response.
13-
func ToAPIGovernanceQueryResponse(res governance.QueryResult, pageSize int) api.GovernanceQueryResponse {
14-
data := make([]api.GovernanceQueryResult, 0, len(res.Customers))
13+
func ToAPIGovernanceQueryResponse(res governance.QueryResult, pageSize int) apiv3.GovernanceQueryResponse {
14+
data := make([]apiv3.GovernanceQueryResult, 0, len(res.Customers))
15+
1516
for _, c := range res.Customers {
16-
features := make(map[string]api.GovernanceFeatureAccess, len(c.Features))
17+
features := make(map[string]apiv3.GovernanceFeatureAccess, len(c.Features))
1718
for key, fa := range c.Features {
1819
features[key] = toAPIFeatureAccess(fa)
1920
}
2021

21-
data = append(data, api.GovernanceQueryResult{
22+
data = append(data, apiv3.GovernanceQueryResult{
2223
Matched: c.Matched,
2324
Customer: customershandler.ToAPIBillingCustomer(c.Customer),
2425
Features: features,
2526
UpdatedAt: c.UpdatedAt,
2627
})
2728
}
2829

29-
errs := make([]api.GovernanceQueryError, 0, len(res.Errors))
30+
errs := make([]apiv3.GovernanceQueryError, 0, len(res.Errors))
31+
3032
for _, e := range res.Errors {
31-
errs = append(errs, api.GovernanceQueryError{
33+
errs = append(errs, apiv3.GovernanceQueryError{
3234
Customer: lo.ToPtr(e.CustomerKey),
3335
Code: toAPIQueryErrorCode(e.Code),
3436
Message: e.Message,
3537
})
3638
}
3739

38-
return api.GovernanceQueryResponse{
40+
return apiv3.GovernanceQueryResponse{
3941
Data: data,
4042
Errors: errs,
4143
Meta: toAPICursorMeta(res, pageSize),
4244
}
4345
}
4446

45-
func toAPIFeatureAccess(fa governance.FeatureAccess) api.GovernanceFeatureAccess {
46-
out := api.GovernanceFeatureAccess{HasAccess: fa.HasAccess}
47+
func toAPIFeatureAccess(fa governance.FeatureAccess) apiv3.GovernanceFeatureAccess {
48+
out := apiv3.GovernanceFeatureAccess{HasAccess: fa.HasAccess}
49+
4750
if fa.Reason != nil {
48-
out.Reason = &api.GovernanceFeatureAccessReason{
51+
out.Reason = &apiv3.GovernanceFeatureAccessReason{
4952
Code: toAPIReasonCode(fa.Reason.Code),
5053
Message: fa.Reason.Message,
5154
}
5255
}
56+
5357
return out
5458
}
5559

56-
func toAPIReasonCode(code governance.ReasonCode) api.GovernanceFeatureAccessReasonCode {
60+
func toAPIReasonCode(code governance.ReasonCode) apiv3.GovernanceFeatureAccessReasonCode {
5761
switch code {
5862
case governance.ReasonUsageLimitReached:
59-
return api.GovernanceFeatureAccessReasonCodeUsageLimitReached
63+
return apiv3.GovernanceFeatureAccessReasonCodeUsageLimitReached
6064
case governance.ReasonFeatureUnavailable:
61-
return api.GovernanceFeatureAccessReasonCodeFeatureUnavailable
65+
return apiv3.GovernanceFeatureAccessReasonCodeFeatureUnavailable
6266
case governance.ReasonFeatureNotFound:
63-
return api.GovernanceFeatureAccessReasonCodeFeatureNotFound
67+
return apiv3.GovernanceFeatureAccessReasonCodeFeatureNotFound
6468
case governance.ReasonNoCreditAvailable:
65-
return api.GovernanceFeatureAccessReasonCodeNoCreditAvailable
69+
return apiv3.GovernanceFeatureAccessReasonCodeNoCreditAvailable
6670
default:
67-
return api.GovernanceFeatureAccessReasonCodeUnknown
71+
return apiv3.GovernanceFeatureAccessReasonCodeUnknown
6872
}
6973
}
7074

71-
func toAPIQueryErrorCode(code governance.QueryErrorCode) api.GovernanceQueryErrorCode {
75+
func toAPIQueryErrorCode(code governance.QueryErrorCode) apiv3.GovernanceQueryErrorCode {
7276
switch code {
7377
case governance.QueryErrorCustomerNotFound:
74-
return api.GovernanceQueryErrorCodeCustomerNotFound
78+
return apiv3.GovernanceQueryErrorCodeCustomerNotFound
7579
default:
76-
return api.GovernanceQueryErrorCodeUnknown
80+
return apiv3.GovernanceQueryErrorCodeUnknown
7781
}
7882
}
7983

8084
// toAPICursorMeta builds cursor pagination metadata from the domain result.
81-
func toAPICursorMeta(res governance.QueryResult, pageSize int) api.CursorMeta {
82-
meta := api.CursorMeta{
83-
Page: api.CursorMetaPage{
85+
func toAPICursorMeta(res governance.QueryResult, pageSize int) apiv3.CursorMeta {
86+
meta := apiv3.CursorMeta{
87+
Page: apiv3.CursorMetaPage{
8488
Next: nullable.NewNullNullable[string](),
8589
Previous: nullable.NewNullNullable[string](),
8690
Size: float32(pageSize),
@@ -93,6 +97,7 @@ func toAPICursorMeta(res governance.QueryResult, pageSize int) api.CursorMeta {
9397
meta.Page.Previous = nullable.NewNullableWithValue(res.First.Encode())
9498
}
9599
}
100+
96101
if res.Last != nil {
97102
meta.Page.Last = lo.ToPtr(res.Last.Encode())
98103
if res.HasNext {

api/v3/handlers/governance/query.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"net/http"
77

8-
api "github.com/openmeterio/openmeter/api/v3"
8+
apiv3 "github.com/openmeterio/openmeter/api/v3"
99
"github.com/openmeterio/openmeter/api/v3/apierrors"
1010
"github.com/openmeterio/openmeter/openmeter/governance"
1111
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
@@ -19,20 +19,22 @@ const (
1919
)
2020

2121
type (
22-
QueryGovernanceAccessParams = api.QueryGovernanceAccessParams
23-
QueryGovernanceAccessResponse = api.GovernanceQueryResponse
22+
QueryGovernanceAccessParams = apiv3.QueryGovernanceAccessParams
23+
QueryGovernanceAccessResponse = apiv3.GovernanceQueryResponse
2424
QueryGovernanceAccessHandler = httptransport.HandlerWithArgs[governance.QueryAccessInput, QueryGovernanceAccessResponse, QueryGovernanceAccessParams]
2525
)
2626

2727
func (h *handler) QueryGovernanceAccess() QueryGovernanceAccessHandler {
2828
return httptransport.NewHandlerWithArgs(
2929
func(ctx context.Context, r *http.Request, params QueryGovernanceAccessParams) (governance.QueryAccessInput, error) {
3030
ns, err := h.resolveNamespace(ctx)
31+
3132
if err != nil {
3233
return governance.QueryAccessInput{}, err
3334
}
3435

35-
var body api.GovernanceQueryRequest
36+
var body apiv3.GovernanceQueryRequest
37+
3638
if err := commonhttp.JSONRequestBodyDecoder(r, &body); err != nil {
3739
return governance.QueryAccessInput{}, err
3840
}
@@ -42,9 +44,11 @@ func (h *handler) QueryGovernanceAccess() QueryGovernanceAccessHandler {
4244
CustomerKeys: body.Customer.Keys,
4345
PageSize: defaultPageSize,
4446
}
47+
4548
if body.Feature != nil {
4649
input.FeatureKeys = body.Feature.Keys
4750
}
51+
4852
if body.IncludeCredits != nil {
4953
input.IncludeCredits = *body.IncludeCredits
5054
}
@@ -57,9 +61,11 @@ func (h *handler) QueryGovernanceAccess() QueryGovernanceAccessHandler {
5761
},
5862
func(ctx context.Context, input governance.QueryAccessInput) (QueryGovernanceAccessResponse, error) {
5963
res, err := h.governanceService.QueryAccess(ctx, input)
64+
6065
if err != nil {
6166
return QueryGovernanceAccessResponse{}, err
6267
}
68+
6369
return ToAPIGovernanceQueryResponse(res, input.PageSize), nil
6470
},
6571
commonhttp.JSONResponseEncoderWithStatus[QueryGovernanceAccessResponse](http.StatusOK),
@@ -88,6 +94,7 @@ func applyPaging(ctx context.Context, input *governance.QueryAccessInput, params
8894
}},
8995
)
9096
}
97+
9198
input.PageSize = *params.Page.Size
9299
}
93100

@@ -123,12 +130,14 @@ func applyPaging(ctx context.Context, input *governance.QueryAccessInput, params
123130

124131
func decodeCursorParam(ctx context.Context, field, raw string) (*pagination.Cursor, error) {
125132
cursor, err := pagination.DecodeCursor(raw)
133+
126134
if err != nil {
127135
return nil, apierrors.NewBadRequestError(ctx, err, apierrors.InvalidParameters{{
128136
Field: field,
129137
Reason: err.Error(),
130138
Source: apierrors.InvalidParamSourceQuery,
131139
}})
132140
}
141+
133142
return cursor, nil
134143
}

app/common/governance.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ var Governance = wire.NewSet(
1515
)
1616

1717
func NewGovernanceService(
18-
customerService customer.Service,
18+
customer customer.Service,
1919
entitlementRegistry *registry.Entitlement,
2020
tracer trace.Tracer,
2121
) (governance.Service, error) {
2222
return governanceservice.New(governanceservice.Config{
23-
CustomerService: customerService,
24-
EntitlementService: entitlementRegistry.Entitlement,
25-
FeatureConnector: entitlementRegistry.Feature,
26-
Tracer: tracer,
23+
Customer: customer,
24+
Entitlement: entitlementRegistry.Entitlement,
25+
Feature: entitlementRegistry.Feature,
26+
Tracer: tracer,
2727
})
2828
}

openmeter/governance/service/mapping.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func mapEntitlementToAccess(v entitlement.EntitlementValue) governance.FeatureAc
1616
if ent.HasAccess() {
1717
return governance.FeatureAccess{HasAccess: true}
1818
}
19+
1920
return governance.FeatureAccess{
2021
HasAccess: false,
2122
Reason: &governance.AccessReason{
@@ -28,6 +29,7 @@ func mapEntitlementToAccess(v entitlement.EntitlementValue) governance.FeatureAc
2829
if ent.HasAccess() {
2930
return governance.FeatureAccess{HasAccess: true}
3031
}
32+
3133
return governance.FeatureAccess{
3234
HasAccess: false,
3335
Reason: &governance.AccessReason{
@@ -40,6 +42,7 @@ func mapEntitlementToAccess(v entitlement.EntitlementValue) governance.FeatureAc
4042
if ent.HasAccess() {
4143
return governance.FeatureAccess{HasAccess: true}
4244
}
45+
4346
return governance.FeatureAccess{
4447
HasAccess: false,
4548
Reason: &governance.AccessReason{

openmeter/governance/service/mapping_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"testing"
55

6+
"github.com/samber/lo"
67
"github.com/stretchr/testify/assert"
78

89
"github.com/openmeterio/openmeter/openmeter/entitlement"
@@ -28,7 +29,7 @@ func TestMapEntitlementToAccess(t *testing.T) {
2829
name: "metered exhausted — usage limit reached",
2930
value: &meteredentitlement.MeteredEntitlementValue{Balance: 0},
3031
wantHasAccess: false,
31-
wantCode: ptr(governance.ReasonUsageLimitReached),
32+
wantCode: lo.ToPtr(governance.ReasonUsageLimitReached),
3233
},
3334
{
3435
// BooleanEntitlementValue is always HasAccess=true; the gateway returns
@@ -48,14 +49,16 @@ func TestMapEntitlementToAccess(t *testing.T) {
4849
name: "no access value — feature unavailable",
4950
value: &entitlement.NoAccessValue{},
5051
wantHasAccess: false,
51-
wantCode: ptr(governance.ReasonFeatureUnavailable),
52+
wantCode: lo.ToPtr(governance.ReasonFeatureUnavailable),
5253
},
5354
}
5455

5556
for _, tc := range tests {
5657
t.Run(tc.name, func(t *testing.T) {
5758
got := mapEntitlementToAccess(tc.value)
59+
5860
assert.Equal(t, tc.wantHasAccess, got.HasAccess)
61+
5962
if tc.wantCode != nil {
6063
if assert.NotNil(t, got.Reason) {
6164
assert.Equal(t, *tc.wantCode, got.Reason.Code)
@@ -66,5 +69,3 @@ func TestMapEntitlementToAccess(t *testing.T) {
6669
})
6770
}
6871
}
69-
70-
func ptr[T any](v T) *T { return &v }

0 commit comments

Comments
 (0)