当前位置: 首页>>代码示例>>Golang>>正文


Golang appengine.Namespace函数代码示例

本文整理汇总了Golang中google/golang.org/appengine.Namespace函数的典型用法代码示例。如果您正苦于以下问题:Golang Namespace函数的具体用法?Golang Namespace怎么用?Golang Namespace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Namespace函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestNamespaceResetting

func TestNamespaceResetting(t *testing.T) {
	// These environment variables are necessary because *Query.Run will
	// call internal.FullyQualifiedAppID which checks these variables or falls
	// back to the Metadata service that is not available in tests.
	environ := []struct {
		key, value string
	}{
		{"GAE_LONG_APP_ID", "my-app-id"},
		{"GAE_PARTITION", "1"},
	}
	for _, v := range environ {
		old := os.Getenv(v.key)
		os.Setenv(v.key, v.value)
		v.value = old
	}
	defer func() { // Restore old environment after the test completes.
		for _, v := range environ {
			if v.value == "" {
				os.Unsetenv(v.key)
				continue
			}
			os.Setenv(v.key, v.value)
		}
	}()

	namec := make(chan *string, 1)
	c0 := aetesting.FakeSingleContext(t, "datastore_v3", "RunQuery", func(req *pb.Query, res *pb.QueryResult) error {
		namec <- req.NameSpace
		return fmt.Errorf("RPC error")
	})

	// Check that wrapping c0 in a namespace twice works correctly.
	c1, err := appengine.Namespace(c0, "A")
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}
	c2, err := appengine.Namespace(c1, "") // should act as the original context
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}

	q := NewQuery("SomeKind")

	q.Run(c0)
	if ns := <-namec; ns != nil {
		t.Errorf(`RunQuery with c0: ns = %q, want nil`, *ns)
	}

	q.Run(c1)
	if ns := <-namec; ns == nil {
		t.Error(`RunQuery with c1: ns = nil, want "A"`)
	} else if *ns != "A" {
		t.Errorf(`RunQuery with c1: ns = %q, want "A"`, *ns)
	}

	q.Run(c2)
	if ns := <-namec; ns != nil {
		t.Errorf(`RunQuery with c2: ns = %q, want nil`, *ns)
	}
}
开发者ID:trythings,项目名称:trythings,代码行数:60,代码来源:datastore_test.go

示例2: TestNamespaceResetting

func TestNamespaceResetting(t *testing.T) {
	namec := make(chan *string, 1)
	c0 := aetesting.FakeSingleContext(t, "memcache", "Get", func(req *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
		namec <- req.NameSpace
		return errRPC
	})

	// Check that wrapping c0 in a namespace twice works correctly.
	c1, err := appengine.Namespace(c0, "A")
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}
	c2, err := appengine.Namespace(c1, "") // should act as the original context
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}

	Get(c0, "key")
	if ns := <-namec; ns != nil {
		t.Errorf(`Get with c0: ns = %q, want nil`, *ns)
	}

	Get(c1, "key")
	if ns := <-namec; ns == nil {
		t.Error(`Get with c1: ns = nil, want "A"`)
	} else if *ns != "A" {
		t.Errorf(`Get with c1: ns = %q, want "A"`, *ns)
	}

	Get(c2, "key")
	if ns := <-namec; ns != nil {
		t.Errorf(`Get with c2: ns = %q, want nil`, *ns)
	}
}
开发者ID:trythings,项目名称:trythings,代码行数:34,代码来源:memcache_test.go

示例3: TestNamespaceResetting

func TestNamespaceResetting(t *testing.T) {
	var nsField *string
	c := aetesting.FakeSingleContext(t, "memcache", "Get", func(req *pb.MemcacheGetRequest, res *pb.MemcacheGetResponse) error {
		nsField = req.NameSpace
		return errRPC
	})

	// Check that wrapping c in a namespace twice works correctly.
	nc, err := appengine.Namespace(c, "A")
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}
	c0, err := appengine.Namespace(nc, "") // should act as the original context
	if err != nil {
		t.Fatalf("appengine.Namespace: %v", err)
	}

	Get(c, "key")
	if nsField != nil {
		t.Fatalf("Get with c yielded %q", *nsField)
	}
	Get(nc, "key")
	if nsField == nil || *nsField != "A" {
		t.Fatalf("Get with nc yielded %v", nsField)
	}
	Get(c0, "key")
	if nsField != nil && *nsField != "" {
		t.Fatalf("Get with c0 yielded %q", *nsField)
	}
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:30,代码来源:memcache_test.go

示例4: createInNamespace

func createInNamespace(c context.Context, namespace string) {
	ns, _ := appengine.Namespace(c, namespace)
	p := new(pet)
	p.Value = 1
	k := datastore.NewKey(ns, "pet", "", 1, nil)
	datastore.Put(ns, k, p)
}
开发者ID:CaptainCodeman,项目名称:go-appengine-mapper,代码行数:7,代码来源:namespace_range_test.go

示例5: CleanupDatastore

