本文整理汇总了Golang中github.com/corestoreio/csfw/store/scope.Option.StoreCode方法的典型用法代码示例。如果您正苦于以下问题:Golang Option.StoreCode方法的具体用法?Golang Option.StoreCode怎么用?Golang Option.StoreCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/corestoreio/csfw/store/scope.Option
的用法示例。
在下文中一共展示了Option.StoreCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testStoreCodeFrom
func testStoreCodeFrom(t *testing.T, i int, haveErr, wantErr error, haveScope scope.Option, wantScope scope.Scope, wantCode string, wantID int64) {
if wantErr != nil {
assert.EqualError(t, haveErr, wantErr.Error(), "Index: %d", i)
}
switch sos := haveScope.Scope(); sos {
case scope.StoreID:
assert.Exactly(t, wantID, haveScope.Store.StoreID(), "Index: %d", i)
case scope.GroupID:
assert.Exactly(t, wantID, haveScope.Group.GroupID(), "Index: %d", i)
case scope.WebsiteID:
assert.Exactly(t, wantID, haveScope.Website.WebsiteID(), "Index: %d", i)
case scope.DefaultID:
assert.Nil(t, haveScope.Store, "Index: %d", i)
assert.Nil(t, haveScope.Group, "Index: %d", i)
assert.Nil(t, haveScope.Website, "Index: %d", i)
default:
t.Fatalf("Unknown scope: %d", sos)
}
assert.Exactly(t, wantScope, haveScope.Scope(), "Index: %d", i)
assert.Exactly(t, wantCode, haveScope.StoreCode(), "Index: %d", i)
}
示例2: WithInitStoreByFormCookie
// WithInitStoreByFormCookie reads from a GET parameter or cookie the store code.
// Checks if the store code is valid and allowed. If so it adjusts the context.Context
// to provide the new requestedStore.
//
// It calls Reader.RequestedStore() to determine the correct store.
// 1. check cookie store, always a string and the store code
// 2. check for GET ___store variable, always a string and the store code
func WithInitStoreByFormCookie() ctxhttp.Middleware {
return func(hf ctxhttp.HandlerFunc) ctxhttp.HandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
storeService, requestedStore, err := FromContextReader(ctx)
if err != nil {
if PkgLog.IsDebug() {
PkgLog.Debug("store.WithInitStoreByToken.FromContextServiceReader", "err", err, "ctx", ctx)
}
return errgo.Mask(err)
}
var reqSO scope.Option
reqSO, err = CodeFromRequestGET(r)
if err != nil {
if PkgLog.IsDebug() {
PkgLog.Debug("store.WithInitStoreByFormCookie.StoreCodeFromRequestGET", "err", err, "req", r, "scope", reqSO)
}
reqSO, err = CodeFromCookie(r)
if err != nil {
// ignore further processing because all codes are invalid or not found
if PkgLog.IsDebug() {
PkgLog.Debug("store.WithInitStoreByFormCookie.StoreCodeFromCookie", "err", err, "req", r, "scope", reqSO)
}
return hf.ServeHTTPContext(ctx, w, r)
}
}
var newRequestedStore *Store
if newRequestedStore, err = storeService.RequestedStore(reqSO); err != nil {
if PkgLog.IsDebug() {
PkgLog.Debug("store.WithInitStoreByFormCookie.storeService.RequestedStore", "err", err, "req", r, "scope", reqSO)
}
return errgo.Mask(err)
}
soStoreCode := reqSO.StoreCode()
// delete and re-set a new cookie, adjust context.Context
if newRequestedStore != nil && newRequestedStore.Data.Code.String == soStoreCode {
wds, err := newRequestedStore.Website.DefaultStore()
if err != nil {
if PkgLog.IsDebug() {
PkgLog.Debug("store.WithInitStoreByFormCookie.Website.DefaultStore", "err", err, "soStoreCode", soStoreCode)
}
return errgo.Mask(err)
}
if wds.Data.Code.String == soStoreCode {
newRequestedStore.DeleteCookie(w) // cookie not needed anymore
} else {
newRequestedStore.SetCookie(w) // make sure we force set the new store
if newRequestedStore.StoreID() != requestedStore.StoreID() {
// this may lead to a bug because the previously set storeService and requestedStore
// will still exists and have not been removed.
ctx = WithContextReader(ctx, storeService, newRequestedStore)
}
}
}
return hf(ctx, w, r)
}
}
}