Skip to content

Commit

Permalink
Check ptr before mapping to wallet objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sonda2208 committed Mar 18, 2020
1 parent b8d5b47 commit 39511bc
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 29 deletions.
36 changes: 35 additions & 1 deletion gpass_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
package gpass
package gpass_test

import (
"context"
"log"
"testing"

"github.com/sonda2208/gpass"
"github.com/stretchr/testify/require"
)

func TestGPass(t *testing.T) {
ctx := context.Background()
cli, err := gpass.NewClient(ctx, "/Users/sonda/Downloads/mmpt-233505-c59ca5545a08.json")
require.NoError(t, err)

oc := cli.Issuer(3304132759545979026).OfferClass("3304132759545979026.CreditPlusDemo")

ocm, err := oc.Metadata(ctx)
if err != nil {
log.Fatal(err)
}

_ = ocm
_, err = oc.Update(ctx, &gpass.OfferClassMetadataToUpdate{
ReviewStatus: "UNDER_REVIEW",
LocalizedShortTitle: &gpass.LocalizedString{
DefaultValue: &gpass.TranslatedString{
Language: "en",
Value: "Status: Processing...",
},
},
})
require.NoError(t, err)
}
12 changes: 8 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ type Image struct {

func (img *Image) toWO() *walletobjects.Image {
return &walletobjects.Image{
Kind: "walletobjects#image",
SourceUri: img.SourceURI.toWO(),
Kind: "walletobjects#image",
SourceUri: img.SourceURI.toWO(),
}
}

func woToImage(img *walletobjects.Image) *Image {
if img == nil {
return nil
}

return &Image{
SourceURI:woToImageUri(img.SourceUri),
SourceURI: woToImageUri(img.SourceUri),
}
}
}
34 changes: 22 additions & 12 deletions issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package gpass

import (
"context"
"errors"

"github.com/sonda2208/gpass/walletobjects"
)

type Issuer struct {
IssuerID int64
c *Client
c *Client
}

