namespace.go 761 B

12345678910111213141516171819202122232425
  1. // Copyright 2012 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package appengine
  5. import (
  6. "fmt"
  7. "regexp"
  8. "golang.org/x/net/context"
  9. "google.golang.org/appengine/internal"
  10. )
  11. // Namespace returns a replacement context that operates within the given namespace.
  12. func Namespace(c context.Context, namespace string) (context.Context, error) {
  13. if !validNamespace.MatchString(namespace) {
  14. return nil, fmt.Errorf("appengine: namespace %q does not match /%s/", namespace, validNamespace)
  15. }
  16. return internal.NamespacedContext(c, namespace), nil
  17. }
  18. // validNamespace matches valid namespace names.
  19. var validNamespace = regexp.MustCompile(`^[0-9A-Za-z._-]{0,100}$`)