Skip to content

Commit

Permalink
Merge pull request #1074 from openmeterio/fix/make-sure-no-nil-pointe…
Browse files Browse the repository at this point in the history
…r-casting-happens

fix: make sure we are handling nil maps
  • Loading branch information
turip authored Jun 27, 2024
2 parents 3cab451 + 0509d69 commit db7876a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/entitlement/httpdriver/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (parser) ToMetered(e *entitlement.Entitlement) (*api.EntitlementMetered, er
Id: &metered.ID,
IsUnlimited: convert.ToPointer(false), // implement
IssueAfterReset: metered.IssuesAfterReset,
Metadata: &metered.Metadata,
Metadata: convert.MapToPointer(metered.Metadata),
SubjectKey: metered.SubjectKey,
Type: api.EntitlementMeteredType(metered.EntitlementType),
UpdatedAt: &metered.UpdatedAt,
Expand All @@ -50,7 +50,7 @@ func (parser) ToStatic(e *entitlement.Entitlement) (*api.EntitlementStatic, erro
DeletedAt: static.DeletedAt,
FeatureId: static.FeatureID,
Id: &static.ID,
Metadata: &static.Metadata,
Metadata: convert.MapToPointer(static.Metadata),
SubjectKey: static.SubjectKey,
Type: api.EntitlementStaticType(static.EntitlementType),
UpdatedAt: &static.UpdatedAt,
Expand All @@ -73,7 +73,7 @@ func (parser) ToBoolean(e *entitlement.Entitlement) (*api.EntitlementBoolean, er
DeletedAt: boolean.DeletedAt,
FeatureId: boolean.FeatureID,
Id: &boolean.ID,
Metadata: &boolean.Metadata,
Metadata: convert.MapToPointer(boolean.Metadata),
SubjectKey: boolean.SubjectKey,
Type: api.EntitlementBooleanType(boolean.EntitlementType),
UpdatedAt: &boolean.UpdatedAt,
Expand Down
7 changes: 7 additions & 0 deletions pkg/convert/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ func ToPointer[T any](value T) *T {
return &value
}

func MapToPointer[T comparable, U any](value map[T]U) *map[T]U {
if len(value) == 0 {
return nil
}
return &value
}

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

0 comments on commit db7876a

Please sign in to comment.