Skip to content

Commit 73cf10c

Browse files
author
Cristian Vidmar
committed
fix: cache update fails on new entries
1 parent 445a1e1 commit 73cf10c

6 files changed

+38
-25
lines changed

erm/templates/contentful_vo_lib_contenttype.gotmpl

+1-4
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,7 @@ func (cc *ContentfulClient) cacheAll{{ firstCap $contentType.Sys.ID }}(ctx conte
637637
for _, {{ $contentType.Sys.ID }} := range all{{ firstCap $contentType.Sys.ID }} {
638638
if cc.Cache != nil {
639639
existing{{ firstCap $contentType.Sys.ID }}, err := cc.Get{{ firstCap $contentType.Sys.ID }}ByID({{ $contentType.Sys.ID }}.Sys.ID)
640-
if err != nil {
641-
return nil, err
642-
}
643-
if existing{{ firstCap $contentType.Sys.ID }}.Sys.Version > {{ $contentType.Sys.ID }}.Sys.Version {
640+
if err == nil && existing{{ firstCap $contentType.Sys.ID }} != nil && existing{{ firstCap $contentType.Sys.ID }}.Sys.Version > {{ $contentType.Sys.ID }}.Sys.Version {
644641
return nil, fmt.Errorf("cache update canceled because {{ firstCap $contentType.Sys.ID }} entry %s is newer in cache", {{ $contentType.Sys.ID }}.Sys.ID)
645642
}
646643
}

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/foomo/gocontentful/erm"
1212
)
1313

14-
var VERSION = "v1.0.7"
14+
var VERSION = "v1.0.8"
1515

1616
var Usage = func() {
1717
fmt.Printf("\nSYNOPSIS\n")

test/testapi/gocontentfulvolib.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ type ContentTypeResult struct {
7171
References map[string][]EntryReference
7272
}
7373

74+
type ContentTypeInfo struct {
75+
ContentType string
76+
Title string
77+
Description string
78+
}
79+
80+
type ContentTypeInfoMap map[string]ContentTypeInfo
81+
7482
type EntryReference struct {
7583
ContentType string
7684
ID string
@@ -142,6 +150,23 @@ var (
142150
)
143151

144152
var spaceContentTypes = []string{ContentTypeBrand, ContentTypeCategory, ContentTypeProduct}
153+
var SpaceContentTypeInfoMap = ContentTypeInfoMap{
154+
"brand": ContentTypeInfo{
155+
ContentType: "brand",
156+
Title: "Brand",
157+
Description: "",
158+
},
159+
"category": ContentTypeInfo{
160+
ContentType: "category",
161+
Title: "Category",
162+
Description: "",
163+
},
164+
"product": ContentTypeInfo{
165+
ContentType: "product",
166+
Title: "Product",
167+
Description: "",
168+
},
169+
}
145170

146171
func (cc *ContentfulClient) BrokenReferences() (brokenReferences []BrokenReference) {
147172
if cc.Cache == nil {
@@ -235,11 +260,11 @@ func (cc *ContentfulClient) GetAllAssets() (map[string]*contentful.Asset, error)
235260
return cc.getAllAssets(true)
236261
}
237262

238-
func (cc *ContentfulClient) GetAssetByID(id string) (*contentful.Asset, error) {
263+
func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*contentful.Asset, error) {
239264
if cc == nil || cc.Client == nil {
240265
return nil, errors.New("GetAssetByID: No client available")
241266
}
242-
if cc.Cache != nil && cc.Cache.assets != nil {
267+
if cc.Cache != nil && cc.Cache.assets != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
243268
cc.Cache.assetsGcLock.RLock()
244269
asset, okAsset := cc.Cache.assets[id]
245270
cc.Cache.assetsGcLock.RUnlock()

test/testapi/gocontentfulvolibbrand.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredBrand(query *contentful.Query) (voMap map
6464
return brandMap, nil
6565
}
6666

67-
func (cc *ContentfulClient) GetBrandByID(id string) (vo *CfBrand, err error) {
67+
func (cc *ContentfulClient) GetBrandByID(id string, forceNoCache ...bool) (vo *CfBrand, err error) {
6868
if cc == nil || cc.Client == nil {
6969
return nil, errors.New("GetBrandByID: No client available")
7070
}
71-
if cc.Cache != nil {
71+
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
7272
cc.Cache.entryMaps.brandGcLock.RLock()
7373
vo, ok := cc.Cache.entryMaps.brand[id]
7474
cc.Cache.entryMaps.brandGcLock.RUnlock()
@@ -725,10 +725,7 @@ func (cc *ContentfulClient) cacheAllBrand(ctx context.Context, resultChan chan<-
725725
for _, brand := range allBrand {
726726
if cc.Cache != nil {
727727
existingBrand, err := cc.GetBrandByID(brand.Sys.ID)
728-
if err != nil {
729-
return nil, err
730-
}
731-
if existingBrand.Sys.Version > brand.Sys.Version {
728+
if err == nil && existingBrand != nil && existingBrand.Sys.Version > brand.Sys.Version {
732729
return nil, fmt.Errorf("cache update canceled because Brand entry %s is newer in cache", brand.Sys.ID)
733730
}
734731
}

test/testapi/gocontentfulvolibcategory.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredCategory(query *contentful.Query) (voMap
6464
return categoryMap, nil
6565
}
6666

67-
func (cc *ContentfulClient) GetCategoryByID(id string) (vo *CfCategory, err error) {
67+
func (cc *ContentfulClient) GetCategoryByID(id string, forceNoCache ...bool) (vo *CfCategory, err error) {
6868
if cc == nil || cc.Client == nil {
6969
return nil, errors.New("GetCategoryByID: No client available")
7070
}
71-
if cc.Cache != nil {
71+
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
7272
cc.Cache.entryMaps.categoryGcLock.RLock()
7373
vo, ok := cc.Cache.entryMaps.category[id]
7474
cc.Cache.entryMaps.categoryGcLock.RUnlock()
@@ -489,10 +489,7 @@ func (cc *ContentfulClient) cacheAllCategory(ctx context.Context, resultChan cha
489489
for _, category := range allCategory {
490490
if cc.Cache != nil {
491491
existingCategory, err := cc.GetCategoryByID(category.Sys.ID)
492-
if err != nil {
493-
return nil, err
494-
}
495-
if existingCategory.Sys.Version > category.Sys.Version {
492+
if err == nil && existingCategory != nil && existingCategory.Sys.Version > category.Sys.Version {
496493
return nil, fmt.Errorf("cache update canceled because Category entry %s is newer in cache", category.Sys.ID)
497494
}
498495
}

test/testapi/gocontentfulvolibproduct.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (cc *ContentfulClient) GetFilteredProduct(query *contentful.Query) (voMap m
6464
return productMap, nil
6565
}
6666

67-
func (cc *ContentfulClient) GetProductByID(id string) (vo *CfProduct, err error) {
67+
func (cc *ContentfulClient) GetProductByID(id string, forceNoCache ...bool) (vo *CfProduct, err error) {
6868
if cc == nil || cc.Client == nil {
6969
return nil, errors.New("GetProductByID: No client available")
7070
}
71-
if cc.Cache != nil {
71+
if cc.Cache != nil && (len(forceNoCache) == 0 || !forceNoCache[0]) {
7272
cc.Cache.entryMaps.productGcLock.RLock()
7373
vo, ok := cc.Cache.entryMaps.product[id]
7474
cc.Cache.entryMaps.productGcLock.RUnlock()
@@ -1107,10 +1107,7 @@ func (cc *ContentfulClient) cacheAllProduct(ctx context.Context, resultChan chan
11071107
for _, product := range allProduct {
11081108
if cc.Cache != nil {
11091109
existingProduct, err := cc.GetProductByID(product.Sys.ID)
1110-
if err != nil {
1111-
return nil, err
1112-
}
1113-
if existingProduct.Sys.Version > product.Sys.Version {
1110+
if err == nil && existingProduct != nil && existingProduct.Sys.Version > product.Sys.Version {
11141111
return nil, fmt.Errorf("cache update canceled because Product entry %s is newer in cache", product.Sys.ID)
11151112
}
11161113
}

0 commit comments

Comments
 (0)