Skip to content

Commit

Permalink
feat(assert): safe type assertion (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdotwork authored Dec 13, 2022
1 parent 2186aeb commit e605442
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 55 deletions.
30 changes: 24 additions & 6 deletions add_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (adr *AddOnRequest) Get(ctx context.Context, addOnCode string) (*AddOn, *Er
return nil, err
}

addOnResult := result.(*AddOnResult)
addOnResult, ok := result.(*AddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return addOnResult.AddOn, nil
}
Expand All @@ -122,7 +125,10 @@ func (adr *AddOnRequest) GetList(ctx context.Context, addOnListInput *AddOnListI
return nil, clientErr
}

addOnResult := result.(*AddOnResult)
addOnResult, ok := result.(*AddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return addOnResult, nil
}
Expand All @@ -143,7 +149,10 @@ func (adr *AddOnRequest) Create(ctx context.Context, addOnInput *AddOnInput) (*A
return nil, err
}

addOnResult := result.(*AddOnResult)
addOnResult, ok := result.(*AddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return addOnResult.AddOn, nil
}
Expand All @@ -165,7 +174,10 @@ func (adr *AddOnRequest) Update(ctx context.Context, addOnInput *AddOnInput) (*A
return nil, err
}

addOnResult := result.(*AddOnResult)
addOnResult, ok := result.(*AddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return addOnResult.AddOn, nil
}
Expand All @@ -183,7 +195,10 @@ func (adr *AddOnRequest) Delete(ctx context.Context, addOnCode string) (*AddOn,
return nil, err
}

addOnResult := result.(*AddOnResult)
addOnResult, ok := result.(*AddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return addOnResult.AddOn, nil
}
Expand All @@ -204,7 +219,10 @@ func (adr *AddOnRequest) ApplyToCustomer(ctx context.Context, applyAddOnInput *A
return nil, err
}

appliedAddOnResult := result.(*AppliedAddOnResult)
appliedAddOnResult, ok := result.(*AppliedAddOnResult)
if !ok {
return nil, &ErrorTypeAssert
}

return appliedAddOnResult.AppliedAddOn, nil
}
25 changes: 20 additions & 5 deletions billable_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (bmr *BillableMetricRequest) Get(ctx context.Context, billableMetricCode st
return nil, err
}

billableMetricResult := result.(*BillableMetricResult)
billableMetricResult, ok := result.(*BillableMetricResult)
if !ok {
return nil, &ErrorTypeAssert
}

return billableMetricResult.BillableMetric, nil
}
Expand All @@ -95,7 +98,10 @@ func (bmr *BillableMetricRequest) GetList(ctx context.Context, billableMetricLis
return nil, clientErr
}

billableMetricResult := result.(*BillableMetricResult)
billableMetricResult, ok := result.(*BillableMetricResult)
if !ok {
return nil, &ErrorTypeAssert
}

return billableMetricResult, nil
}
Expand All @@ -112,7 +118,10 @@ func (bmr *BillableMetricRequest) Create(ctx context.Context, billableMetricInpu
return nil, err
}

billableMetricResult := result.(*BillableMetricResult)
billableMetricResult, ok := result.(*BillableMetricResult)
if !ok {
return nil, &ErrorTypeAssert
}

return billableMetricResult.BillableMetric, nil
}
Expand All @@ -130,7 +139,10 @@ func (bmr *BillableMetricRequest) Update(ctx context.Context, billableMetricInpu
return nil, err
}

billableMetricResult := result.(*BillableMetricResult)
billableMetricResult, ok := result.(*BillableMetricResult)
if !ok {
return nil, &ErrorTypeAssert
}

return billableMetricResult.BillableMetric, nil
}
Expand All @@ -147,7 +159,10 @@ func (bmr *BillableMetricRequest) Delete(ctx context.Context, billableMetricCode
return nil, err
}

billableMetricResult := result.(*BillableMetricResult)
billableMetricResult, ok := result.(*BillableMetricResult)
if !ok {
return nil, &ErrorTypeAssert
}

return billableMetricResult.BillableMetric, nil
}
35 changes: 28 additions & 7 deletions coupon.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ func (cr *CouponRequest) Get(ctx context.Context, couponCode string) (*Coupon, *
return nil, err
}

couponResult := result.(*CouponResult)
couponResult, ok := result.(*CouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return couponResult.Coupon, nil
}
Expand All @@ -189,7 +192,10 @@ func (cr *CouponRequest) GetList(ctx context.Context, couponListInput *CouponLis
return nil, clientErr
}

couponResult := result.(*CouponResult)
couponResult, ok := result.(*CouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return couponResult, nil
}
Expand All @@ -210,7 +216,10 @@ func (cr *CouponRequest) Create(ctx context.Context, couponInput *CouponInput) (
return nil, err
}

couponResult := result.(*CouponResult)
couponResult, ok := result.(*CouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return couponResult.Coupon, nil
}
Expand All @@ -232,7 +241,10 @@ func (cr *CouponRequest) Update(ctx context.Context, couponInput *CouponInput) (
return nil, err
}

