github.com/m3db/m3@v1.5.0/src/cluster/kv/util/types.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package util
    22  
    23  import (
    24  	"github.com/m3db/m3/src/cluster/kv"
    25  
    26  	"go.uber.org/zap"
    27  )
    28  
    29  // ValidateFn validates an update from KV.
    30  type ValidateFn func(interface{}) error
    31  
    32  type getValueFn func(kv.Value) (interface{}, error)
    33  
    34  type updateFn func(interface{})
    35  
    36  // GenericGetValueFn runs when a watched value changes to translate the KV
    37  // value to a concrete type.
    38  type GenericGetValueFn getValueFn
    39  
    40  // GenericUpdateFn runs when a watched value changes and receives the output of
    41  // the GenericGetValueFn.
    42  type GenericUpdateFn updateFn
    43  
    44  // Options is a set of options for kv utility functions.
    45  type Options interface {
    46  	// SetValidateFn sets the validation function applied to kv values.
    47  	SetValidateFn(val ValidateFn) Options
    48  
    49  	// ValidateFn returns the validation function applied to kv values.
    50  	ValidateFn() ValidateFn
    51  
    52  	// SetLogger sets the logger.
    53  	SetLogger(val *zap.Logger) Options
    54  
    55  	// Logger returns the logger.
    56  	Logger() *zap.Logger
    57  }
    58  
    59  type options struct {
    60  	validateFn ValidateFn
    61  	logger     *zap.Logger
    62  }
    63  
    64  // NewOptions returns a new set of options for kv utility functions.
    65  func NewOptions() Options {
    66  	return &options{}
    67  }
    68  
    69  func (o *options) SetValidateFn(val ValidateFn) Options {
    70  	opts := *o
    71  	opts.validateFn = val
    72  	return &opts
    73  }
    74  
    75  func (o *options) ValidateFn() ValidateFn {
    76  	return o.validateFn
    77  }
    78  
    79  func (o *options) SetLogger(val *zap.Logger) Options {
    80  	opts := *o
    81  	opts.logger = val
    82  	return &opts
    83  }
    84  
    85  func (o *options) Logger() *zap.Logger {
    86  	return o.logger
    87  }