Skip to content

Commit

Permalink
Make description an optional field
Browse files Browse the repository at this point in the history
Allow localized unmarshalling

This should fix contentful-labs#45
  • Loading branch information
Florian Besser committed Apr 20, 2021
1 parent c2cba1b commit da162fc
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit da162fc

Please sign in to comment.