func (c *Client) Issuer(id int64) *Issuer {
Expand All @@ -27,7 +29,7 @@ func (c *Client) Issuers(ctx context.Context) ([]*Issuer, error) {
for i, iss := range res.Resources {
issuers[i] = toIssuer(iss, c)
}

return issuers, nil
}

Expand Down Expand Up @@ -58,9 +60,9 @@ func (iss *Issuer) Metadata(ctx context.Context) (*IssuerMetadata, error) {
return nil, err
}

meta, err := woToIssuerMetadata(i)
if err != nil {
return nil, err
meta := woToIssuerMetadata(i)
if meta == nil {
return nil, errors.New("invalid metadata")
}

return meta, nil
Expand All @@ -69,7 +71,7 @@ func (iss *Issuer) Metadata(ctx context.Context) (*IssuerMetadata, error) {
type IssuerMetadata struct {
ContactInfo *IssuerContactInfo
HomepageURL string
Name string
Name string
}

func (im *IssuerMetadata) toWO() (*walletobjects.Issuer, error) {
Expand All @@ -84,20 +86,24 @@ func (im *IssuerMetadata) toWO() (*walletobjects.Issuer, error) {
return i, nil
}

func woToIssuerMetadata(i *walletobjects.Issuer) (*IssuerMetadata, error ) {
func woToIssuerMetadata(i *walletobjects.Issuer) *IssuerMetadata {
if i == nil {
return nil
}

meta := &IssuerMetadata{
ContactInfo: woToIssuerContactInfo(i.ContactInfo),
HomepageURL: i.HomepageUrl,
Name: i.Name,
}
return meta, nil
return meta
}

type IssuerContactInfo struct {
AlertsEmails []string
Email string
Name string
Phone string
Email string
Name string
Phone string
}

func (ici *IssuerContactInfo) toWO() *walletobjects.IssuerContactInfo {
Expand All @@ -114,10 +120,14 @@ func (ici *IssuerContactInfo) toWO() *walletobjects.IssuerContactInfo {
}

func woToIssuerContactInfo(i *walletobjects.IssuerContactInfo) *IssuerContactInfo {
if i == nil {
return nil
}

return &IssuerContactInfo{
AlertsEmails: i.AlertsEmails,
Email: i.Email,
Name: i.Name,
Phone: i.Phone,
}
}
}
4 changes: 4 additions & 0 deletions links_module_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (d *LinksModuleData) toWO() *walletobjects.LinksModuleData {
}

func woToLinksModuleData(d *walletobjects.LinksModuleData) *LinksModuleData {
if d == nil {
return nil
}

res := &LinksModuleData{
URIs: make([]*URI, len(d.Uris)),
}
Expand Down
4 changes: 4 additions & 0 deletions localized_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (ls *LocalizedString) toWO() *walletobjects.LocalizedString {
}

func woToLocalizedString(s *walletobjects.LocalizedString) *LocalizedString {
if s == nil {
return nil
}

res := &LocalizedString{
DefaultValue: woToTranslatedString(s.DefaultValue),
TranslatedValues: make([]*TranslatedString, len(s.TranslatedValues)),
Expand Down
4 changes: 4 additions & 0 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func locationListToWO(ll []*LatLongPoint) []*walletobjects.LatLongPoint {
}

func woToLatLongPoint(llp *walletobjects.LatLongPoint) *LatLongPoint {
if llp == nil {
return nil
}

return &LatLongPoint{
Latitude: llp.Latitude,
Longitude: llp.Longitude,
Expand Down
8 changes: 8 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (m *Message) toWO() *walletobjects.Message {
}

func woToMessage(m *walletobjects.Message) *Message {
if m == nil {
return nil
}

return &Message{
Body: m.Body,
Header: m.Header,
Expand All @@ -34,6 +38,10 @@ func (amr *AddMessageRequest) toWO() *walletobjects.AddMessageRequest {
}

func woToAddMessageRequest(amr *walletobjects.AddMessageRequest) *AddMessageRequest {
if amr == nil {
return nil
}

return &AddMessageRequest{
Message: woToMessage(amr.Message),
}
Expand Down
21 changes: 15 additions & 6 deletions offerclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gpass

import (
"context"
"errors"

"github.com/sonda2208/gpass/walletobjects"
)
Expand Down Expand Up @@ -36,6 +37,10 @@ func (iss *Issuer) OfferClasses(ctx context.Context) ([]*OfferClass, error) {
}

func toOfferClass(oc *walletobjects.OfferClass, c *Client) *OfferClass {
if oc == nil {
return nil
}

return &OfferClass{
OfferClassID: oc.Id,
c: c,
Expand Down Expand Up @@ -63,9 +68,9 @@ func (oc *OfferClass) Metadata(ctx context.Context) (*OfferClassMetadata, error)
return nil, err
}

meta, err := woToOfferClassMeta(o)
if err != nil {
return nil, err
meta := woToOfferClassMeta(o)
if meta == nil {
return nil, errors.New("invalid metadata")
}

return meta, nil
Expand All @@ -82,7 +87,7 @@ func (oc *OfferClass) Update(ctx context.Context, ocm *OfferClassMetadataToUpdat
return nil, err
}

return woToOfferClassMeta(res)
return woToOfferClassMeta(res), nil
}

func (oc *OfferClass) AddMessage(ctx context.Context, amr *AddMessageRequest) error {
Expand Down Expand Up @@ -122,7 +127,11 @@ func (ocm *OfferClassMetadata) toWO() (*walletobjects.OfferClass, error) {
}, nil
}

func woToOfferClassMeta(o *walletobjects.OfferClass) (*OfferClassMetadata, error) {
func woToOfferClassMeta(o *walletobjects.OfferClass) *OfferClassMetadata {
if o == nil {
return nil
}

ocm := &OfferClassMetadata{
IssuerName: o.IssuerName,
Provider: o.Provider,
Expand All @@ -133,7 +142,7 @@ func woToOfferClassMeta(o *walletobjects.OfferClass) (*OfferClassMetadata, error
LocalizedShortTitle: woToLocalizedString(o.LocalizedShortTitle),
LocalizedTitle: woToLocalizedString(o.LocalizedTitle),
}
return ocm, nil
return ocm
}

type OfferClassMetadataToUpdate struct {
Expand Down
17 changes: 11 additions & 6 deletions offerobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gpass

import (
"context"
"errors"

"github.com/sonda2208/gpass/walletobjects"
)
Expand Down Expand Up @@ -67,9 +68,9 @@ func (oo *OfferObject) Metadata(ctx context.Context) (*OfferObjectMetadata, erro
return nil, err
}

meta, err := woToOfferObjectMeta(o)
if err != nil {
return nil, err
meta := woToOfferObjectMeta(o)
if meta == nil {
return nil, errors.New("invalid metadata")
}

return meta, nil
Expand All @@ -86,7 +87,7 @@ func (oo *OfferObject) Update(ctx context.Context, oom *OfferObjectMetadataToUpd
return nil, err
}

return woToOfferObjectMeta(res)
return woToOfferObjectMeta(res), nil
}

func (oo *OfferObject) AddMessage(ctx context.Context, amr *AddMessageRequest) error {
Expand Down Expand Up @@ -117,7 +118,11 @@ func (oom *OfferObjectMetadata) toWO() (*walletobjects.OfferObject, error) {
return o, nil
}

func woToOfferObjectMeta(oo *walletobjects.OfferObject) (*OfferObjectMetadata, error) {
func woToOfferObjectMeta(oo *walletobjects.OfferObject) *OfferObjectMetadata {
if oo == nil {
return nil
}

oom := &OfferObjectMetadata{
State: oo.State,
Locations: make([]*LatLongPoint, len(oo.Locations)),
Expand All @@ -134,7 +139,7 @@ func woToOfferObjectMeta(oo *walletobjects.OfferObject) (*OfferObjectMetadata, e
oom.TextModulesData[i] = woToTextModuleData(d)
}

return oom, nil
return oom
}

type OfferObjectMetadataToUpdate struct {
Expand Down
4 changes: 4 additions & 0 deletions text_module_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func listTextModuleDataToWO(d []*TextModuleData) []*walletobjects.TextModuleData
}

func woToTextModuleData(d *walletobjects.TextModuleData) *TextModuleData {
if d == nil {
return nil
}

return &TextModuleData{
Body: d.Body,
Header: d.Header,
Expand Down
4 changes: 4 additions & 0 deletions translated_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func listTranslatedStringToWO(ts []*TranslatedString) []*walletobjects.Translate
}

func woToTranslatedString(s *walletobjects.TranslatedString) *TranslatedString {
if s == nil {
return nil
}

return &TranslatedString{
Language: s.Language,
Value: s.Value,
Expand Down
8 changes: 8 additions & 0 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func listURIToWO(uris []*URI) []*walletobjects.Uri {
}

func woToUri(u *walletobjects.Uri) *URI {
if u == nil {
return nil
}

return &URI{
ID: u.Id,
URI: u.Uri,
Expand All @@ -47,6 +51,10 @@ func (iu *ImageUri) toWO() *walletobjects.ImageUri {
}

func woToImageUri(u *walletobjects.ImageUri) *ImageUri {
if u == nil {
return nil
}

return &ImageUri{
URI: u.Uri,
LocalizedDescription: woToLocalizedString(u.LocalizedDescription),
Expand Down

0 comments on commit 39511bc

Please sign in to comment.