// CleanupDatastore is to remove all data in datastore
func CleanupDatastore(ctx context.Context, namespaces ...string) error {
	var dummy []interface{}
	logger := wcg.NewLogger(nil)
	logger.Debugf("[Fixture] --------- CleanupDatastore ---------")
	namespaceList := append(namespaces, "")
	for _, ns := range namespaceList {
		logger.Debugf("[Fixture] Cleanup: ns=%q", ns)
		var _ctx = ctx
		if ns != "" {
			_ctx, _ = appengine.Namespace(_ctx, ns)
		}
		err := wcg.RetryUntil(func() error {
			var keys []*datastore.Key
			var err error
			if keys, err = datastore.NewQuery("").KeysOnly().GetAll(_ctx, dummy); err != nil {
				return err
			}
			if err := datastore.DeleteMulti(_ctx, keys); err != nil {
				return err
			}
			count, _ := datastore.NewQuery("").KeysOnly().Count(_ctx)
			if count == 0 {
				return nil
			}
			return fmt.Errorf("Still have %d keys.", count)
		}, 10*time.Second, 100*time.Millisecond)
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:speedland,项目名称:service,代码行数:33,代码来源:fixture.go

示例6: NewKey

// NewKey returns a new *datastore.Key for `kind`
func (kind *Kind) NewKey(req *wcg.Request, key string, parent *datastore.Key) *datastore.Key {
	ctx, err := appengine.Namespace(gae.NewContext(req), kind.Namespace)
	if err != nil {
		panic(err)
	}
	return datastore.NewKey(ctx, string(kind.Name), key, 0, parent)
}
开发者ID:speedland,项目名称:service,代码行数:8,代码来源:kind.go

示例7: namespace

func namespace(ctx context.Context) context.Context {
	// assumes the user is logged in.
	ctx, err := appengine.Namespace(ctx, user.Current(ctx).ID)
	if err != nil {
		// ...
	}
	return ctx
}
开发者ID:wuman,项目名称:golang-samples,代码行数:8,代码来源:namespace_user.go

示例8: TestGetNamespacedKey

// TestGetNamespacedKey ensures issue https://goo.gl/rXU8nK is fixed so that
// memcache uses the namespace from the key instead of the context.
func TestGetNamespacedKey(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	const intVal = int64(12)
	type testEntity struct {
		IntVal int64
	}

	namespacedCtx, err := appengine.Namespace(c, "keyNamespace")
	if err != nil {
		t.Fatal(err)
	}

	key := datastore.NewKey(c, "Entity", "", 1, nil)
	namespacedKey := datastore.NewKey(namespacedCtx,
		"Entity", "", key.IntID(), nil)
	entity := &testEntity{intVal}

	if namespacedKey, err = nds.Put(c, namespacedKey, entity); err != nil {
		t.Fatal(err)
	}

	// Prime cache.
	if err := nds.Get(namespacedCtx, namespacedKey, &testEntity{}); err != nil {
		t.Fatal(err)
	}

	// Ensure that we get a value back from the cache by checking if the
	// datastore is called at all.
	entityFromCache := true
	nds.SetDatastoreGetMulti(func(c context.Context,
		keys []*datastore.Key, vals interface{}) error {
		if len(keys) != 0 {
			entityFromCache = false
		}
		return nil
	})
	if err := nds.Get(c, namespacedKey, &testEntity{}); err != nil {
		t.Fatal(err)
	}
	nds.SetDatastoreGetMulti(datastore.GetMulti)

	if !entityFromCache {
		t.Fatal("entity not obtained from cache")
	}

	if err := nds.Delete(namespacedCtx, namespacedKey); err != nil {
		t.Fatal(err)
	}

	entity = &testEntity{}
	if err := nds.Get(c, namespacedKey, entity); err == nil {
		t.Fatalf("expected no such entity error but got %+v", entity)
	} else if err != datastore.ErrNoSuchEntity {
		t.Fatal(err)
	}
}
开发者ID:zeekay,项目名称:nds,代码行数:60,代码来源:get_test.go

示例9: Namespace

// Namespace set the namespace
func (d *Driver) Namespace(namespace string) *Driver {
	var err error
	d.ctx, err = appengine.Namespace(d.ctx, namespace)
	d.namespace = namespace
	if err != nil {
		panic(err)
	}
	return d
}
开发者ID:speedland,项目名称:service,代码行数:10,代码来源:memcache.go

示例10: loadJsonToDatastore

func loadJsonToDatastore(ctx context.Context, pkey *datastore.Key, data map[string]interface{}, logger wcg.Logger) error {
	var kind string
	var ns string
	var keyval interface{}
	var key *datastore.Key
	var ok bool
	var err error
	if _, ok = data["_kind"]; !ok {
		return fmt.Errorf("Missing key `_kind`")
	}
	kind = data["_kind"].(string)
	if keyval, ok = data["_key"]; !ok {
		return fmt.Errorf("Missing key `_key`")
	}
	if _, ok = data["_ns"]; ok {
		ns = data["_ns"].(string)
		ctx, err = appengine.Namespace(ctx, ns)
		if err != nil {
			return fmt.Errorf("Could not change the namespace of %q, check _ns value: ", ns, err)
		}
	}

	switch keyval.(type) {
	case int64:
		key = datastore.NewKey(ctx, kind, "", keyval.(int64), pkey)
	case string:
		key = datastore.NewKey(ctx, kind, keyval.(string), 0, pkey)
	default:
		return fmt.Errorf("Invalid `_key` type.")
	}
	if _, err := datastore.Put(ctx, key, jsonSaver(data)); err != nil {
		return err
	}
	// Check the data is actually stored.
	if err := wcg.RetryUntil(func() error {
		var v jsonSaver
		if err := datastore.Get(ctx, key, &v); err != nil {
			return fmt.Errorf(
				"fixture is not synched on '%s:[%s]': internal error?(%v) on ",
				kind, keyval, err,
			)
		}
		return nil
	}, 5*time.Second, 500*time.Millisecond); err != nil {
		return err
	}

	logger.Debugf("[Fixture] %s%s -- %v", ns, key, data)
	if children, ok := data["_children"]; ok {
		for _, v := range children.([]interface{}) {
			if err := loadJsonToDatastore(ctx, key, v.(map[string]interface{}), logger); err != nil {
				return err
			}
		}
	}
	return nil
}
开发者ID:speedland,项目名称:service,代码行数:57,代码来源:fixture.go

示例11: storeContext

func storeContext(ctx context.Context) context.Context {
	ctx, err := appengine.Namespace(ctx, Namespace)
	if err != nil {
		// NOTE: appengine.Namespace will only return an error if
		// the passed namespace name itself is invalid.
		panic(err)
	}
	return ctx
}
开发者ID:flowlo,项目名称:appstats,代码行数:9,代码来源:appstats.go

示例12: Namespace

func (g giImpl) Namespace(namespace string) (context.Context, error) {
	c, err := appengine.Namespace(g, namespace)
	if err != nil {
		return c, err
	}
	pc := *getProbeCache(g)
	pc.namespace = namespace
	return withProbeCache(c, &pc), nil
}
开发者ID:martiniss,项目名称:gae,代码行数:9,代码来源:info.go

示例13: cachedCerts

// cachedCerts fetches public certificates info from DefaultCertURI and
// caches it for the duration specified in Age header of a response.
func cachedCerts(c context.Context) (*certsList, error) {
	namespacedContext, err := appengine.Namespace(c, certNamespace)
	if err != nil {
		return nil, err
	}

	var certs *certsList

	_, err = memcache.JSON.Get(namespacedContext, DefaultCertURI, &certs)
	if err == nil {
		return certs, nil
	}

	// Cache miss or server error.
	// If any error other than cache miss, it's proably not a good time
	// to use memcache.
	var cacheResults = err == memcache.ErrCacheMiss
	if !cacheResults {
		log.Debugf(c, "%s", err.Error())
	}

	log.Debugf(c, "Fetching provider certs from: %s", DefaultCertURI)
	resp, err := newHTTPClient(c).Get(DefaultCertURI)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, errors.New("Could not reach Cert URI or bad response.")
	}

	certBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(certBytes, &certs)
	if err != nil {
		return nil, err
	}

	if cacheResults {
		expiration := certExpirationTime(resp.Header)
		if expiration > 0 {
			item := &memcache.Item{
				Key:        DefaultCertURI,
				Value:      certBytes,
				Expiration: expiration,
			}
			err = memcache.Set(namespacedContext, item)
			if err != nil {
				log.Errorf(c, "Error adding Certs to memcache: %v", err)
			}
		}
	}
	return certs, nil
}
开发者ID:davars,项目名称:go-endpoints,代码行数:58,代码来源:auth.go

示例14: Namespace

func (g giImpl) Namespace(namespace string) (context.Context, error) {
	aeCtx, err := appengine.Namespace(g.aeCtx, namespace)
	if err != nil {
		return g.usrCtx, err
	}
	usrCtx := context.WithValue(g.usrCtx, prodContextKey, aeCtx)
	pc := *getProbeCache(usrCtx)
	pc.namespace = namespace
	return withProbeCache(usrCtx, &pc), nil
}
开发者ID:nishanths,项目名称:gae,代码行数:10,代码来源:info.go

示例15: createInNamespace

// this will generate some random data
// on production this will create 96,768 entitied
func createInNamespace(c context.Context, namespace string) {
	c, _ = appengine.Namespace(c, namespace)

	for m := 1; m <= 12; m++ {
		for d := 1; d <= 28; d++ {
			taken := time.Date(2015, time.Month(m), d, 0, 0, 0, 0, time.UTC)
			generateRandomFn.Call(c, taken)
		}
	}
}
开发者ID:CaptainCodeman,项目名称:datastore-mapper,代码行数:12,代码来源:warmup.go


注:本文中的google/golang.org/appengine.Namespace函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。