Skip to content

Commit

Permalink
fix(*): address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GAlexIHU committed May 30, 2024
1 parent a950b78 commit 80d8f1c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
2 changes: 2 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ func TestCredit(t *testing.T) {
Type: api.LedgerGrantTypeUsage,
FeatureID: *featureId,
Amount: 100,
LedgerID: string(ledgerID),
Priority: &priority,
EffectiveAt: effectiveAt,
Rollover: &api.LedgerGrantRollover{
Expand Down Expand Up @@ -697,6 +698,7 @@ func TestCredit(t *testing.T) {
},
ExpiresAt: grant.ExpiresAt,
FeatureID: *feature.Id,
ParentId: grant.ParentId,
Metadata: grant.Metadata,
Priority: grant.Priority,
Rollover: grant.Rollover,
Expand Down
19 changes: 4 additions & 15 deletions internal/credit/creditdriver/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/internal/credit"
"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
Expand Down Expand Up @@ -88,14 +89,10 @@ func mapBalanceToAPI(balance credit.Balance, highwatermark credit.HighWatermark)
}

func mapFeatureBalanceToAPI(featureBalance credit.FeatureBalance) api.FeatureBalance {
id := new(string)
if featureBalance.ID != nil {
*id = string(*featureBalance.ID)
}
return api.FeatureBalance{
Archived: featureBalance.Archived,
CreatedAt: featureBalance.CreatedAt,
Id: id,
Id: convert.ToStringLike[credit.FeatureID, string](featureBalance.ID),
MeterGroupByFilters: featureBalance.MeterGroupByFilters,
MeterSlug: featureBalance.MeterSlug,
Name: featureBalance.Name,
Expand All @@ -106,14 +103,6 @@ func mapFeatureBalanceToAPI(featureBalance credit.FeatureBalance) api.FeatureBal
}

func mapGrantBalanceToAPI(grantBalance credit.GrantBalance) api.LedgerGrantBalance {
var id *string
if grantBalance.ID != nil {
*id = string(*grantBalance.ID)
}
var parentId *string
if grantBalance.ParentID != nil {
*parentId = string(*grantBalance.ParentID)
}
priority := int(grantBalance.Priority)

return api.LedgerGrantBalance{
Expand All @@ -127,9 +116,9 @@ func mapGrantBalanceToAPI(grantBalance credit.GrantBalance) api.LedgerGrantBalan
},
ExpiresAt: &grantBalance.ExpiresAt,
FeatureID: string(*grantBalance.FeatureID),
Id: id,
Id: convert.ToStringLike[credit.GrantID, string](grantBalance.ID),
Metadata: &grantBalance.Metadata,
ParentId: parentId,
ParentId: convert.ToStringLike[credit.GrantID, string](grantBalance.ParentID),
Priority: &priority,
Rollover: grantBalance.Rollover,
Type: api.LedgerGrantType(grantBalance.Type),
Expand Down
25 changes: 3 additions & 22 deletions internal/credit/creditdriver/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,7 @@ func mapGrantToAPI(grant credit.Grant) api.LedgerGrantResponse {
}

func mapGrantWithBalanceToAPI(grant credit.Grant) api.LedgerGrantResponse {
priority := int(grant.Priority)

return api.LedgerGrantResponse{
Amount: grant.Amount,
CreatedAt: grant.CreatedAt,
EffectiveAt: grant.EffectiveAt,
Expiration: &api.LedgerGrantExpirationPeriod{
Count: int(grant.Expiration.Count),
Duration: api.LedgerGrantExpirationPeriodDuration(grant.Expiration.Duration),
},
ExpiresAt: &grant.ExpiresAt,
FeatureID: string(defaultx.WithDefault(grant.FeatureID, credit.FeatureID(""))),
Id: (*string)(grant.ID),
LedgerID: string(grant.LedgerID),
Metadata: &grant.Metadata,
ParentId: (*string)(grant.ParentID),
Priority: &priority,
Rollover: grant.Rollover,
Type: api.LedgerGrantType(grant.Type),
UpdatedAt: grant.UpdatedAt,
Void: &grant.Void,
}
res := mapGrantToAPI(grant)
res.LedgerID = string(grant.LedgerID)
return res
}
7 changes: 2 additions & 5 deletions internal/credit/creditdriver/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/internal/credit"
"github.com/openmeterio/openmeter/pkg/convert"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
Expand Down Expand Up @@ -136,19 +137,15 @@ func (b *builder) GetLedgerHistory() GetLedgerHistoryHandler {
}

func mapLedgerEntry(entry credit.LedgerEntry) api.LedgerEntry {
var entryId *string
var period *api.Period
if entry.ID != nil {
entryId = (*string)(entry.ID)
}
if entry.Period != nil {
period = &api.Period{
From: entry.Period.From,
To: entry.Period.To,
}
}
return api.LedgerEntry{
Id: entryId,
Id: convert.ToStringLike[credit.GrantID, string](entry.ID),
Type: api.LedgerEntryType(entry.Type),
Time: entry.Time,
FeatureID: string(defaultx.WithDefault(entry.FeatureID, credit.FeatureID(""))),
Expand Down
2 changes: 1 addition & 1 deletion internal/credit/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type FeatureWithNameAlreadyExistsError struct {
func (e *FeatureWithNameAlreadyExistsError) Error() string {
// Is it an issue that we leak ID on another Feature here?
// Shouldn't be an isue as it's namespaced.
return fmt.Sprintf("Feature %s with name %s already exists.", e.ID, e.Name)
return fmt.Sprintf("feature %s with name %s already exists", e.ID, e.Name)
}

// Feature is a feature or service offered to a customer.
Expand Down
7 changes: 7 additions & 0 deletions pkg/convert/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ package convert
func ToPointer[T any](value T) *T {
return &value
}

func ToStringLike[Source, Dest ~string](value *Source) *Dest {
if value == nil {
return nil
}
return ToPointer(Dest(*value))
}

0 comments on commit 80d8f1c

Please sign in to comment.