From da162fc524a91abcc05bf9742c5442e91a21baa8 Mon Sep 17 00:00:00 2001 From: Florian Besser Date: Tue, 20 Apr 2021 13:45:15 +0800 Subject: [PATCH 1/3] Make description an optional field Allow localized unmarshalling This should fix https://github.com/contentful-labs/contentful-go/issues/45 --- asset.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/asset.go b/asset.go index 661dc5a..e82c914 100644 --- a/asset.go +++ b/asset.go @@ -108,13 +108,19 @@ func (asset *Asset) UnmarshalJSON(data []byte) error { } asset.Fields = &FileFields{ - Title: title.(string), - Description: description.(string), - File: &File{}, + Title: title.(string), + File: &File{}, + } + if description != nil { + asset.Fields.Description = description.(string) } file := payload["fields"].(map[string]interface{})["file"].(map[string]interface{})[asset.locale] - if err := json.Unmarshal([]byte(file.(string)), asset.Fields.File); err != nil { + marshal, err := json.Marshal(file.(map[string]interface{})) + if err != nil { + return err + } + if err := json.Unmarshal(marshal, asset.Fields.File); err != nil { return err } } else { @@ -171,6 +177,24 @@ func (service *AssetsService) Get(spaceID, assetID string) (*Asset, error) { return &asset, nil } +// Get returns a single asset entity +func (service *AssetsService) GetLocalized(spaceID, assetID string, local string) (*Asset, error) { + path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID) + method := "GET" + + req, err := service.c.newRequest(method, path, nil, nil) + if err != nil { + return nil, err + } + + asset := Asset{locale: local} + if err := service.c.do(req, &asset); err != nil { + return nil, err + } + + return &asset, nil +} + // Upsert updates or creates a new asset entity func (service *AssetsService) Upsert(spaceID string, asset *Asset) error { bytesArray, err := json.Marshal(asset) From bc56c8e0c205277fad145dc22d5868053e3f5919 Mon Sep 17 00:00:00 2001 From: Florian Besser Date: Tue, 20 Apr 2021 13:47:05 +0800 Subject: [PATCH 2/3] Update documentation --- asset.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asset.go b/asset.go index e82c914..a05b98a 100644 --- a/asset.go +++ b/asset.go @@ -177,7 +177,7 @@ func (service *AssetsService) Get(spaceID, assetID string) (*Asset, error) { return &asset, nil } -// Get returns a single asset entity +// GetLocalized returns a single asset entity. You need to specify the target locale you wish to retrieve. func (service *AssetsService) GetLocalized(spaceID, assetID string, local string) (*Asset, error) { path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID) method := "GET" From d3674b09bf8c1cb38b68446184cf89ab1e8e8eda Mon Sep 17 00:00:00 2001 From: Florian Besser Date: Tue, 20 Apr 2021 13:47:30 +0800 Subject: [PATCH 3/3] Typo --- asset.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asset.go b/asset.go index a05b98a..1d6a2ff 100644 --- a/asset.go +++ b/asset.go @@ -178,7 +178,7 @@ func (service *AssetsService) Get(spaceID, assetID string) (*Asset, error) { } // GetLocalized returns a single asset entity. You need to specify the target locale you wish to retrieve. -func (service *AssetsService) GetLocalized(spaceID, assetID string, local string) (*Asset, error) { +func (service *AssetsService) GetLocalized(spaceID, assetID string, locale string) (*Asset, error) { path := fmt.Sprintf("/spaces/%s/assets/%s", spaceID, assetID) method := "GET" @@ -187,7 +187,7 @@ func (service *AssetsService) GetLocalized(spaceID, assetID string, local string return nil, err } - asset := Asset{locale: local} + asset := Asset{locale: locale} if err := service.c.do(req, &asset); err != nil { return nil, err }