Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ type context struct {

var contextKey = "holds a *context"

// fromContext returns the App Engine context or nil if ctx is not
// derived from an App Engine context.
func fromContext(ctx netcontext.Context) *context {
c, _ := ctx.Value(&contextKey).(*context)
return c
Expand Down Expand Up @@ -468,7 +470,7 @@ func Call(ctx netcontext.Context, service, method string, in, out proto.Message)
c := fromContext(ctx)
if c == nil {
// Give a good error message rather than a panic lower down.
return errors.New("not an App Engine context")
return errNotAppEngineContext
}

// Apply transaction modifications if we're in a transaction.
Expand Down
12 changes: 9 additions & 3 deletions internal/api_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ import (

var contextKey = "holds an appengine.Context"

// fromContext returns the App Engine context or nil if ctx is not
// derived from an App Engine context.
func fromContext(ctx netcontext.Context) appengine.Context {
c, _ := ctx.Value(&contextKey).(appengine.Context)
return c
}

// This is only for classic App Engine adapters.
func ClassicContextFromContext(ctx netcontext.Context) appengine.Context {
return fromContext(ctx)
func ClassicContextFromContext(ctx netcontext.Context) (appengine.Context, error) {
c := fromContext(ctx)
if c == nil {
return nil, errNotAppEngineContext
}
return c, nil
}

func withContext(parent netcontext.Context, c appengine.Context) netcontext.Context {
Expand Down Expand Up @@ -98,7 +104,7 @@ func Call(ctx netcontext.Context, service, method string, in, out proto.Message)
c := fromContext(ctx)
if c == nil {
// Give a good error message rather than a panic lower down.
return errors.New("not an App Engine context")
return errNotAppEngineContext
}

// Apply transaction modifications if we're in a transaction.
Expand Down
9 changes: 8 additions & 1 deletion internal/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
package internal

import (
"errors"
"os"

"github.com/golang/protobuf/proto"
netcontext "golang.org/x/net/context"
)

var errNotAppEngineContext = errors.New("not an App Engine context")

type CallOverrideFunc func(ctx netcontext.Context, service, method string, in, out proto.Message) error

var callOverrideKey = "holds []CallOverrideFunc"
Expand Down Expand Up @@ -79,7 +82,11 @@ func Logf(ctx netcontext.Context, level int64, format string, args ...interface{
f(level, format, args...)
return
}
logf(fromContext(ctx), level, format, args...)
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
logf(c, level, format, args...)
}

// NamespacedContext wraps a Context to support namespaces.
Expand Down
48 changes: 39 additions & 9 deletions internal/identity_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,45 @@ import (
)

func DefaultVersionHostname(ctx netcontext.Context) string {
return appengine.DefaultVersionHostname(fromContext(ctx))
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
return appengine.DefaultVersionHostname(c)
}

func RequestID(ctx netcontext.Context) string { return appengine.RequestID(fromContext(ctx)) }
func Datacenter(_ netcontext.Context) string { return appengine.Datacenter() }
func ServerSoftware() string { return appengine.ServerSoftware() }
func ModuleName(ctx netcontext.Context) string { return appengine.ModuleName(fromContext(ctx)) }
func VersionID(ctx netcontext.Context) string { return appengine.VersionID(fromContext(ctx)) }
func InstanceID() string { return appengine.InstanceID() }
func IsDevAppServer() bool { return appengine.IsDevAppServer() }
func Datacenter(_ netcontext.Context) string { return appengine.Datacenter() }
func ServerSoftware() string { return appengine.ServerSoftware() }
func InstanceID() string { return appengine.InstanceID() }
func IsDevAppServer() bool { return appengine.IsDevAppServer() }

func fullyQualifiedAppID(ctx netcontext.Context) string { return fromContext(ctx).FullyQualifiedAppID() }
func RequestID(ctx netcontext.Context) string {
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
return appengine.RequestID(c)
}

func ModuleName(ctx netcontext.Context) string {
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
return appengine.ModuleName(c)
}
func VersionID(ctx netcontext.Context) string {
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
return appengine.VersionID(c)
}

func fullyQualifiedAppID(ctx netcontext.Context) string {
c := fromContext(ctx)
if c == nil {
panic(errNotAppEngineContext)
}
return c.FullyQualifiedAppID()
}
6 changes: 5 additions & 1 deletion internal/identity_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const (
)

func ctxHeaders(ctx netcontext.Context) http.Header {
return fromContext(ctx).Request().Header
c := fromContext(ctx)
if c == nil {
return nil
}
return c.Request().Header
}

func DefaultVersionHostname(ctx netcontext.Context) string {
Expand Down
6 changes: 5 additions & 1 deletion user/user_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import (
)

func Current(ctx context.Context) *User {
u := user.Current(internal.ClassicContextFromContext(ctx))
c, err := internal.ClassicContextFromContext(ctx)
if err != nil {
panic(err)
}
u := user.Current(c)
if u == nil {
return nil
}
Expand Down