From a29b575d2c75116a562bf385339517c31d82ed06 Mon Sep 17 00:00:00 2001 From: Cristian Vidmar Date: Tue, 12 May 2020 14:43:39 +0200 Subject: [PATCH] Asset Map Includes for multi-locale queries --- collection.go | 14 ++++++++++++++ include.go | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/collection.go b/collection.go index 86866c5..fbab75d 100644 --- a/collection.go +++ b/collection.go @@ -234,3 +234,17 @@ func (col *Collection) ToIncludesAssetMap() map[string]*IncludeAsset { } return includesAssetMap } + +// ToIncludesLocalizedAssetMap returns a map of Asset's from the Includes +func (col *Collection) ToIncludesLocalizedAssetMap() map[string]*IncludeLocalizedAsset { + var includesAsset []*IncludeLocalizedAsset + includesAssetMap := make(map[string]*IncludeLocalizedAsset) + + byteArray, _ := json.Marshal(col.Includes["Asset"]) + json.NewDecoder(bytes.NewReader(byteArray)).Decode(&includesAsset) + + for _, a := range includesAsset { + includesAssetMap[a.Sys.ID] = a + } + return includesAssetMap +} diff --git a/include.go b/include.go index 519158d..0db1edb 100644 --- a/include.go +++ b/include.go @@ -1,11 +1,5 @@ package contentful -/* // Include model -type Include struct { - Entry []*Entry - Asset []*Asset -} */ - //IncludeEntry model type IncludeEntry struct { Fields map[string]interface{} `json:"fields,omitempty"` @@ -24,3 +18,16 @@ type IncludeAsset struct { Fields *IncludeFileFields `json:"fields"` Sys *Sys `json:"sys"` } + +// IncludeFileLocalizedFields model +type IncludeFileLocalizedFields struct { + Title map[string]string `json:"title,omitempty"` + Description map[string]string `json:"description,omitempty"` + File map[string]*File `json:"file,omitempty"` +} + +// IncludeLocalizedAsset model +type IncludeLocalizedAsset struct { + Fields *IncludeFileLocalizedFields `json:"fields"` + Sys *Sys `json:"sys"` +}