go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/settings/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 settings
    16  
    17  import "context"
    18  
    19  type contextKey int
    20  
    21  // Use injects Settings into the context to be used by Get and Set.
    22  func Use(c context.Context, s *Settings) context.Context {
    23  	return context.WithValue(c, contextKey(0), s)
    24  }
    25  
    26  // GetSettings grabs Settings from the context if it's there.
    27  func GetSettings(c context.Context) *Settings {
    28  	if s, ok := c.Value(contextKey(0)).(*Settings); ok && s != nil {
    29  		return s
    30  	}
    31  	return nil
    32  }
    33  
    34  // Get returns setting value (possibly cached) for the given key.
    35  //
    36  // It will be deserialized into the supplied value. Caller is responsible to
    37  // pass correct type here. If the setting is not set or the context doesn't have
    38  // settings implementation in it, returns ErrNoSettings.
    39  func Get(c context.Context, key string, value any) error {
    40  	if s := GetSettings(c); s != nil {
    41  		return s.Get(c, key, value)
    42  	}
    43  	return ErrNoSettings
    44  }
    45  
    46  // GetUncached is like Get, by always fetches settings from the storage.
    47  //
    48  // Do not use GetUncached in performance critical parts, it is much heavier than
    49  // Get.
    50  func GetUncached(c context.Context, key string, value any) error {
    51  	if s := GetSettings(c); s != nil {
    52  		return s.GetUncached(c, key, value)
    53  	}
    54  	return ErrNoSettings
    55  }
    56  
    57  // Set overwrites a setting value for the given key.
    58  //
    59  // New settings will apply only when existing in-memory cache expires.
    60  // In particular, Get() right after Set() may still return old value.
    61  //
    62  // Returns ErrNoSettings if context doesn't have Settings implementation.
    63  func Set(c context.Context, key string, value any) error {
    64  	if s := GetSettings(c); s != nil {
    65  		return s.Set(c, key, value)
    66  	}
    67  	return ErrNoSettings
    68  }
    69  
    70  // SetIfChanged is like Set, but fetches an existing value and compares it to
    71  // a new one before changing it.
    72  //
    73  // Avoids generating new revisions of settings if no changes are actually
    74  // made. Also logs who is making the change.
    75  //
    76  // Returns ErrNoSettings if context doesn't have Settings implementation.
    77  func SetIfChanged(c context.Context, key string, value any) error {
    78  	if s := GetSettings(c); s != nil {
    79  		return s.SetIfChanged(c, key, value)
    80  	}
    81  	return ErrNoSettings
    82  }