From 39511bcd75b6fb72b001889dc6fbf0ee73fc65ad Mon Sep 17 00:00:00 2001 From: sonda2208 Date: Wed, 18 Mar 2020 14:23:07 +0700 Subject: [PATCH] Check ptr before mapping to wallet objects --- gpass_test.go | 36 +++++++++++++++++++++++++++++++++++- image.go | 12 ++++++++---- issuer.go | 34 ++++++++++++++++++++++------------ links_module_data.go | 4 ++++ localized_string.go | 4 ++++ location.go | 4 ++++ message.go | 8 ++++++++ offerclass.go | 21 +++++++++++++++------ offerobject.go | 17 +++++++++++------ text_module_data.go | 4 ++++ translated_string.go | 4 ++++ uri.go | 8 ++++++++ 12 files changed, 127 insertions(+), 29 deletions(-) diff --git a/gpass_test.go b/gpass_test.go index 92a9cc5..70ff3ba 100644 --- a/gpass_test.go +++ b/gpass_test.go @@ -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) +} diff --git a/image.go b/image.go index 93eb53f..c62249b 100644 --- a/image.go +++ b/image.go @@ -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), } -} \ No newline at end of file +} diff --git a/issuer.go b/issuer.go index 17a8ce8..cb0f4d8 100644 --- a/issuer.go +++ b/issuer.go @@ -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 { @@ -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 } @@ -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 @@ -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) { @@ -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 { @@ -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, } -} \ No newline at end of file +} diff --git a/links_module_data.go b/links_module_data.go index 69a2d4f..7527886 100644 --- a/links_module_data.go +++ b/links_module_data.go @@ -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)), } diff --git a/localized_string.go b/localized_string.go index dfe78db..7c27392 100644 --- a/localized_string.go +++ b/localized_string.go @@ -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)), diff --git a/location.go b/location.go index 8d18d45..7d6848b 100644 --- a/location.go +++ b/location.go @@ -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, diff --git a/message.go b/message.go index c7c7b6c..5c1b80d 100644 --- a/message.go +++ b/message.go @@ -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, @@ -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), } diff --git a/offerclass.go b/offerclass.go index 4f52096..bfe4c15 100644 --- a/offerclass.go +++ b/offerclass.go @@ -2,6 +2,7 @@ package gpass import ( "context" + "errors" "github.com/sonda2208/gpass/walletobjects" ) @@ -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, @@ -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 @@ -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 { @@ -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, @@ -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 { diff --git a/offerobject.go b/offerobject.go index fef0add..ed80157 100644 --- a/offerobject.go +++ b/offerobject.go @@ -2,6 +2,7 @@ package gpass import ( "context" + "errors" "github.com/sonda2208/gpass/walletobjects" ) @@ -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 @@ -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 { @@ -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)), @@ -134,7 +139,7 @@ func woToOfferObjectMeta(oo *walletobjects.OfferObject) (*OfferObjectMetadata, e oom.TextModulesData[i] = woToTextModuleData(d) } - return oom, nil + return oom } type OfferObjectMetadataToUpdate struct { diff --git a/text_module_data.go b/text_module_data.go index 66fe423..c8b3e4f 100644 --- a/text_module_data.go +++ b/text_module_data.go @@ -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, diff --git a/translated_string.go b/translated_string.go index a669be7..e8dd30b 100644 --- a/translated_string.go +++ b/translated_string.go @@ -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, diff --git a/uri.go b/uri.go index 05313a6..f885abd 100644 --- a/uri.go +++ b/uri.go @@ -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, @@ -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),