go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/mail/context.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mail
    16  
    17  import (
    18  	"context"
    19  )
    20  
    21  type key int
    22  
    23  var (
    24  	serviceKey       key
    25  	serviceFilterKey key = 1
    26  )
    27  
    28  // Factory is the function signature for factory methods compatible with
    29  // SetFactory.
    30  type Factory func(context.Context) RawInterface
    31  
    32  // Filter is the function signature for a filter mail implementation. It
    33  // gets the current mail implementation, and returns a new mail implementation
    34  // backed by the one passed in.
    35  type Filter func(context.Context, RawInterface) RawInterface
    36  
    37  // getUnfiltered gets gets the RawInterface implementation from context without
    38  // any of the filters applied.
    39  func getUnfiltered(c context.Context) RawInterface {
    40  	if f, ok := c.Value(serviceKey).(Factory); ok && f != nil {
    41  		return f(c)
    42  	}
    43  	return nil
    44  }
    45  
    46  func getCurFilters(c context.Context) []Filter {
    47  	curFiltsI := c.Value(serviceFilterKey)
    48  	if curFiltsI != nil {
    49  		return curFiltsI.([]Filter)
    50  	}
    51  	return nil
    52  }
    53  
    54  // Raw pulls the raw mail service implementation from context or nil if it
    55  // wasn't set.
    56  func Raw(c context.Context) RawInterface {
    57  	ret := getUnfiltered(c)
    58  	if ret == nil {
    59  		return nil
    60  	}
    61  	for _, f := range getCurFilters(c) {
    62  		ret = f(c, ret)
    63  	}
    64  	return ret
    65  }
    66  
    67  // SetFactory sets the function to produce mail.RawInterface instances,
    68  // as returned by the Get method.
    69  func SetFactory(c context.Context, f Factory) context.Context {
    70  	return context.WithValue(c, serviceKey, f)
    71  }
    72  
    73  // Set sets the mail service in this context. Useful for testing with a quick
    74  // mock. This is just a shorthand SetFactory invocation to set a factory which
    75  // always returns the same object.
    76  func Set(c context.Context, u RawInterface) context.Context {
    77  	return SetFactory(c, func(context.Context) RawInterface { return u })
    78  }
    79  
    80  // AddFilters adds RawInterface filters to the context.
    81  func AddFilters(c context.Context, filts ...Filter) context.Context {
    82  	if len(filts) == 0 {
    83  		return c
    84  	}
    85  	cur := getCurFilters(c)
    86  	newFilts := make([]Filter, 0, len(cur)+len(filts))
    87  	newFilts = append(newFilts, getCurFilters(c)...)
    88  	newFilts = append(newFilts, filts...)
    89  	return context.WithValue(c, serviceFilterKey, newFilts)
    90  }