mirror of
https://github.com/foomo/gocontentful.git
synced 2025-10-16 12:25:39 +00:00
feat: bump foomo/contentful-v0.5.0
This commit is contained in:
parent
8f8d9202c5
commit
81670afe02
13
Makefile
13
Makefile
@ -15,29 +15,34 @@ doc:
|
||||
@open "http://localhost:6060/pkg/github.com/foomo/contentful/"
|
||||
@godoc -http=localhost:6060 -play
|
||||
|
||||
.PHONY: install
|
||||
## Install binary
|
||||
install:
|
||||
@go build -o ${GOPATH}/bin/gocontenful main.go
|
||||
|
||||
.PHONY: build
|
||||
## Build binary
|
||||
build:
|
||||
@mkdir -p bin
|
||||
@go build -o bin/gocontenful main.go
|
||||
|
||||
.PHONY: test
|
||||
|
||||
## Run tests
|
||||
test:
|
||||
@go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi
|
||||
test: testapi
|
||||
@go test -p 1 -coverprofile=coverage.out -race -json ./... | gotestfmt
|
||||
|
||||
.PHONY: test
|
||||
## Run tests
|
||||
testapi:
|
||||
@go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi
|
||||
|
||||
## Test & view coverage
|
||||
cover: test
|
||||
@go tool cover -html=coverage.out -o coverage.html; open coverage.html
|
||||
|
||||
.PHONY: lint
|
||||
## Run linter
|
||||
lint:
|
||||
lint: testapi
|
||||
@golangci-lint run
|
||||
|
||||
.PHONY: lint.fix
|
||||
|
||||
@ -12,5 +12,5 @@ func TestConfigFromYAML(t *testing.T) {
|
||||
require.Equal(t, "abc123", config.SpaceID)
|
||||
require.Equal(t, "dev", config.Environment)
|
||||
require.Equal(t, "v1.0.19", config.RequireVersion)
|
||||
require.Equal(t, 2, len(config.ContentTypes))
|
||||
require.Len(t, config.ContentTypes, 2)
|
||||
}
|
||||
|
||||
33
erm/space.go
33
erm/space.go
@ -2,9 +2,9 @@ package erm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -26,15 +26,17 @@ type spaceConf struct {
|
||||
}
|
||||
|
||||
// GetLocales retrieves locale definition from Contentful
|
||||
func getLocales(CMA *contentful.Contentful, spaceID string) (locales []Locale, err error) {
|
||||
|
||||
col, err := CMA.Locales.List(spaceID).GetAll()
|
||||
func getLocales(ctx context.Context, CMA *contentful.Contentful, spaceID string) (locales []Locale, err error) {
|
||||
col, err := CMA.Locales.List(ctx, spaceID).GetAll()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't get locales")
|
||||
}
|
||||
for _, item := range col.Items {
|
||||
var locale Locale
|
||||
byteArray, _ := json.Marshal(item)
|
||||
byteArray, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&locale)
|
||||
if err != nil {
|
||||
break
|
||||
@ -45,16 +47,19 @@ func getLocales(CMA *contentful.Contentful, spaceID string) (locales []Locale, e
|
||||
}
|
||||
|
||||
// GetContentTypes retrieves content type definition from Contentful
|
||||
func getContentTypes(CMA *contentful.Contentful, spaceID string) (contentTypes []ContentType, err error) {
|
||||
func getContentTypes(ctx context.Context, CMA *contentful.Contentful, spaceID string) (contentTypes []ContentType, err error) {
|
||||
|
||||
col := CMA.ContentTypes.List(spaceID)
|
||||
col := CMA.ContentTypes.List(ctx, spaceID)
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't get content types")
|
||||
}
|
||||
for _, item := range col.Items {
|
||||
var contentType ContentType
|
||||
byteArray, _ := json.Marshal(item)
|
||||
byteArray, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = json.NewDecoder(bytes.NewReader(byteArray)).Decode(&contentType)
|
||||
if err != nil {
|
||||
break
|
||||
@ -76,12 +81,12 @@ func getContentTypes(CMA *contentful.Contentful, spaceID string) (contentTypes [
|
||||
return
|
||||
}
|
||||
|
||||
func getData(spaceID, cmaKey, environment, exportFile string, flagContentTypes []string) (
|
||||
func getData(ctx context.Context, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string) (
|
||||
finalContentTypes []ContentType, locales []Locale, err error,
|
||||
) {
|
||||
var contentTypes []ContentType
|
||||
if exportFile != "" {
|
||||
fileBytes, err := ioutil.ReadFile(exportFile)
|
||||
fileBytes, err := os.ReadFile(exportFile)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error reading export file: %v", err)
|
||||
}
|
||||
@ -101,14 +106,14 @@ func getData(spaceID, cmaKey, environment, exportFile string, flagContentTypes [
|
||||
}
|
||||
|
||||
// Get space locales
|
||||
locales, err = getLocales(CMA, spaceID)
|
||||
locales, err = getLocales(ctx, CMA, spaceID)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not get locales: %v", err)
|
||||
}
|
||||
fmt.Println("Locales found:", locales)
|
||||
|
||||
// Get content types
|
||||
contentTypes, err = getContentTypes(CMA, spaceID)
|
||||
contentTypes, err = getContentTypes(ctx, CMA, spaceID)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not get content types: %v", err)
|
||||
}
|
||||
@ -135,8 +140,8 @@ func getData(spaceID, cmaKey, environment, exportFile string, flagContentTypes [
|
||||
}
|
||||
|
||||
// GenerateAPI calls the generators
|
||||
func GenerateAPI(dir, packageName, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string, version string) (err error) {
|
||||
contentTypes, locales, errGetData := getData(spaceID, cmaKey, environment, exportFile, flagContentTypes)
|
||||
func GenerateAPI(ctx context.Context, dir, packageName, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string, version string) (err error) {
|
||||
contentTypes, locales, errGetData := getData(ctx, spaceID, cmaKey, environment, exportFile, flagContentTypes)
|
||||
if errGetData != nil {
|
||||
return errGetData
|
||||
}
|
||||
|
||||
@ -1,13 +1,15 @@
|
||||
package erm
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func getFuncMap() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"firstCap": strings.Title,
|
||||
"firstCap": cases.Title(language.Und, cases.NoLower).String,
|
||||
"fieldIsBasic": fieldIsBasic,
|
||||
"fieldIsComplex": fieldIsComplex,
|
||||
"fieldIsAsset": fieldIsAsset,
|
||||
|
||||
@ -246,7 +246,7 @@ func (cc *ContentfulClient) ClientStats() {
|
||||
for _, parents := range cc.Cache.parentMap {
|
||||
referenceCount += len(parents)
|
||||
}
|
||||
fieldsMap["cache parentMap parents"] = referenceCount
|
||||
fieldsMap["cache parentMap parents"] = referenceCount
|
||||
}
|
||||
cc.logFn(fieldsMap, LogInfo, "Contentful ClientStats")
|
||||
}
|
||||
@ -256,18 +256,18 @@ func (ref ContentfulReferencedEntry) ContentType() (contentType string) {
|
||||
return ref.Entry.Sys.ContentType.Sys.ID
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) DeleteAsset(asset *contentful.Asset) error {
|
||||
func (cc *ContentfulClient) DeleteAsset(ctx context.Context, asset *contentful.Asset) error {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return errors.New("DeleteAsset: No client available")
|
||||
}
|
||||
if cc.clientMode != ClientModeCMA {
|
||||
return errors.New("DeleteAsset: Only available in ClientModeCMA")
|
||||
}
|
||||
errUnpublish := cc.Client.Assets.Unpublish(cc.SpaceID, asset)
|
||||
errUnpublish := cc.Client.Assets.Unpublish(ctx, cc.SpaceID, asset)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return errUnpublish
|
||||
}
|
||||
errDelete := cc.Client.Assets.Delete(cc.SpaceID, asset)
|
||||
errDelete := cc.Client.Assets.Delete(ctx, cc.SpaceID, asset)
|
||||
if errDelete != nil {
|
||||
return errDelete
|
||||
}
|
||||
@ -286,11 +286,11 @@ func (cc *ContentfulClient) EnableTextJanitor() {
|
||||
cc.textJanitor = true
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetAllAssets() (map[string]*contentful.Asset, error) {
|
||||
return cc.getAllAssets(true)
|
||||
func (cc *ContentfulClient) GetAllAssets(ctx context.Context) (map[string]*contentful.Asset, error) {
|
||||
return cc.getAllAssets(ctx, true)
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*contentful.Asset, error) {
|
||||
func (cc *ContentfulClient) GetAssetByID(ctx context.Context, id string, forceNoCache ...bool) (*contentful.Asset, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("GetAssetByID: No client available")
|
||||
}
|
||||
@ -307,7 +307,7 @@ func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*cont
|
||||
return nil, errors.New("GetAssetByID: not found")
|
||||
}
|
||||
}
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Equal("sys.id",id)
|
||||
_, err := col.Next()
|
||||
if err != nil {
|
||||
@ -334,7 +334,7 @@ func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*cont
|
||||
return &asset, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
|
||||
func (cc *ContentfulClient) GetContentTypeOfID(ctx context.Context, id string) (string, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return "", errors.New("GetContentTypeOfID: No client available")
|
||||
}
|
||||
@ -353,7 +353,7 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
|
||||
{{ end }}
|
||||
return "", fmt.Errorf("GetContentTypeOfID: %s Not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.Include(0).Equal("sys.id",id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
@ -447,7 +447,7 @@ func NewAssetFromURL(id string, uploadUrl string, imageFileType string, title st
|
||||
return asset
|
||||
}
|
||||
|
||||
func NewContentfulClient(spaceID string, clientMode ClientMode, clientKey string, optimisticPageSize uint16, logFn func(fields map[string]interface{}, level int, args ...interface{}), logLevel int, debug bool) (*ContentfulClient, error) {
|
||||
func NewContentfulClient(ctx context.Context, spaceID string, clientMode ClientMode, clientKey string, optimisticPageSize uint16, logFn func(fields map[string]interface{}, level int, args ...interface{}), logLevel int, debug bool) (*ContentfulClient, error) {
|
||||
if spaceID == "" {
|
||||
return nil, errors.New("NewContentfulClient: SpaceID cannot be empty")
|
||||
}
|
||||
@ -497,7 +497,7 @@ func NewContentfulClient(spaceID string, clientMode ClientMode, clientKey string
|
||||
SpaceID: spaceID,
|
||||
sync: clientMode == ClientModeCDA,
|
||||
}
|
||||
_, err = cc.Client.Spaces.Get(spaceID)
|
||||
_, err = cc.Client.Spaces.Get(ctx, spaceID)
|
||||
if err != nil {
|
||||
_, ok := err.(contentful.NotFoundError)
|
||||
if ok {
|
||||
@ -684,6 +684,7 @@ func (cc *ContentfulClient) syncCache(ctx context.Context, contentTypes []string
|
||||
for {
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
col := cc.Client.Entries.Sync(
|
||||
ctx,
|
||||
cc.SpaceID,
|
||||
cc.syncToken == "",
|
||||
cc.syncToken,
|
||||
@ -767,7 +768,7 @@ func (cc *ContentfulClient) cacheSpace(ctx context.Context, contentTypes []strin
|
||||
if cacheAssets {
|
||||
contentTypes = append([]string{assetWorkerType}, contentTypes...)
|
||||
}
|
||||
_, errCanWeEvenConnect := cc.Client.Spaces.Get(cc.SpaceID)
|
||||
_, errCanWeEvenConnect := cc.Client.Spaces.Get(ctx, cc.SpaceID)
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
offlinePreviousState := cc.offline
|
||||
cc.cacheMutex.sharedDataGcLock.RUnlock()
|
||||
@ -893,7 +894,7 @@ func (cc *ContentfulClient) cacheGcAssetByID(ctx context.Context, id string, ass
|
||||
if cc.Client == nil {
|
||||
return errors.New("cacheGcAssetByID: No client available")
|
||||
}
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Equal("sys.id", id)
|
||||
_, err := col.Next()
|
||||
if err != nil {
|
||||
@ -910,7 +911,7 @@ func (cc *ContentfulClient) cacheGcAssetByID(ctx context.Context, id string, ass
|
||||
err = json.Unmarshal(byt, &asset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, loc := range []Locale{ {{ range $index , $locale := $locales }}SpaceLocale{{ onlyLetters $locale.Name }},{{end}} } {
|
||||
if _, ok := asset.Fields.File[string(loc)]; ok {
|
||||
@ -962,7 +963,7 @@ func getContentfulAPIClient(clientMode ClientMode, clientKey string) (*contentfu
|
||||
}
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) getAllAssets(tryCacheFirst bool) (map[string]*contentful.Asset, error) {
|
||||
func (cc *ContentfulClient) getAllAssets(ctx context.Context, tryCacheFirst bool) (map[string]*contentful.Asset, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getAllAssets: No client available")
|
||||
}
|
||||
@ -981,7 +982,7 @@ func (cc *ContentfulClient) getAllAssets(tryCacheFirst bool) (map[string]*conten
|
||||
allItems = append(allItems,asset)
|
||||
}
|
||||
} else {
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Limit(assetPageSize)
|
||||
for {
|
||||
_, err := col.Next()
|
||||
@ -1027,8 +1028,8 @@ func getOfflineSpaceFromFile(filename string) (*offlineTemp, error) {
|
||||
return offlineTemp, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) optimisticPageSizeGetAll(contentType string, limit uint16) (*contentful.Collection, error) {
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
func (cc *ContentfulClient) optimisticPageSizeGetAll(ctx context.Context, contentType string, limit uint16) (*contentful.Collection, error) {
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType(contentType).Locale("*").Include(0).Limit(limit)
|
||||
allItems := []interface{}{}
|
||||
var err error
|
||||
@ -1047,7 +1048,7 @@ func (cc *ContentfulClient) optimisticPageSizeGetAll(contentType string, limit u
|
||||
case contentful.ErrorResponse:
|
||||
msg := errTyped.Message
|
||||
if strings.Contains(msg, "Response size too big") && limit >= 20 {
|
||||
smallerPageCol, err := cc.optimisticPageSizeGetAll(contentType, limit/2)
|
||||
smallerPageCol, err := cc.optimisticPageSizeGetAll(ctx, contentType, limit/2)
|
||||
return smallerPageCol, err
|
||||
}
|
||||
return nil, err
|
||||
@ -1476,7 +1477,7 @@ func updateCacheForContentType(ctx context.Context, results chan ContentTypeResu
|
||||
}
|
||||
{{ end }}
|
||||
case assetWorkerType:
|
||||
allAssets, err := cc.getAllAssets(false)
|
||||
allAssets, err := cc.getAllAssets(ctx, false)
|
||||
if err != nil {
|
||||
return errors.New("updateCacheForContentType failed for assets")
|
||||
}
|
||||
@ -1530,7 +1531,7 @@ func updateCacheForContentTypeAndEntity(ctx context.Context, cc *ContentfulClien
|
||||
return nil
|
||||
}
|
||||
|
||||
func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
|
||||
func commonGetParents(ctx context.Context, cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
|
||||
parents = []EntryReference{}
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
cacheInit := cc.cacheInit
|
||||
@ -1548,7 +1549,7 @@ func commonGetParents(cc *ContentfulClient, id string, contentType []string) (pa
|
||||
}
|
||||
return cc.Cache.parentMap[id], nil
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.Equal("links_to_entry", id).Locale("*")
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
@ -1672,4 +1673,4 @@ func stripInvisibleUnicodeChars(dirty string) string {
|
||||
return -1
|
||||
}, dirty)
|
||||
return clean
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ const ContentType{{ firstCap $contentType.Sys.ID }} = "{{ $contentType.Sys.ID }}
|
||||
|
||||
// ---{{ firstCap $contentType.Sys.ID }} public methods---
|
||||
|
||||
func (cc *ContentfulClient) GetAll{{ firstCap $contentType.Sys.ID }}() (voMap map[string]*Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
func (cc *ContentfulClient) GetAll{{ firstCap $contentType.Sys.ID }}(ctx context.Context) (voMap map[string]*Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
if cc == nil {
|
||||
return nil, errors.New("GetAll{{ firstCap $contentType.Sys.ID }}: No client available")
|
||||
}
|
||||
@ -27,7 +27,7 @@ func (cc *ContentfulClient) GetAll{{ firstCap $contentType.Sys.ID }}() (voMap ma
|
||||
if cacheInit {
|
||||
return cc.Cache.entryMaps.{{ $contentType.Sys.ID }}, nil
|
||||
}
|
||||
col, err := cc.optimisticPageSizeGetAll("{{ $contentType.Sys.ID }}", optimisticPageSize)
|
||||
col, err := cc.optimisticPageSizeGetAll(ctx, "{{ $contentType.Sys.ID }}", optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -42,11 +42,11 @@ func (cc *ContentfulClient) GetAll{{ firstCap $contentType.Sys.ID }}() (voMap ma
|
||||
return {{ $contentType.Sys.ID }}Map, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetFiltered{{ firstCap $contentType.Sys.ID }}(query *contentful.Query) (voMap map[string]*Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
func (cc *ContentfulClient) GetFiltered{{ firstCap $contentType.Sys.ID }}(ctx context.Context, query *contentful.Query) (voMap map[string]*Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getFiltered{{ firstCap $contentType.Sys.ID }}: No client available")
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
if query != nil {
|
||||
col.Query = *query
|
||||
}
|
||||
@ -66,7 +66,7 @@ func (cc *ContentfulClient) GetFiltered{{ firstCap $contentType.Sys.ID }}(query
|
||||
return {{ $contentType.Sys.ID }}Map, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) Get{{ firstCap $contentType.Sys.ID }}ByID(id string, forceNoCache ...bool) (vo *Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
func (cc *ContentfulClient) Get{{ firstCap $contentType.Sys.ID }}ByID(ctx context.Context, id string, forceNoCache ...bool) (vo *Cf{{ firstCap $contentType.Sys.ID }}, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("Get{{ firstCap $contentType.Sys.ID }}ByID: No client available")
|
||||
}
|
||||
@ -79,7 +79,7 @@ func (cc *ContentfulClient) Get{{ firstCap $contentType.Sys.ID }}ByID(id string,
|
||||
}
|
||||
return nil, fmt.Errorf("Get{{ firstCap $contentType.Sys.ID }}ByID: entry '%s' not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("{{ $contentType.Sys.ID }}").Locale("*").Include(0).Equal("sys.id",id)
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
@ -109,14 +109,14 @@ func NewCf{{ firstCap $contentType.Sys.ID }}(contentfulClient ...*ContentfulClie
|
||||
cf{{ firstCap $contentType.Sys.ID }}.Sys.ContentType.Sys.LinkType = "ContentType"
|
||||
return
|
||||
}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) GetParents(contentType ...string) (parents []EntryReference, err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) GetParents(ctx context.Context, contentType ...string) (parents []EntryReference, err error) {
|
||||
if vo == nil {
|
||||
return nil, errors.New("GetParents: Value Object is nil")
|
||||
}
|
||||
if vo.CC == nil {
|
||||
return nil, errors.New("GetParents: Value Object has no Contentful Client set")
|
||||
}
|
||||
return commonGetParents(vo.CC, vo.Sys.ID, contentType)
|
||||
return commonGetParents(ctx, vo.CC, vo.Sys.ID, contentType)
|
||||
}
|
||||
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) GetPublishingStatus() string {
|
||||
@ -212,7 +212,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
{{ end }}
|
||||
{{ if fieldIsMultipleReference $field }}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale ...Locale) []*EntryReference {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(ctx context.Context, locale ...Locale) []*EntryReference {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -248,7 +248,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
}
|
||||
for _, eachLocalized{{ firstCap $field.ID }} := range vo.Fields.{{ firstCap $field.ID }}[string(loc)] {
|
||||
contentType, err := vo.CC.GetContentTypeOfID(eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
contentType, err := vo.CC.GetContentTypeOfID(ctx, eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, ErrNoTypeOfRefEntry)
|
||||
@ -258,7 +258,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
switch contentType {
|
||||
{{ range $index , $contentType := $contentTypes }}
|
||||
case ContentType{{ firstCap $contentType.Sys.ID }}:
|
||||
referencedVO, err := vo.CC.Get{{ firstCap $contentType.Sys.ID }}ByID(eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
referencedVO, err := vo.CC.Get{{ firstCap $contentType.Sys.ID }}ByID(ctx, eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, err)
|
||||
@ -273,7 +273,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
{{ end }}
|
||||
{{ if fieldIsReference $field }}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale ...Locale) *EntryReference {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(ctx context.Context, locale ...Locale) *EntryReference {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -308,7 +308,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
}
|
||||
localized{{ firstCap $field.ID }} := vo.Fields.{{ firstCap $field.ID }}[string(loc)]
|
||||
contentType, err := vo.CC.GetContentTypeOfID(localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
contentType, err := vo.CC.GetContentTypeOfID(ctx, localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, ErrNoTypeOfRefEntry)
|
||||
@ -318,7 +318,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
switch contentType {
|
||||
{{ range $index , $contentType := $contentTypes }}
|
||||
case ContentType{{ firstCap $contentType.Sys.ID }}:
|
||||
referencedVO, err := vo.CC.Get{{ firstCap $contentType.Sys.ID }}ByID(localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
referencedVO, err := vo.CC.Get{{ firstCap $contentType.Sys.ID }}ByID(ctx, localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, err)
|
||||
@ -332,7 +332,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
{{ end }}
|
||||
{{ if fieldIsMultipleAsset $field }}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale ...Locale) []*contentful.AssetNoLocale {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(ctx context.Context, locale ...Locale) []*contentful.AssetNoLocale {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -370,7 +370,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
}
|
||||
for _, eachLocalized{{ firstCap $field.ID }} := range vo.Fields.{{ firstCap $field.ID }}[string(loc)] {
|
||||
asset, err := vo.CC.GetAssetByID(eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
asset, err := vo.CC.GetAssetByID(ctx, eachLocalized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel == LogDebug {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, ErrNoTypeOfRefAsset)
|
||||
@ -401,7 +401,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
{{ end }}
|
||||
{{ if fieldIsAsset $field }}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale ...Locale) *contentful.AssetNoLocale {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(ctx context.Context, locale ...Locale) *contentful.AssetNoLocale {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -438,7 +438,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) {{ firstCap $field.ID }}(locale
|
||||
}
|
||||
}
|
||||
localized{{ firstCap $field.ID }} := vo.Fields.{{ firstCap $field.ID }}[string(loc)]
|
||||
asset, err := vo.CC.GetAssetByID(localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
asset, err := vo.CC.GetAssetByID(ctx, localized{{ firstCap $field.ID }}.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel == LogDebug {
|
||||
vo.CC.logFn(map[string]interface{}{"content type":vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method":"{{ firstCap $field.ID }}()"}, LogError, ErrNoTypeOfRefAsset)
|
||||
@ -490,7 +490,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) Set{{ firstCap $field.ID }}({{ $
|
||||
return
|
||||
}
|
||||
{{ end }}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpsertEntry() (err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpsertEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpsertEntry: Value Object is nil")
|
||||
}
|
||||
@ -510,13 +510,13 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpsertEntry() (err error) {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} UpsertEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} UpsertEntry: Operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) PublishEntry() (err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) PublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("PublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -535,13 +535,13 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) PublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} PublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} PublishEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UnpublishEntry() (err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UnpublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UnpublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -560,13 +560,13 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UnpublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} UnpublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} UnpublishEntry: unpublish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpdateEntry() (err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpdateEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpdateEntry: Value Object is nil")
|
||||
}
|
||||
@ -586,7 +586,7 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpdateEntry() (err error) {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} UpdateEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} UpdateEntry: upsert operation failed: %w", err)
|
||||
}
|
||||
@ -598,13 +598,13 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) UpdateEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} UpdateEntry: Can't unmarshal JSON back into VO")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} UpdateEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) DeleteEntry() (err error) {
|
||||
func (vo *Cf{{ firstCap $contentType.Sys.ID }}) DeleteEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("DeleteEntry: Value Object is nil")
|
||||
}
|
||||
@ -624,12 +624,12 @@ func (vo *Cf{{ firstCap $contentType.Sys.ID }}) DeleteEntry() (err error) {
|
||||
return errors.New("Cf{{ firstCap $contentType.Sys.ID }} DeleteEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
if cfEntry.Sys.PublishedCounter > 0 {
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} DeleteEntry: Unpublish entry failed: %w", errUnpublish)
|
||||
}
|
||||
}
|
||||
errDelete := vo.CC.Client.Entries.Delete(vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
errDelete := vo.CC.Client.Entries.Delete(ctx, vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
if errDelete != nil {
|
||||
return fmt.Errorf("Cf{{ firstCap $contentType.Sys.ID }} DeleteEntry: Delete entry failed: %w", errDelete)
|
||||
}
|
||||
@ -662,7 +662,7 @@ func (cc *ContentfulClient) cacheAll{{ firstCap $contentType.Sys.ID }}(ctx conte
|
||||
}
|
||||
}
|
||||
} else {
|
||||
col, err = cc.optimisticPageSizeGetAll("{{ $contentType.Sys.ID }}", cc.optimisticPageSize)
|
||||
col, err = cc.optimisticPageSizeGetAll(ctx, "{{ $contentType.Sys.ID }}", cc.optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, errors.New("optimisticPageSizeGetAll for {{ firstCap $contentType.Sys.ID }} failed: "+err.Error())
|
||||
}
|
||||
@ -674,7 +674,7 @@ func (cc *ContentfulClient) cacheAll{{ firstCap $contentType.Sys.ID }}(ctx conte
|
||||
{{ $contentType.Sys.ID }}Map := map[string]*Cf{{ firstCap $contentType.Sys.ID }}{}
|
||||
for _, {{ $contentType.Sys.ID }} := range all{{ firstCap $contentType.Sys.ID }} {
|
||||
if cc.cacheInit {
|
||||
existing{{ firstCap $contentType.Sys.ID }}, err := cc.Get{{ firstCap $contentType.Sys.ID }}ByID({{ $contentType.Sys.ID }}.Sys.ID)
|
||||
existing{{ firstCap $contentType.Sys.ID }}, err := cc.Get{{ firstCap $contentType.Sys.ID }}ByID(ctx, {{ $contentType.Sys.ID }}.Sys.ID)
|
||||
if err == nil && existing{{ firstCap $contentType.Sys.ID }} != nil && existing{{ firstCap $contentType.Sys.ID }}.Sys.Version > {{ $contentType.Sys.ID }}.Sys.Version {
|
||||
return nil, fmt.Errorf("cache update canceled because {{ firstCap $contentType.Sys.ID }} entry %s is newer in cache", {{ $contentType.Sys.ID }}.Sys.ID)
|
||||
}
|
||||
@ -735,7 +735,7 @@ func (cc *ContentfulClient) cache{{ firstCap $contentType.Sys.ID }}ByID(ctx cont
|
||||
defer cc.cacheMutex.idContentTypeMapGcLock.Unlock()
|
||||
cc.cacheMutex.parentMapGcLock.Lock()
|
||||
defer cc.cacheMutex.parentMapGcLock.Unlock()
|
||||
|
||||
|
||||
var col *contentful.Collection
|
||||
if entryPayload != nil {
|
||||
col = &contentful.Collection{
|
||||
@ -747,13 +747,13 @@ func (cc *ContentfulClient) cache{{ firstCap $contentType.Sys.ID }}ByID(ctx cont
|
||||
return errors.New("cache{{ firstCap $contentType.Sys.ID }}ByID: No client available")
|
||||
}
|
||||
if !entryDelete {
|
||||
col = cc.Client.Entries.List(cc.SpaceID)
|
||||
col = cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("{{ $contentType.Sys.ID }}").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// It was deleted
|
||||
if col != nil && len(col.Items) == 0 || entryDelete {
|
||||
@ -833,7 +833,7 @@ func (cc *ContentfulClient) cache{{ firstCap $contentType.Sys.ID }}ByID(ctx cont
|
||||
for childID, parents := range cc.Cache.parentMap {
|
||||
if _, isCollectedChildID := allChildrensIds[childID]; isCollectedChildID {
|
||||
continue
|
||||
}
|
||||
}
|
||||
newParents := []EntryReference{}
|
||||
for _, parent := range parents {
|
||||
if parent.ID != id {
|
||||
@ -855,11 +855,11 @@ func colToCf{{ firstCap $contentType.Sys.ID }}(col *contentful.Collection, cc *C
|
||||
}
|
||||
if cc.textJanitor {
|
||||
{{ range $fieldIndex, $field := $contentType.Fields }}
|
||||
{{ if or (fieldIsSymbol $field) (fieldIsText $field) }}
|
||||
{{ if or (fieldIsSymbol $field) (fieldIsText $field) }}
|
||||
vo.Fields.{{ firstCap $field.ID }} = cleanUpStringField(vo.Fields.{{ firstCap $field.ID }}){{ end }}
|
||||
{{ if fieldIsSymbolList $field }}
|
||||
{{ if fieldIsSymbolList $field }}
|
||||
vo.Fields.{{ firstCap $field.ID }} = cleanUpStringSliceField(vo.Fields.{{ firstCap $field.ID }}){{ end }}
|
||||
{{ if fieldIsRichText $field }}
|
||||
{{ if fieldIsRichText $field }}
|
||||
vo.Fields.{{ firstCap $field.ID }} = cleanUpRichTextField(vo.Fields.{{ firstCap $field.ID }}){{ end }}
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
11
erm/util.go
11
erm/util.go
@ -2,9 +2,10 @@ package erm
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var onlyLettersRegex = regexp.MustCompile("[^A-Za-z]")
|
||||
|
||||
func sliceIncludes(slice []string, key string) bool {
|
||||
for _, val := range slice {
|
||||
if val == key {
|
||||
@ -14,12 +15,6 @@ func sliceIncludes(slice []string, key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func firstCap(inputString string) (outputString string) {
|
||||
outputString = strings.Title(inputString)
|
||||
return
|
||||
}
|
||||
|
||||
func onlyLetters(inputString string) (outputString string) {
|
||||
re := regexp.MustCompile("[^A-Za-z]")
|
||||
return re.ReplaceAllString(inputString, "")
|
||||
return onlyLettersRegex.ReplaceAllString(inputString, "")
|
||||
}
|
||||
|
||||
@ -51,8 +51,8 @@ type ContentTypeField struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Items *ContentTypeFieldItems `json:"items,omitempty"`
|
||||
LinkType string `json:"linkType,omitempty"`
|
||||
Omitted bool `json:"omitted,omitempty"`
|
||||
ReferencedTypes []string
|
||||
Omitted bool `json:"omitted,omitempty"`
|
||||
ReferencedTypes []string `json:"referencedTypes,omitempty"`
|
||||
}
|
||||
|
||||
// ContentType VO
|
||||
|
||||
28
go.mod
28
go.mod
@ -1,16 +1,24 @@
|
||||
module github.com/foomo/gocontentful
|
||||
|
||||
go 1.16
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/foomo/contentful v0.3.6
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||
github.com/stretchr/testify v1.7.0
|
||||
golang.org/x/mod v0.4.2 // indirect
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
|
||||
golang.org/x/tools v0.1.0
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
||||
github.com/foomo/contentful v0.4.6-0.20240426090016-36498310ced0
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/sync v0.7.0
|
||||
golang.org/x/text v0.14.0
|
||||
golang.org/x/tools v0.20.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
|
||||
github.com/jtolds/gls v4.20.0+incompatible // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/smartystreets/assertions v1.2.0 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
moul.io/http2curl v1.0.0 // indirect
|
||||
)
|
||||
|
||||
66
go.sum
66
go.sum
@ -1,59 +1,39 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/foomo/contentful v0.3.6 h1:yiwhWayrXCe0wpQGzhO32bl8AE/46T261bBqNdPODWk=
|
||||
github.com/foomo/contentful v0.3.6/go.mod h1:6Pf8efSKeMbwKgVkjSFWeBsLYdUA0NTW7OUsMWzHXvY=
|
||||
github.com/foomo/contentful v0.4.6-0.20240426090016-36498310ced0 h1:RrL0mO0RhA7e01Uogjz2AWEletuNUFQymyn8PJZEnoQ=
|
||||
github.com/foomo/contentful v0.4.6-0.20240426090016-36498310ced0/go.mod h1:x9QtObXj7qKw4Rpxg6kI6bi6RGXzD7o/l+XPj1cr2Og=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
|
||||
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
|
||||
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
|
||||
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A=
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
|
||||
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
|
||||
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
|
||||
|
||||
3
main.go
3
main.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
@ -134,7 +135,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
err = erm.GenerateAPI(filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION)
|
||||
err = erm.GenerateAPI(context.Background(), filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION)
|
||||
if err != nil {
|
||||
fatal("Something went horribly wrong...", err)
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func TestCacheHasContentType(t *testing.T) {
|
||||
func TestGetAsset(t *testing.T) {
|
||||
contentfulClient, err := getTestClient()
|
||||
require.NoError(t, err)
|
||||
_, err = contentfulClient.GetAssetByID("Xc0ny7GWsMEMCeASWO2um")
|
||||
_, err = contentfulClient.GetAssetByID(context.TODO(), "Xc0ny7GWsMEMCeASWO2um")
|
||||
require.NoError(t, err)
|
||||
newAsset := testapi.NewAssetFromURL("12345", "https://example.com", "PNG", "New Asset")
|
||||
require.NotNil(t, newAsset)
|
||||
@ -60,7 +60,7 @@ func TestDeleteAssetFromCache(t *testing.T) {
|
||||
func TestGetContentTypeOfID(t *testing.T) {
|
||||
contentfulClient, err := getTestClient()
|
||||
require.NoError(t, err)
|
||||
contentType, err := contentfulClient.GetContentTypeOfID("651CQ8rLoIYCeY6G0QG22q")
|
||||
contentType, err := contentfulClient.GetContentTypeOfID(context.TODO(), "651CQ8rLoIYCeY6G0QG22q")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "brand", contentType)
|
||||
}
|
||||
@ -68,9 +68,9 @@ func TestGetContentTypeOfID(t *testing.T) {
|
||||
func TestGetParents(t *testing.T) {
|
||||
contentfulClient, err := getTestClient()
|
||||
require.NoError(t, err)
|
||||
product, err := contentfulClient.GetProductByID("6dbjWqNd9SqccegcqYq224")
|
||||
product, err := contentfulClient.GetProductByID(context.TODO(), "6dbjWqNd9SqccegcqYq224")
|
||||
require.NoError(t, err)
|
||||
brandRef := product.Brand()
|
||||
brandRef := product.Brand(context.TODO())
|
||||
brandParents, err := brandRef.GetParents(contentfulClient)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(brandParents))
|
||||
@ -98,7 +98,7 @@ func TestPreserveCacheIfNewer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
err = contentfulClient.UpdateCache(context.TODO(), nil, false)
|
||||
require.NoError(t, err)
|
||||
brand, err := contentfulClient.GetBrandByID("JrePkDVYomE8AwcuCUyMi")
|
||||
brand, err := contentfulClient.GetBrandByID(context.TODO(), "JrePkDVYomE8AwcuCUyMi")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2.0, brand.Sys.Version)
|
||||
}
|
||||
|
||||
@ -14,20 +14,20 @@ var (
|
||||
concurrency = 10000
|
||||
)
|
||||
|
||||
func readWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
product, err := contentfulClient.GetProductByID(testProductID)
|
||||
func readWorker(ctx context.Context, contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
product, err := contentfulClient.GetProductByID(ctx, testProductID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = contentfulClient.GetAllProduct()
|
||||
_, err = contentfulClient.GetAllProduct(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
price := product.Price()
|
||||
testLogger.Infof("Read worker %d read price: %f", i, price)
|
||||
_ = product.Brand()
|
||||
_ = product.Categories()
|
||||
_ = product.Image()
|
||||
_ = product.Brand(ctx)
|
||||
_ = product.Categories(ctx)
|
||||
_ = product.Image(ctx)
|
||||
_ = product.Nodes()
|
||||
_ = product.ProductDescription()
|
||||
_ = product.ProductName()
|
||||
@ -42,12 +42,12 @@ func readWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parentWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
brand, err := contentfulClient.GetBrandByID(testBrandID)
|
||||
func parentWorker(ctx context.Context, contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
brand, err := contentfulClient.GetBrandByID(ctx, testBrandID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parents, err := brand.GetParents()
|
||||
parents, err := brand.GetParents(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -55,8 +55,8 @@ func parentWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
product, err := contentfulClient.GetProductByID(testProductID)
|
||||
func writeWorker(ctx context.Context, contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
product, err := contentfulClient.GetProductByID(ctx, testProductID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -66,19 +66,19 @@ func writeWorker(contentfulClient *testapi.ContentfulClient, i int) error {
|
||||
}
|
||||
contentfulClient.SetProductInCache(product)
|
||||
testLogger.Infof("Write worker %d set price: %d", i, i)
|
||||
product.SetBrand(testapi.ContentTypeSys{})
|
||||
product.SetCategories([]testapi.ContentTypeSys{})
|
||||
product.SetImage([]testapi.ContentTypeSys{})
|
||||
product.SetNodes(nil)
|
||||
product.SetProductDescription("")
|
||||
product.SetProductName("")
|
||||
product.SetQuantity(1)
|
||||
product.SetSeoText("")
|
||||
product.SetSizetypecolor("")
|
||||
product.SetSku("")
|
||||
product.SetSlug("")
|
||||
product.SetTags([]string{""})
|
||||
product.SetWebsite("")
|
||||
_ = product.SetBrand(testapi.ContentTypeSys{})
|
||||
_ = product.SetCategories([]testapi.ContentTypeSys{})
|
||||
_ = product.SetImage([]testapi.ContentTypeSys{})
|
||||
_ = product.SetNodes(nil)
|
||||
_ = product.SetProductDescription("")
|
||||
_ = product.SetProductName("")
|
||||
_ = product.SetQuantity(1)
|
||||
_ = product.SetSeoText("")
|
||||
_ = product.SetSizetypecolor("")
|
||||
_ = product.SetSku("")
|
||||
_ = product.SetSlug("")
|
||||
_ = product.SetTags([]string{""})
|
||||
_ = product.SetWebsite("")
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ func TestConcurrentReadWrites(t *testing.T) {
|
||||
i := i
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := writeWorker(contentfulClient, i)
|
||||
err := writeWorker(context.TODO(), contentfulClient, i)
|
||||
if err != nil {
|
||||
testLogger.Errorf("testConcurrentReadWrites: %v", err)
|
||||
}
|
||||
@ -116,7 +116,7 @@ func TestConcurrentReadWrites(t *testing.T) {
|
||||
i := i
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := readWorker(contentfulClient, i)
|
||||
err := readWorker(context.TODO(), contentfulClient, i)
|
||||
if err != nil {
|
||||
testLogger.Errorf("testConcurrentReadWrites: %v", err)
|
||||
}
|
||||
@ -127,7 +127,7 @@ func TestConcurrentReadWrites(t *testing.T) {
|
||||
i := i
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := parentWorker(contentfulClient, i)
|
||||
err := parentWorker(context.TODO(), contentfulClient, i)
|
||||
if err != nil {
|
||||
testLogger.Errorf("testConcurrentReadWrites: %v", err)
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -14,13 +15,13 @@ func TestPublishingStatus(t *testing.T) {
|
||||
contentfulClient, err := getTestClient()
|
||||
require.NoError(t, err)
|
||||
time.Sleep(time.Second)
|
||||
draft, err := contentfulClient.GetProductByID("6dbjWqNd9SqccegcqYq224")
|
||||
draft, err := contentfulClient.GetProductByID(context.TODO(), "6dbjWqNd9SqccegcqYq224")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testapi.StatusDraft, draft.GetPublishingStatus())
|
||||
published, err := contentfulClient.GetCategoryByID("7LAnCobuuWYSqks6wAwY2a")
|
||||
published, err := contentfulClient.GetCategoryByID(context.TODO(), "7LAnCobuuWYSqks6wAwY2a")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testapi.StatusPublished, published.GetPublishingStatus())
|
||||
changed, err := contentfulClient.GetProductByID("3DVqIYj4dOwwcKu6sgqOgg")
|
||||
changed, err := contentfulClient.GetProductByID(context.TODO(), "3DVqIYj4dOwwcKu6sgqOgg")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testapi.StatusChanged, changed.GetPublishingStatus())
|
||||
}
|
||||
@ -33,7 +34,7 @@ func TestCleanUpUnicode(t *testing.T) {
|
||||
true,
|
||||
true)
|
||||
require.NoError(t, errClient)
|
||||
testCleanUpUnicode, err := cc.GetProductByID("6dbjWqNd9SqccegcqYq224")
|
||||
testCleanUpUnicode, err := cc.GetProductByID(context.TODO(), "6dbjWqNd9SqccegcqYq224")
|
||||
require.NoError(t, err)
|
||||
html, err := testapi.RichTextToHtml(testCleanUpUnicode.SeoText(testapi.SpaceLocaleGerman), nil, nil, nil, nil, testapi.SpaceLocaleGerman)
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/gocontentful/test/testapi"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var targetRichText = &testapi.RichTextNode{
|
||||
|
||||
@ -273,18 +273,18 @@ func (ref ContentfulReferencedEntry) ContentType() (contentType string) {
|
||||
return ref.Entry.Sys.ContentType.Sys.ID
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) DeleteAsset(asset *contentful.Asset) error {
|
||||
func (cc *ContentfulClient) DeleteAsset(ctx context.Context, asset *contentful.Asset) error {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return errors.New("DeleteAsset: No client available")
|
||||
}
|
||||
if cc.clientMode != ClientModeCMA {
|
||||
return errors.New("DeleteAsset: Only available in ClientModeCMA")
|
||||
}
|
||||
errUnpublish := cc.Client.Assets.Unpublish(cc.SpaceID, asset)
|
||||
errUnpublish := cc.Client.Assets.Unpublish(ctx, cc.SpaceID, asset)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return errUnpublish
|
||||
}
|
||||
errDelete := cc.Client.Assets.Delete(cc.SpaceID, asset)
|
||||
errDelete := cc.Client.Assets.Delete(ctx, cc.SpaceID, asset)
|
||||
if errDelete != nil {
|
||||
return errDelete
|
||||
}
|
||||
@ -303,11 +303,11 @@ func (cc *ContentfulClient) EnableTextJanitor() {
|
||||
cc.textJanitor = true
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetAllAssets() (map[string]*contentful.Asset, error) {
|
||||
return cc.getAllAssets(true)
|
||||
func (cc *ContentfulClient) GetAllAssets(ctx context.Context) (map[string]*contentful.Asset, error) {
|
||||
return cc.getAllAssets(ctx, true)
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*contentful.Asset, error) {
|
||||
func (cc *ContentfulClient) GetAssetByID(ctx context.Context, id string, forceNoCache ...bool) (*contentful.Asset, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("GetAssetByID: No client available")
|
||||
}
|
||||
@ -324,7 +324,7 @@ func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*cont
|
||||
return nil, errors.New("GetAssetByID: not found")
|
||||
}
|
||||
}
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Equal("sys.id", id)
|
||||
_, err := col.Next()
|
||||
if err != nil {
|
||||
@ -351,7 +351,7 @@ func (cc *ContentfulClient) GetAssetByID(id string, forceNoCache ...bool) (*cont
|
||||
return &asset, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
|
||||
func (cc *ContentfulClient) GetContentTypeOfID(ctx context.Context, id string) (string, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return "", errors.New("GetContentTypeOfID: No client available")
|
||||
}
|
||||
@ -384,7 +384,7 @@ func (cc *ContentfulClient) GetContentTypeOfID(id string) (string, error) {
|
||||
|
||||
return "", fmt.Errorf("GetContentTypeOfID: %s Not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.Include(0).Equal("sys.id", id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
@ -478,7 +478,7 @@ func NewAssetFromURL(id string, uploadUrl string, imageFileType string, title st
|
||||
return asset
|
||||
}
|
||||
|
||||
func NewContentfulClient(spaceID string, clientMode ClientMode, clientKey string, optimisticPageSize uint16, logFn func(fields map[string]interface{}, level int, args ...interface{}), logLevel int, debug bool) (*ContentfulClient, error) {
|
||||
func NewContentfulClient(ctx context.Context, spaceID string, clientMode ClientMode, clientKey string, optimisticPageSize uint16, logFn func(fields map[string]interface{}, level int, args ...interface{}), logLevel int, debug bool) (*ContentfulClient, error) {
|
||||
if spaceID == "" {
|
||||
return nil, errors.New("NewContentfulClient: SpaceID cannot be empty")
|
||||
}
|
||||
@ -529,7 +529,7 @@ func NewContentfulClient(spaceID string, clientMode ClientMode, clientKey string
|
||||
SpaceID: spaceID,
|
||||
sync: clientMode == ClientModeCDA,
|
||||
}
|
||||
_, err = cc.Client.Spaces.Get(spaceID)
|
||||
_, err = cc.Client.Spaces.Get(ctx, spaceID)
|
||||
if err != nil {
|
||||
_, ok := err.(contentful.NotFoundError)
|
||||
if ok {
|
||||
@ -718,6 +718,7 @@ func (cc *ContentfulClient) syncCache(ctx context.Context, contentTypes []string
|
||||
for {
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
col := cc.Client.Entries.Sync(
|
||||
ctx,
|
||||
cc.SpaceID,
|
||||
cc.syncToken == "",
|
||||
cc.syncToken,
|
||||
@ -801,7 +802,7 @@ func (cc *ContentfulClient) cacheSpace(ctx context.Context, contentTypes []strin
|
||||
if cacheAssets {
|
||||
contentTypes = append([]string{assetWorkerType}, contentTypes...)
|
||||
}
|
||||
_, errCanWeEvenConnect := cc.Client.Spaces.Get(cc.SpaceID)
|
||||
_, errCanWeEvenConnect := cc.Client.Spaces.Get(ctx, cc.SpaceID)
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
offlinePreviousState := cc.offline
|
||||
cc.cacheMutex.sharedDataGcLock.RUnlock()
|
||||
@ -931,7 +932,7 @@ func (cc *ContentfulClient) cacheGcAssetByID(ctx context.Context, id string, ass
|
||||
if cc.Client == nil {
|
||||
return errors.New("cacheGcAssetByID: No client available")
|
||||
}
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Equal("sys.id", id)
|
||||
_, err := col.Next()
|
||||
if err != nil {
|
||||
@ -1010,7 +1011,7 @@ func getContentfulAPIClient(clientMode ClientMode, clientKey string) (*contentfu
|
||||
}
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) getAllAssets(tryCacheFirst bool) (map[string]*contentful.Asset, error) {
|
||||
func (cc *ContentfulClient) getAllAssets(ctx context.Context, tryCacheFirst bool) (map[string]*contentful.Asset, error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getAllAssets: No client available")
|
||||
}
|
||||
@ -1029,7 +1030,7 @@ func (cc *ContentfulClient) getAllAssets(tryCacheFirst bool) (map[string]*conten
|
||||
allItems = append(allItems, asset)
|
||||
}
|
||||
} else {
|
||||
col := cc.Client.Assets.List(cc.SpaceID)
|
||||
col := cc.Client.Assets.List(ctx, cc.SpaceID)
|
||||
col.Query.Locale("*").Limit(assetPageSize)
|
||||
for {
|
||||
_, err := col.Next()
|
||||
@ -1075,8 +1076,8 @@ func getOfflineSpaceFromFile(filename string) (*offlineTemp, error) {
|
||||
return offlineTemp, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) optimisticPageSizeGetAll(contentType string, limit uint16) (*contentful.Collection, error) {
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
func (cc *ContentfulClient) optimisticPageSizeGetAll(ctx context.Context, contentType string, limit uint16) (*contentful.Collection, error) {
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType(contentType).Locale("*").Include(0).Limit(limit)
|
||||
allItems := []interface{}{}
|
||||
var err error
|
||||
@ -1095,7 +1096,7 @@ func (cc *ContentfulClient) optimisticPageSizeGetAll(contentType string, limit u
|
||||
case contentful.ErrorResponse:
|
||||
msg := errTyped.Message
|
||||
if strings.Contains(msg, "Response size too big") && limit >= 20 {
|
||||
smallerPageCol, err := cc.optimisticPageSizeGetAll(contentType, limit/2)
|
||||
smallerPageCol, err := cc.optimisticPageSizeGetAll(ctx, contentType, limit/2)
|
||||
return smallerPageCol, err
|
||||
}
|
||||
return nil, err
|
||||
@ -1544,7 +1545,7 @@ func updateCacheForContentType(ctx context.Context, results chan ContentTypeResu
|
||||
}
|
||||
|
||||
case assetWorkerType:
|
||||
allAssets, err := cc.getAllAssets(false)
|
||||
allAssets, err := cc.getAllAssets(ctx, false)
|
||||
if err != nil {
|
||||
return errors.New("updateCacheForContentType failed for assets")
|
||||
}
|
||||
@ -1622,7 +1623,7 @@ func updateCacheForContentTypeAndEntity(ctx context.Context, cc *ContentfulClien
|
||||
return nil
|
||||
}
|
||||
|
||||
func commonGetParents(cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
|
||||
func commonGetParents(ctx context.Context, cc *ContentfulClient, id string, contentType []string) (parents []EntryReference, err error) {
|
||||
parents = []EntryReference{}
|
||||
cc.cacheMutex.sharedDataGcLock.RLock()
|
||||
cacheInit := cc.cacheInit
|
||||
@ -1640,7 +1641,7 @@ func commonGetParents(cc *ContentfulClient, id string, contentType []string) (pa
|
||||
}
|
||||
return cc.Cache.parentMap[id], nil
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.Equal("links_to_entry", id).Locale("*")
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
|
||||
@ -18,7 +18,7 @@ const ContentTypeBrand = "brand"
|
||||
|
||||
// ---Brand public methods---
|
||||
|
||||
func (cc *ContentfulClient) GetAllBrand() (voMap map[string]*CfBrand, err error) {
|
||||
func (cc *ContentfulClient) GetAllBrand(ctx context.Context) (voMap map[string]*CfBrand, err error) {
|
||||
if cc == nil {
|
||||
return nil, errors.New("GetAllBrand: No client available")
|
||||
}
|
||||
@ -29,7 +29,7 @@ func (cc *ContentfulClient) GetAllBrand() (voMap map[string]*CfBrand, err error)
|
||||
if cacheInit {
|
||||
return cc.Cache.entryMaps.brand, nil
|
||||
}
|
||||
col, err := cc.optimisticPageSizeGetAll("brand", optimisticPageSize)
|
||||
col, err := cc.optimisticPageSizeGetAll(ctx, "brand", optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -44,11 +44,11 @@ func (cc *ContentfulClient) GetAllBrand() (voMap map[string]*CfBrand, err error)
|
||||
return brandMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetFilteredBrand(query *contentful.Query) (voMap map[string]*CfBrand, err error) {
|
||||
func (cc *ContentfulClient) GetFilteredBrand(ctx context.Context, query *contentful.Query) (voMap map[string]*CfBrand, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getFilteredBrand: No client available")
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
if query != nil {
|
||||
col.Query = *query
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (cc *ContentfulClient) GetFilteredBrand(query *contentful.Query) (voMap map
|
||||
return brandMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetBrandByID(id string, forceNoCache ...bool) (vo *CfBrand, err error) {
|
||||
func (cc *ContentfulClient) GetBrandByID(ctx context.Context, id string, forceNoCache ...bool) (vo *CfBrand, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("GetBrandByID: No client available")
|
||||
}
|
||||
@ -81,7 +81,7 @@ func (cc *ContentfulClient) GetBrandByID(id string, forceNoCache ...bool) (vo *C
|
||||
}
|
||||
return nil, fmt.Errorf("GetBrandByID: entry '%s' not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("brand").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
@ -123,14 +123,14 @@ func NewCfBrand(contentfulClient ...*ContentfulClient) (cfBrand *CfBrand) {
|
||||
cfBrand.Sys.ContentType.Sys.LinkType = "ContentType"
|
||||
return
|
||||
}
|
||||
func (vo *CfBrand) GetParents(contentType ...string) (parents []EntryReference, err error) {
|
||||
func (vo *CfBrand) GetParents(ctx context.Context, contentType ...string) (parents []EntryReference, err error) {
|
||||
if vo == nil {
|
||||
return nil, errors.New("GetParents: Value Object is nil")
|
||||
}
|
||||
if vo.CC == nil {
|
||||
return nil, errors.New("GetParents: Value Object has no Contentful Client set")
|
||||
}
|
||||
return commonGetParents(vo.CC, vo.Sys.ID, contentType)
|
||||
return commonGetParents(ctx, vo.CC, vo.Sys.ID, contentType)
|
||||
}
|
||||
|
||||
func (vo *CfBrand) GetPublishingStatus() string {
|
||||
@ -185,7 +185,7 @@ func (vo *CfBrand) CompanyName(locale ...Locale) string {
|
||||
return vo.Fields.CompanyName[string(loc)]
|
||||
}
|
||||
|
||||
func (vo *CfBrand) Logo(locale ...Locale) *contentful.AssetNoLocale {
|
||||
func (vo *CfBrand) Logo(ctx context.Context, locale ...Locale) *contentful.AssetNoLocale {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -222,7 +222,7 @@ func (vo *CfBrand) Logo(locale ...Locale) *contentful.AssetNoLocale {
|
||||
}
|
||||
}
|
||||
localizedLogo := vo.Fields.Logo[string(loc)]
|
||||
asset, err := vo.CC.GetAssetByID(localizedLogo.Sys.ID)
|
||||
asset, err := vo.CC.GetAssetByID(ctx, localizedLogo.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel == LogDebug {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Logo()"}, LogError, ErrNoTypeOfRefAsset)
|
||||
@ -577,7 +577,7 @@ func (vo *CfBrand) SetPhone(phone []string, locale ...Locale) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (vo *CfBrand) UpsertEntry() (err error) {
|
||||
func (vo *CfBrand) UpsertEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpsertEntry: Value Object is nil")
|
||||
}
|
||||
@ -597,13 +597,13 @@ func (vo *CfBrand) UpsertEntry() (err error) {
|
||||
return errors.New("CfBrand UpsertEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfBrand UpsertEntry: Operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfBrand) PublishEntry() (err error) {
|
||||
func (vo *CfBrand) PublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("PublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -622,13 +622,13 @@ func (vo *CfBrand) PublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfBrand PublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfBrand PublishEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfBrand) UnpublishEntry() (err error) {
|
||||
func (vo *CfBrand) UnpublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UnpublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -647,13 +647,13 @@ func (vo *CfBrand) UnpublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfBrand UnpublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfBrand UnpublishEntry: unpublish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfBrand) UpdateEntry() (err error) {
|
||||
func (vo *CfBrand) UpdateEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpdateEntry: Value Object is nil")
|
||||
}
|
||||
@ -673,7 +673,7 @@ func (vo *CfBrand) UpdateEntry() (err error) {
|
||||
return errors.New("CfBrand UpdateEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfBrand UpdateEntry: upsert operation failed: %w", err)
|
||||
}
|
||||
@ -685,13 +685,13 @@ func (vo *CfBrand) UpdateEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfBrand UpdateEntry: Can't unmarshal JSON back into VO")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfBrand UpdateEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfBrand) DeleteEntry() (err error) {
|
||||
func (vo *CfBrand) DeleteEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("DeleteEntry: Value Object is nil")
|
||||
}
|
||||
@ -711,12 +711,12 @@ func (vo *CfBrand) DeleteEntry() (err error) {
|
||||
return errors.New("CfBrand DeleteEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
if cfEntry.Sys.PublishedCounter > 0 {
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return fmt.Errorf("CfBrand DeleteEntry: Unpublish entry failed: %w", errUnpublish)
|
||||
}
|
||||
}
|
||||
errDelete := vo.CC.Client.Entries.Delete(vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
errDelete := vo.CC.Client.Entries.Delete(ctx, vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
if errDelete != nil {
|
||||
return fmt.Errorf("CfBrand DeleteEntry: Delete entry failed: %w", errDelete)
|
||||
}
|
||||
@ -749,7 +749,7 @@ func (cc *ContentfulClient) cacheAllBrand(ctx context.Context, resultChan chan<-
|
||||
}
|
||||
}
|
||||
} else {
|
||||
col, err = cc.optimisticPageSizeGetAll("brand", cc.optimisticPageSize)
|
||||
col, err = cc.optimisticPageSizeGetAll(ctx, "brand", cc.optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, errors.New("optimisticPageSizeGetAll for Brand failed: " + err.Error())
|
||||
}
|
||||
@ -761,7 +761,7 @@ func (cc *ContentfulClient) cacheAllBrand(ctx context.Context, resultChan chan<-
|
||||
brandMap := map[string]*CfBrand{}
|
||||
for _, brand := range allBrand {
|
||||
if cc.cacheInit {
|
||||
existingBrand, err := cc.GetBrandByID(brand.Sys.ID)
|
||||
existingBrand, err := cc.GetBrandByID(ctx, brand.Sys.ID)
|
||||
if err == nil && existingBrand != nil && existingBrand.Sys.Version > brand.Sys.Version {
|
||||
return nil, fmt.Errorf("cache update canceled because Brand entry %s is newer in cache", brand.Sys.ID)
|
||||
}
|
||||
@ -807,7 +807,7 @@ func (cc *ContentfulClient) cacheBrandByID(ctx context.Context, id string, entry
|
||||
return errors.New("cacheBrandByID: No client available")
|
||||
}
|
||||
if !entryDelete {
|
||||
col = cc.Client.Entries.List(cc.SpaceID)
|
||||
col = cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("brand").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
|
||||
@ -18,7 +18,7 @@ const ContentTypeCategory = "category"
|
||||
|
||||
// ---Category public methods---
|
||||
|
||||
func (cc *ContentfulClient) GetAllCategory() (voMap map[string]*CfCategory, err error) {
|
||||
func (cc *ContentfulClient) GetAllCategory(ctx context.Context) (voMap map[string]*CfCategory, err error) {
|
||||
if cc == nil {
|
||||
return nil, errors.New("GetAllCategory: No client available")
|
||||
}
|
||||
@ -29,7 +29,7 @@ func (cc *ContentfulClient) GetAllCategory() (voMap map[string]*CfCategory, err
|
||||
if cacheInit {
|
||||
return cc.Cache.entryMaps.category, nil
|
||||
}
|
||||
col, err := cc.optimisticPageSizeGetAll("category", optimisticPageSize)
|
||||
col, err := cc.optimisticPageSizeGetAll(ctx, "category", optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -44,11 +44,11 @@ func (cc *ContentfulClient) GetAllCategory() (voMap map[string]*CfCategory, err
|
||||
return categoryMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetFilteredCategory(query *contentful.Query) (voMap map[string]*CfCategory, err error) {
|
||||
func (cc *ContentfulClient) GetFilteredCategory(ctx context.Context, query *contentful.Query) (voMap map[string]*CfCategory, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getFilteredCategory: No client available")
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
if query != nil {
|
||||
col.Query = *query
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (cc *ContentfulClient) GetFilteredCategory(query *contentful.Query) (voMap
|
||||
return categoryMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetCategoryByID(id string, forceNoCache ...bool) (vo *CfCategory, err error) {
|
||||
func (cc *ContentfulClient) GetCategoryByID(ctx context.Context, id string, forceNoCache ...bool) (vo *CfCategory, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("GetCategoryByID: No client available")
|
||||
}
|
||||
@ -81,7 +81,7 @@ func (cc *ContentfulClient) GetCategoryByID(id string, forceNoCache ...bool) (vo
|
||||
}
|
||||
return nil, fmt.Errorf("GetCategoryByID: entry '%s' not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("category").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
@ -115,14 +115,14 @@ func NewCfCategory(contentfulClient ...*ContentfulClient) (cfCategory *CfCategor
|
||||
cfCategory.Sys.ContentType.Sys.LinkType = "ContentType"
|
||||
return
|
||||
}
|
||||
func (vo *CfCategory) GetParents(contentType ...string) (parents []EntryReference, err error) {
|
||||
func (vo *CfCategory) GetParents(ctx context.Context, contentType ...string) (parents []EntryReference, err error) {
|
||||
if vo == nil {
|
||||
return nil, errors.New("GetParents: Value Object is nil")
|
||||
}
|
||||
if vo.CC == nil {
|
||||
return nil, errors.New("GetParents: Value Object has no Contentful Client set")
|
||||
}
|
||||
return commonGetParents(vo.CC, vo.Sys.ID, contentType)
|
||||
return commonGetParents(ctx, vo.CC, vo.Sys.ID, contentType)
|
||||
}
|
||||
|
||||
func (vo *CfCategory) GetPublishingStatus() string {
|
||||
@ -177,7 +177,7 @@ func (vo *CfCategory) Title(locale ...Locale) string {
|
||||
return vo.Fields.Title[string(loc)]
|
||||
}
|
||||
|
||||
func (vo *CfCategory) Icon(locale ...Locale) *contentful.AssetNoLocale {
|
||||
func (vo *CfCategory) Icon(ctx context.Context, locale ...Locale) *contentful.AssetNoLocale {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -214,7 +214,7 @@ func (vo *CfCategory) Icon(locale ...Locale) *contentful.AssetNoLocale {
|
||||
}
|
||||
}
|
||||
localizedIcon := vo.Fields.Icon[string(loc)]
|
||||
asset, err := vo.CC.GetAssetByID(localizedIcon.Sys.ID)
|
||||
asset, err := vo.CC.GetAssetByID(ctx, localizedIcon.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel == LogDebug {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Icon()"}, LogError, ErrNoTypeOfRefAsset)
|
||||
@ -341,7 +341,7 @@ func (vo *CfCategory) SetCategoryDescription(categoryDescription string, locale
|
||||
return
|
||||
}
|
||||
|
||||
func (vo *CfCategory) UpsertEntry() (err error) {
|
||||
func (vo *CfCategory) UpsertEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpsertEntry: Value Object is nil")
|
||||
}
|
||||
@ -361,13 +361,13 @@ func (vo *CfCategory) UpsertEntry() (err error) {
|
||||
return errors.New("CfCategory UpsertEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfCategory UpsertEntry: Operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfCategory) PublishEntry() (err error) {
|
||||
func (vo *CfCategory) PublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("PublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -386,13 +386,13 @@ func (vo *CfCategory) PublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfCategory PublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfCategory PublishEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfCategory) UnpublishEntry() (err error) {
|
||||
func (vo *CfCategory) UnpublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UnpublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -411,13 +411,13 @@ func (vo *CfCategory) UnpublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfCategory UnpublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfCategory UnpublishEntry: unpublish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfCategory) UpdateEntry() (err error) {
|
||||
func (vo *CfCategory) UpdateEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpdateEntry: Value Object is nil")
|
||||
}
|
||||
@ -437,7 +437,7 @@ func (vo *CfCategory) UpdateEntry() (err error) {
|
||||
return errors.New("CfCategory UpdateEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfCategory UpdateEntry: upsert operation failed: %w", err)
|
||||
}
|
||||
@ -449,13 +449,13 @@ func (vo *CfCategory) UpdateEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfCategory UpdateEntry: Can't unmarshal JSON back into VO")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfCategory UpdateEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfCategory) DeleteEntry() (err error) {
|
||||
func (vo *CfCategory) DeleteEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("DeleteEntry: Value Object is nil")
|
||||
}
|
||||
@ -475,12 +475,12 @@ func (vo *CfCategory) DeleteEntry() (err error) {
|
||||
return errors.New("CfCategory DeleteEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
if cfEntry.Sys.PublishedCounter > 0 {
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return fmt.Errorf("CfCategory DeleteEntry: Unpublish entry failed: %w", errUnpublish)
|
||||
}
|
||||
}
|
||||
errDelete := vo.CC.Client.Entries.Delete(vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
errDelete := vo.CC.Client.Entries.Delete(ctx, vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
if errDelete != nil {
|
||||
return fmt.Errorf("CfCategory DeleteEntry: Delete entry failed: %w", errDelete)
|
||||
}
|
||||
@ -513,7 +513,7 @@ func (cc *ContentfulClient) cacheAllCategory(ctx context.Context, resultChan cha
|
||||
}
|
||||
}
|
||||
} else {
|
||||
col, err = cc.optimisticPageSizeGetAll("category", cc.optimisticPageSize)
|
||||
col, err = cc.optimisticPageSizeGetAll(ctx, "category", cc.optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, errors.New("optimisticPageSizeGetAll for Category failed: " + err.Error())
|
||||
}
|
||||
@ -525,7 +525,7 @@ func (cc *ContentfulClient) cacheAllCategory(ctx context.Context, resultChan cha
|
||||
categoryMap := map[string]*CfCategory{}
|
||||
for _, category := range allCategory {
|
||||
if cc.cacheInit {
|
||||
existingCategory, err := cc.GetCategoryByID(category.Sys.ID)
|
||||
existingCategory, err := cc.GetCategoryByID(ctx, category.Sys.ID)
|
||||
if err == nil && existingCategory != nil && existingCategory.Sys.Version > category.Sys.Version {
|
||||
return nil, fmt.Errorf("cache update canceled because Category entry %s is newer in cache", category.Sys.ID)
|
||||
}
|
||||
@ -571,7 +571,7 @@ func (cc *ContentfulClient) cacheCategoryByID(ctx context.Context, id string, en
|
||||
return errors.New("cacheCategoryByID: No client available")
|
||||
}
|
||||
if !entryDelete {
|
||||
col = cc.Client.Entries.List(cc.SpaceID)
|
||||
col = cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("category").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
|
||||
@ -18,7 +18,7 @@ const ContentTypeProduct = "product"
|
||||
|
||||
// ---Product public methods---
|
||||
|
||||
func (cc *ContentfulClient) GetAllProduct() (voMap map[string]*CfProduct, err error) {
|
||||
func (cc *ContentfulClient) GetAllProduct(ctx context.Context) (voMap map[string]*CfProduct, err error) {
|
||||
if cc == nil {
|
||||
return nil, errors.New("GetAllProduct: No client available")
|
||||
}
|
||||
@ -29,7 +29,7 @@ func (cc *ContentfulClient) GetAllProduct() (voMap map[string]*CfProduct, err er
|
||||
if cacheInit {
|
||||
return cc.Cache.entryMaps.product, nil
|
||||
}
|
||||
col, err := cc.optimisticPageSizeGetAll("product", optimisticPageSize)
|
||||
col, err := cc.optimisticPageSizeGetAll(ctx, "product", optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -44,11 +44,11 @@ func (cc *ContentfulClient) GetAllProduct() (voMap map[string]*CfProduct, err er
|
||||
return productMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetFilteredProduct(query *contentful.Query) (voMap map[string]*CfProduct, err error) {
|
||||
func (cc *ContentfulClient) GetFilteredProduct(ctx context.Context, query *contentful.Query) (voMap map[string]*CfProduct, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("getFilteredProduct: No client available")
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
if query != nil {
|
||||
col.Query = *query
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (cc *ContentfulClient) GetFilteredProduct(query *contentful.Query) (voMap m
|
||||
return productMap, nil
|
||||
}
|
||||
|
||||
func (cc *ContentfulClient) GetProductByID(id string, forceNoCache ...bool) (vo *CfProduct, err error) {
|
||||
func (cc *ContentfulClient) GetProductByID(ctx context.Context, id string, forceNoCache ...bool) (vo *CfProduct, err error) {
|
||||
if cc == nil || cc.Client == nil {
|
||||
return nil, errors.New("GetProductByID: No client available")
|
||||
}
|
||||
@ -81,7 +81,7 @@ func (cc *ContentfulClient) GetProductByID(id string, forceNoCache ...bool) (vo
|
||||
}
|
||||
return nil, fmt.Errorf("GetProductByID: entry '%s' not found in cache", id)
|
||||
}
|
||||
col := cc.Client.Entries.List(cc.SpaceID)
|
||||
col := cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("product").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err = col.GetAll()
|
||||
if err != nil {
|
||||
@ -137,14 +137,14 @@ func NewCfProduct(contentfulClient ...*ContentfulClient) (cfProduct *CfProduct)
|
||||
cfProduct.Sys.ContentType.Sys.LinkType = "ContentType"
|
||||
return
|
||||
}
|
||||
func (vo *CfProduct) GetParents(contentType ...string) (parents []EntryReference, err error) {
|
||||
func (vo *CfProduct) GetParents(ctx context.Context, contentType ...string) (parents []EntryReference, err error) {
|
||||
if vo == nil {
|
||||
return nil, errors.New("GetParents: Value Object is nil")
|
||||
}
|
||||
if vo.CC == nil {
|
||||
return nil, errors.New("GetParents: Value Object has no Contentful Client set")
|
||||
}
|
||||
return commonGetParents(vo.CC, vo.Sys.ID, contentType)
|
||||
return commonGetParents(ctx, vo.CC, vo.Sys.ID, contentType)
|
||||
}
|
||||
|
||||
func (vo *CfProduct) GetPublishingStatus() string {
|
||||
@ -310,7 +310,7 @@ func (vo *CfProduct) Sizetypecolor(locale ...Locale) string {
|
||||
return vo.Fields.Sizetypecolor[string(loc)]
|
||||
}
|
||||
|
||||
func (vo *CfProduct) Image(locale ...Locale) []*contentful.AssetNoLocale {
|
||||
func (vo *CfProduct) Image(ctx context.Context, locale ...Locale) []*contentful.AssetNoLocale {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -348,7 +348,7 @@ func (vo *CfProduct) Image(locale ...Locale) []*contentful.AssetNoLocale {
|
||||
}
|
||||
}
|
||||
for _, eachLocalizedImage := range vo.Fields.Image[string(loc)] {
|
||||
asset, err := vo.CC.GetAssetByID(eachLocalizedImage.Sys.ID)
|
||||
asset, err := vo.CC.GetAssetByID(ctx, eachLocalizedImage.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel == LogDebug {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Image()"}, LogError, ErrNoTypeOfRefAsset)
|
||||
@ -415,7 +415,7 @@ func (vo *CfProduct) Tags(locale ...Locale) []string {
|
||||
return vo.Fields.Tags[string(loc)]
|
||||
}
|
||||
|
||||
func (vo *CfProduct) Categories(locale ...Locale) []*EntryReference {
|
||||
func (vo *CfProduct) Categories(ctx context.Context, locale ...Locale) []*EntryReference {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -451,7 +451,7 @@ func (vo *CfProduct) Categories(locale ...Locale) []*EntryReference {
|
||||
}
|
||||
}
|
||||
for _, eachLocalizedCategories := range vo.Fields.Categories[string(loc)] {
|
||||
contentType, err := vo.CC.GetContentTypeOfID(eachLocalizedCategories.Sys.ID)
|
||||
contentType, err := vo.CC.GetContentTypeOfID(ctx, eachLocalizedCategories.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Categories()"}, LogError, ErrNoTypeOfRefEntry)
|
||||
@ -461,7 +461,7 @@ func (vo *CfProduct) Categories(locale ...Locale) []*EntryReference {
|
||||
switch contentType {
|
||||
|
||||
case ContentTypeBrand:
|
||||
referencedVO, err := vo.CC.GetBrandByID(eachLocalizedCategories.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetBrandByID(ctx, eachLocalizedCategories.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Categories()"}, LogError, err)
|
||||
@ -471,7 +471,7 @@ func (vo *CfProduct) Categories(locale ...Locale) []*EntryReference {
|
||||
categories = append(categories, &EntryReference{ContentType: contentType, ID: eachLocalizedCategories.Sys.ID, VO: referencedVO})
|
||||
|
||||
case ContentTypeCategory:
|
||||
referencedVO, err := vo.CC.GetCategoryByID(eachLocalizedCategories.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetCategoryByID(ctx, eachLocalizedCategories.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Categories()"}, LogError, err)
|
||||
@ -481,7 +481,7 @@ func (vo *CfProduct) Categories(locale ...Locale) []*EntryReference {
|
||||
categories = append(categories, &EntryReference{ContentType: contentType, ID: eachLocalizedCategories.Sys.ID, VO: referencedVO})
|
||||
|
||||
case ContentTypeProduct:
|
||||
referencedVO, err := vo.CC.GetProductByID(eachLocalizedCategories.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetProductByID(ctx, eachLocalizedCategories.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Categories()"}, LogError, err)
|
||||
@ -532,7 +532,7 @@ func (vo *CfProduct) Price(locale ...Locale) float64 {
|
||||
return vo.Fields.Price[string(loc)]
|
||||
}
|
||||
|
||||
func (vo *CfProduct) Brand(locale ...Locale) *EntryReference {
|
||||
func (vo *CfProduct) Brand(ctx context.Context, locale ...Locale) *EntryReference {
|
||||
if vo == nil {
|
||||
return nil
|
||||
}
|
||||
@ -567,7 +567,7 @@ func (vo *CfProduct) Brand(locale ...Locale) *EntryReference {
|
||||
}
|
||||
}
|
||||
localizedBrand := vo.Fields.Brand[string(loc)]
|
||||
contentType, err := vo.CC.GetContentTypeOfID(localizedBrand.Sys.ID)
|
||||
contentType, err := vo.CC.GetContentTypeOfID(ctx, localizedBrand.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Brand()"}, LogError, ErrNoTypeOfRefEntry)
|
||||
@ -577,7 +577,7 @@ func (vo *CfProduct) Brand(locale ...Locale) *EntryReference {
|
||||
switch contentType {
|
||||
|
||||
case ContentTypeBrand:
|
||||
referencedVO, err := vo.CC.GetBrandByID(localizedBrand.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetBrandByID(ctx, localizedBrand.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Brand()"}, LogError, err)
|
||||
@ -587,7 +587,7 @@ func (vo *CfProduct) Brand(locale ...Locale) *EntryReference {
|
||||
return &EntryReference{ContentType: contentType, ID: localizedBrand.Sys.ID, VO: referencedVO}
|
||||
|
||||
case ContentTypeCategory:
|
||||
referencedVO, err := vo.CC.GetCategoryByID(localizedBrand.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetCategoryByID(ctx, localizedBrand.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Brand()"}, LogError, err)
|
||||
@ -597,7 +597,7 @@ func (vo *CfProduct) Brand(locale ...Locale) *EntryReference {
|
||||
return &EntryReference{ContentType: contentType, ID: localizedBrand.Sys.ID, VO: referencedVO}
|
||||
|
||||
case ContentTypeProduct:
|
||||
referencedVO, err := vo.CC.GetProductByID(localizedBrand.Sys.ID)
|
||||
referencedVO, err := vo.CC.GetProductByID(ctx, localizedBrand.Sys.ID)
|
||||
if err != nil {
|
||||
if vo.CC.logFn != nil && vo.CC.logLevel <= LogError {
|
||||
vo.CC.logFn(map[string]interface{}{"content type": vo.Sys.ContentType.Sys.ID, "entry ID": vo.Sys.ID, "method": "Brand()"}, LogError, err)
|
||||
@ -1079,7 +1079,7 @@ func (vo *CfProduct) SetNodes(nodes interface{}, locale ...Locale) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (vo *CfProduct) UpsertEntry() (err error) {
|
||||
func (vo *CfProduct) UpsertEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpsertEntry: Value Object is nil")
|
||||
}
|
||||
@ -1099,13 +1099,13 @@ func (vo *CfProduct) UpsertEntry() (err error) {
|
||||
return errors.New("CfProduct UpsertEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfProduct UpsertEntry: Operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfProduct) PublishEntry() (err error) {
|
||||
func (vo *CfProduct) PublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("PublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -1124,13 +1124,13 @@ func (vo *CfProduct) PublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfProduct PublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfProduct PublishEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfProduct) UnpublishEntry() (err error) {
|
||||
func (vo *CfProduct) UnpublishEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UnpublishEntry: Value Object is nil")
|
||||
}
|
||||
@ -1149,13 +1149,13 @@ func (vo *CfProduct) UnpublishEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfProduct UnpublishEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfProduct UnpublishEntry: unpublish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfProduct) UpdateEntry() (err error) {
|
||||
func (vo *CfProduct) UpdateEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("UpdateEntry: Value Object is nil")
|
||||
}
|
||||
@ -1175,7 +1175,7 @@ func (vo *CfProduct) UpdateEntry() (err error) {
|
||||
return errors.New("CfProduct UpdateEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
|
||||
err = vo.CC.Client.Entries.Upsert(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Upsert(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfProduct UpdateEntry: upsert operation failed: %w", err)
|
||||
}
|
||||
@ -1187,13 +1187,13 @@ func (vo *CfProduct) UpdateEntry() (err error) {
|
||||
if errUnmarshal != nil {
|
||||
return errors.New("CfProduct UpdateEntry: Can't unmarshal JSON back into VO")
|
||||
}
|
||||
err = vo.CC.Client.Entries.Publish(vo.CC.SpaceID, cfEntry)
|
||||
err = vo.CC.Client.Entries.Publish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CfProduct UpdateEntry: publish operation failed: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
func (vo *CfProduct) DeleteEntry() (err error) {
|
||||
func (vo *CfProduct) DeleteEntry(ctx context.Context) (err error) {
|
||||
if vo == nil {
|
||||
return errors.New("DeleteEntry: Value Object is nil")
|
||||
}
|
||||
@ -1213,12 +1213,12 @@ func (vo *CfProduct) DeleteEntry() (err error) {
|
||||
return errors.New("CfProduct DeleteEntry: Can't unmarshal JSON into CF entry")
|
||||
}
|
||||
if cfEntry.Sys.PublishedCounter > 0 {
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(vo.CC.SpaceID, cfEntry)
|
||||
errUnpublish := vo.CC.Client.Entries.Unpublish(ctx, vo.CC.SpaceID, cfEntry)
|
||||
if errUnpublish != nil && !strings.Contains(errUnpublish.Error(), "Not published") {
|
||||
return fmt.Errorf("CfProduct DeleteEntry: Unpublish entry failed: %w", errUnpublish)
|
||||
}
|
||||
}
|
||||
errDelete := vo.CC.Client.Entries.Delete(vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
errDelete := vo.CC.Client.Entries.Delete(ctx, vo.CC.SpaceID, cfEntry.Sys.ID)
|
||||
if errDelete != nil {
|
||||
return fmt.Errorf("CfProduct DeleteEntry: Delete entry failed: %w", errDelete)
|
||||
}
|
||||
@ -1251,7 +1251,7 @@ func (cc *ContentfulClient) cacheAllProduct(ctx context.Context, resultChan chan
|
||||
}
|
||||
}
|
||||
} else {
|
||||
col, err = cc.optimisticPageSizeGetAll("product", cc.optimisticPageSize)
|
||||
col, err = cc.optimisticPageSizeGetAll(ctx, "product", cc.optimisticPageSize)
|
||||
if err != nil {
|
||||
return nil, errors.New("optimisticPageSizeGetAll for Product failed: " + err.Error())
|
||||
}
|
||||
@ -1263,7 +1263,7 @@ func (cc *ContentfulClient) cacheAllProduct(ctx context.Context, resultChan chan
|
||||
productMap := map[string]*CfProduct{}
|
||||
for _, product := range allProduct {
|
||||
if cc.cacheInit {
|
||||
existingProduct, err := cc.GetProductByID(product.Sys.ID)
|
||||
existingProduct, err := cc.GetProductByID(ctx, product.Sys.ID)
|
||||
if err == nil && existingProduct != nil && existingProduct.Sys.Version > product.Sys.Version {
|
||||
return nil, fmt.Errorf("cache update canceled because Product entry %s is newer in cache", product.Sys.ID)
|
||||
}
|
||||
@ -1333,7 +1333,7 @@ func (cc *ContentfulClient) cacheProductByID(ctx context.Context, id string, ent
|
||||
return errors.New("cacheProductByID: No client available")
|
||||
}
|
||||
if !entryDelete {
|
||||
col = cc.Client.Entries.List(cc.SpaceID)
|
||||
col = cc.Client.Entries.List(ctx, cc.SpaceID)
|
||||
col.Query.ContentType("product").Locale("*").Include(0).Equal("sys.id", id)
|
||||
_, err := col.GetAll()
|
||||
if err != nil {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user