Change return value of xxxIDFromContext()

This commit is contained in:
Jeff (Zhefu) Jiang 2015-07-17 15:02:09 -07:00
Родитель 4c78120a23
Коммит b71335b3f1
2 изменённых файлов: 19 добавлений и 26 удалений

Просмотреть файл

@ -95,19 +95,19 @@ func NewContext(ctx context.Context, ef *EffectiveCallerID, im *ImmediateCallerI
}
// EffectiveCallerIDFromContext returns the EffectiveCallerID stored in the Context, if any
func EffectiveCallerIDFromContext(ctx context.Context) (*EffectiveCallerID, bool) {
func EffectiveCallerIDFromContext(ctx context.Context) *EffectiveCallerID {
ef, ok := ctx.Value(effectiveCallerIDKey).(*EffectiveCallerID)
if ef != nil {
return ef, ok
if ok && ef != nil {
return ef
}
return nil, false
return nil
}
// ImmediateCallerIDFromContext returns the ImmediateCallerID stored in the Context, if any
func ImmediateCallerIDFromContext(ctx context.Context) (*ImmediateCallerID, bool) {
func ImmediateCallerIDFromContext(ctx context.Context) *ImmediateCallerID {
im, ok := ctx.Value(immediateCallerIDKey).(*ImmediateCallerID)
if im != nil {
return im, ok
if ok && im != nil {
return im
}
return nil, false
return nil
}

Просмотреть файл

@ -21,38 +21,31 @@ const (
// Tests performs the necessary testsuite for CallerID operations
func Tests(t *testing.T, im *ImmediateCallerID, ef *EffectiveCallerID) {
ctx := context.TODO()
ctxim, ok := ImmediateCallerIDFromContext(ctx)
ctxim := ImmediateCallerIDFromContext(ctx)
// For Contexts without ImmediateCallerID, ImmediateCallerIDFromContext should fail
if ctxim != nil || ok {
t.Errorf("Expect <nil, false> from ImmediateCallerIDFromContext, but got <%v, %v>", ctxim, ok)
if ctxim != nil {
t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
}
// For Contexts without EffectiveCallerID, EffectiveCallerIDFromContext should fail
ctxef, ok := EffectiveCallerIDFromContext(ctx)
if ctxef != nil || ok {
t.Errorf("Expect <nil, false> from EffectiveCallerIDFromContext, but got <%v, %v>", ctxef, ok)
ctxef := EffectiveCallerIDFromContext(ctx)
if ctxef != nil {
t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
}
ctx = NewContext(ctx, ef, im)
ctxim, ok = ImmediateCallerIDFromContext(ctx)
// If im == nil, ImmediateCallerIDFromContext should fail
if im == nil && (ctxim != nil || ok) {
t.Errorf("Expect <nil, false> from ImmediateCallerIDFromContext, but got <%v, %v>", ctxim, ok)
}
ctxim = ImmediateCallerIDFromContext(ctx)
// retrieved ImmediateCallerID should be equal to the one we put into Context
if !reflect.DeepEqual(ctxim, im) {
t.Errorf("Expect <%v, ok> from ImmediateCallerIDFromContext, but got <%v, %v>", im, ctxim, ok)
t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
}
if im.GetUsername() != FakeUsername {
t.Errorf("Expect %v from im.Username(), but got %v", FakeUsername, im.GetUsername())
}
ctxef, ok = EffectiveCallerIDFromContext(ctx)
// If ef == nil, EffectiveCallerIDFromContext should fail
if ef == nil && (ctxef != nil || ok) {
t.Errorf("Expect <nil, false> from EffectiveCallerIDFromContext, but got <%v, %v>", ctxef, ok)
}
ctxef = EffectiveCallerIDFromContext(ctx)
// retrieved EffectiveCallerID should be equal to the one we put into Context
if !reflect.DeepEqual(ctxef, ef) {
t.Errorf("Expect <%v, ok> from EffectiveCallerIDFromContext, but got <%v, %v>", ef, ctxef, ok)
t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
}
if ef.GetPrincipal() != FakePrincipal {
t.Errorf("Expect %v from ef.Principal(), but got %v", FakePrincipal, ef.GetPrincipal())