couponResult := result.(*CouponResult)
couponResult, ok := result.(*CouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return couponResult.Coupon, nil
}
Expand All @@ -249,7 +261,10 @@ func (cr *CouponRequest) Delete(ctx context.Context, couponCode string) (*Coupon
return nil, err
}

couponResult := result.(*CouponResult)
couponResult, ok := result.(*CouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return couponResult.Coupon, nil
}
Expand All @@ -276,7 +291,10 @@ func (cr *AppliedCouponRequest) GetList(ctx context.Context, appliedCouponListIn
return nil, clientErr
}

appliedCouponResult := result.(*AppliedCouponResult)
appliedCouponResult, ok := result.(*AppliedCouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return appliedCouponResult, nil
}
Expand All @@ -297,7 +315,10 @@ func (cr *CouponRequest) ApplyToCustomer(ctx context.Context, applyCouponInput *
return nil, err
}

appliedCouponResult := result.(*AppliedCouponResult)
appliedCouponResult, ok := result.(*AppliedCouponResult)
if !ok {
return nil, &ErrorTypeAssert
}

return appliedCouponResult.AppliedCoupon, nil
}
30 changes: 24 additions & 6 deletions credit_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func (cr *CreditNoteRequest) Get(ctx context.Context, creditNoteID uuid.UUID) (*
return nil, err
}

creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult.CreditNote, nil
}
Expand All @@ -150,7 +153,10 @@ func (cr *CreditNoteRequest) Download(ctx context.Context, creditNoteID string)
}

if result != nil {
creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult.CreditNote, nil
}
Expand Down Expand Up @@ -180,7 +186,10 @@ func (cr *CreditNoteRequest) GetList(ctx context.Context, creditNoteListInput *C
return nil, clientErr
}

creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult, nil
}
Expand All @@ -201,7 +210,10 @@ func (cr *CreditNoteRequest) Create(ctx context.Context, creditNoteInput *Credit
return nil, err
}

creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult.CreditNote, nil
}
Expand All @@ -223,7 +235,10 @@ func (cr *CreditNoteRequest) Update(ctx context.Context, creditNoteUpdateInput *
return nil, err
}

creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult.CreditNote, nil
}
Expand All @@ -241,7 +256,10 @@ func (cr *CreditNoteRequest) Void(ctx context.Context, creditNoteID string) (*Cr
return nil, err
}

creditNoteResult := result.(*CreditNoteResult)
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}

return creditNoteResult.CreditNote, nil
}
20 changes: 16 additions & 4 deletions customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func (cr *CustomerRequest) Create(ctx context.Context, customerInput *CustomerIn
return nil, err
}

customerResult := result.(*CustomerResult)
customerResult, ok := result.(*CustomerResult)
if !ok {
return nil, &ErrorTypeAssert
}

return customerResult.Customer, nil
}
Expand Down Expand Up @@ -182,7 +185,10 @@ func (cr *CustomerRequest) CurrentUsage(ctx context.Context, externalCustomerID
return nil, clientErr
}

currentUsageResult := result.(*CustomerUsageResult)
currentUsageResult, ok := result.(*CustomerUsageResult)
if !ok {
return nil, &ErrorTypeAssert
}

return currentUsageResult.CustomerUsage, nil
}
Expand All @@ -199,7 +205,10 @@ func (cr *CustomerRequest) Get(ctx context.Context, externalCustomerID string) (
return nil, err
}

customerResult := result.(*CustomerResult)
customerResult, ok := result.(*CustomerResult)
if !ok {
return nil, &ErrorTypeAssert
}

return customerResult.Customer, nil
}
Expand All @@ -226,7 +235,10 @@ func (cr *CustomerRequest) GetList(ctx context.Context, customerListInput *Custo
return nil, clientErr
}

customerResult := result.(*CustomerResult)
customerResult, ok := result.(*CustomerResult)
if !ok {
return nil, &ErrorTypeAssert
}

return customerResult, nil
}
12 changes: 11 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package lago

import (
"errors"
"net/http"
)

type ErrorCode string

const (
ErrorCodeAlreadyExist ErrorCode = "value_already_exist"
ErrorCodeInvalidValue
ErrorTypeAssert ErrorCode = "error_type_assert"
)

var ErrorTypeAssert = Error{
Err: errors.New("type assertion failed"),
HTTPStatusCode: http.StatusUnprocessableEntity,
Msg: "type assertion failed",
}

type ErrorDetail struct {
ErrorCode []ErrorCode `json:"code,omitempty"`
}
Expand Down
5 changes: 4 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func (cr *GroupRequest) GetList(ctx context.Context, groupListInput *GroupListIn
return nil, clientErr
}

groupResult := result.(*GroupResult)
groupResult, ok := result.(*GroupResult)
if !ok {
return nil, &ErrorTypeAssert
}

return groupResult, nil
}
Loading

0 comments on commit e605442

Please sign in to comment.