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

     1  // Copyright 2016 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 module
    16  
    17  import (
    18  	"context"
    19  )
    20  
    21  type key int
    22  
    23  var (
    24  	moduleKey       key
    25  	moduleFilterKey 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 module implementation. It gets
    33  // the current module implementation, and returns a new module 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(moduleKey).(Factory); ok && f != nil {
    41  		return f(c)
    42  	}
    43  	return nil
    44  }
    45  
    46  // Raw gets the RawInterface implementation from context.
    47  func Raw(c context.Context) RawInterface {
    48  	ret := getUnfiltered(c)
    49  	if ret == nil {
    50  		return nil
    51  	}
    52  	for _, f := range getCurFilters(c) {
    53  		ret = f(c, ret)
    54  	}
    55  	return ret
    56  }
    57  
    58  // SetFactory sets the function to produce RawInterface instances, as returned
    59  // by the Get method.
    60  func SetFactory(c context.Context, gif Factory) context.Context {
    61  	return context.WithValue(c, moduleKey, gif)
    62  }
    63  
    64  // Set sets the current RawInterface object in the context. Useful for testing
    65  // with a quick mock. This is just a shorthand SetFactory invocation to set
    66  // a factory which always returns the same object.
    67  func Set(c context.Context, gi RawInterface) context.Context {
    68  	return SetFactory(c, func(context.Context) RawInterface { return gi })
    69  }
    70  
    71  func getCurFilters(c context.Context) []Filter {
    72  	curFiltsI := c.Value(moduleFilterKey)
    73  	if curFiltsI != nil {
    74  		return curFiltsI.([]Filter)
    75  	}
    76  	return nil
    77  }
    78  
    79  // AddFilters adds RawInterface filters to the context.
    80  func AddFilters(c context.Context, filts ...Filter) context.Context {
    81  	if len(filts) == 0 {
    82  		return c
    83  	}
    84  	cur := getCurFilters(c)
    85  	newFilts := make([]Filter, 0, len(cur)+len(filts))
    86  	newFilts = append(newFilts, getCurFilters(c)...)
    87  	newFilts = append(newFilts, filts...)
    88  	return context.WithValue(c, moduleFilterKey, newFilts)
    89  }