Documentation ¶
Overview ¶
Package configcat contains the Go SDK of ConfigCat (https://configcat.com)
Index ¶
- Constants
- type BoolEvaluationDetails
- type BoolFlag
- type Client
- func (client *Client) Close()
- func (client *Client) GetAllKeys() []string
- func (client *Client) GetAllValueDetails(user User) []EvaluationDetails
- func (client *Client) GetAllValues(user User) map[string]interface{}
- func (client *Client) GetBoolValue(key string, defaultValue bool, user User) bool
- func (client *Client) GetBoolValueDetails(key string, defaultValue bool, user User) BoolEvaluationDetails
- func (client *Client) GetFloatValue(key string, defaultValue float64, user User) float64
- func (client *Client) GetFloatValueDetails(key string, defaultValue float64, user User) FloatEvaluationDetails
- func (client *Client) GetIntValue(key string, defaultValue int, user User) int
- func (client *Client) GetIntValueDetails(key string, defaultValue int, user User) IntEvaluationDetails
- func (client *Client) GetKeyValueForVariationID(id string) (string, interface{})
- func (client *Client) GetStringValue(key string, defaultValue string, user User) string
- func (client *Client) GetStringValueDetails(key string, defaultValue string, user User) StringEvaluationDetails
- func (client *Client) GetVariationID(key string, defaultVariationId string, user User) string
- func (client *Client) GetVariationIDs(user User) []string
- func (client *Client) IsOffline() bool
- func (client *Client) Refresh(ctx context.Context) error
- func (client *Client) RefreshIfOlder(ctx context.Context, age time.Duration) error
- func (client *Client) SetOffline()
- func (client *Client) SetOnline()
- func (client *Client) Snapshot(user User) *Snapshot
- type Config
- type ConfigCache
- type DataGovernance
- type ErrKeyNotFound
- type EvaluationDetails
- type EvaluationDetailsData
- type Flag
- type FlagOverrides
- type FloatEvaluationDetails
- type FloatFlag
- type Hooks
- type IntEvaluationDetails
- type IntFlag
- type LogLevel
- type Logger
- type OverrideBehavior
- type PercentageRule
- type PollingMode
- type RolloutRule
- type Snapshot
- func (snap *Snapshot) FetchTime() time.Time
- func (snap *Snapshot) GetAllKeys() []string
- func (snap *Snapshot) GetAllValueDetails() []EvaluationDetails
- func (snap *Snapshot) GetAllValues() map[string]interface{}
- func (snap *Snapshot) GetKeyValueForVariationID(id string) (string, interface{})
- func (snap *Snapshot) GetValue(key string) interface{}
- func (snap *Snapshot) GetValueDetails(key string) EvaluationDetails
- func (snap *Snapshot) GetVariationID(key string) string
- func (snap *Snapshot) GetVariationIDs() []string
- func (snap *Snapshot) WithUser(user User) *Snapshot
- type StringEvaluationDetails
- type StringFlag
- type User
- type UserAttributes
- type UserData
Constants ¶
const ( // LocalOnly means that when evaluating values, the SDK will not use feature flags and settings from the // ConfigCat CDN, but it will use all feature flags and settings that are loaded from local-override sources. LocalOnly = 0 // LocalOverRemote means that when evaluating values, the SDK will use all feature flags and settings that are // downloaded from the ConfigCat CDN, plus all feature flags and settings that are loaded from // local-override sources. If a feature flag or a setting is defined both in the fetched and the local-override // source then the local-override version will take precedence. LocalOverRemote = 1 // RemoteOverLocal means when evaluating values, the SDK will use all feature flags and settings that are // downloaded from the ConfigCat CDN, plus all feature flags and settings that are loaded from local-override // sources. If a feature flag or a setting is defined both in the fetched and the local-override source then the // fetched version will take precedence. RemoteOverLocal = 2 )
const ( LogLevelPanic = logrus.PanicLevel LogLevelFatal = logrus.FatalLevel LogLevelError = logrus.ErrorLevel LogLevelWarn = logrus.WarnLevel LogLevelInfo = logrus.InfoLevel LogLevelDebug = logrus.DebugLevel LogLevelTrace = logrus.TraceLevel )
Define the logrus log levels
const DefaultPollInterval = 60 * time.Second
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoolEvaluationDetails ¶ added in v7.7.0
type BoolEvaluationDetails struct { Data EvaluationDetailsData Value bool }
BoolEvaluationDetails holds the additional evaluation information along with the value of a bool flag.
type BoolFlag ¶
type BoolFlag struct {
// contains filtered or unexported fields
}
func Bool ¶
Bool returns a representation of a boolean-valued flag. This can to be used as the value of a global variable for a specific flag; for example:
var fooFlag = configcat.Bool("foo", false) func someRequest(client *configcat.Client) { if fooFlag.Get(client.Snapshot()) { // foo is enabled. } }
func (BoolFlag) Get ¶
Get reports whether the flag is enabled with respect to the given snapshot. It returns the flag's default value if snap is nil or the key isn't in the configuration.
func (BoolFlag) GetValueDetails ¶ added in v7.7.0
func (f BoolFlag) GetValueDetails(snap *Snapshot) EvaluationDetails
GetValueDetails implements Flag.GetValueDetails.
func (BoolFlag) GetWithDetails ¶ added in v7.7.0
func (f BoolFlag) GetWithDetails(snap *Snapshot) BoolEvaluationDetails
GetWithDetails returns the evaluation details along with the flag's value. It returns BoolEvaluationDetails with the flag's default value if snap is nil or the key isn't in the configuration.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an object for handling configurations provided by ConfigCat.
func NewClient ¶
NewClient returns a new Client value that access the default configcat servers using the given SDK key.
The GetBoolValue, GetIntValue, GetFloatValue and GetStringValue methods can be used to find out current feature flag values. These methods will always return immediately without waiting - if there is no configuration available, they'll return a default value.
func NewCustomClient ¶
NewCustomClient initializes a new ConfigCat Client with advanced configuration.
func (*Client) Close ¶
func (client *Client) Close()
Close shuts down the client. After closing, it shouldn't be used.
func (*Client) GetAllKeys ¶
GetAllKeys returns all the known keys.
func (*Client) GetAllValueDetails ¶ added in v7.7.0
func (client *Client) GetAllValueDetails(user User) []EvaluationDetails
GetAllValueDetails returns values along with evaluation details of all feature flags and settings.
func (*Client) GetAllValues ¶ added in v7.5.0
GetAllValues returns all keys and values in a key-value map.
func (*Client) GetBoolValue ¶
GetBoolValue returns the value of a boolean-typed feature flag, or defaultValue if no value can be found. If user is non-nil, it will be used to choose the value (see the User documentation for details). If user is nil and Config.DefaultUser was non-nil, that will be used instead.
In Lazy refresh mode, this can block indefinitely while the configuration is fetched. Use RefreshIfOlder explicitly if explicit control of timeouts is needed.
func (*Client) GetBoolValueDetails ¶ added in v7.7.0
func (client *Client) GetBoolValueDetails(key string, defaultValue bool, user User) BoolEvaluationDetails
GetBoolValueDetails returns the value and evaluation details of a boolean-typed feature flag. If user is non-nil, it will be used to choose the value (see the User documentation for details). If user is nil and Config.DefaultUser was non-nil, that will be used instead.
In Lazy refresh mode, this can block indefinitely while the configuration is fetched. Use RefreshIfOlder explicitly if explicit control of timeouts is needed.
func (*Client) GetFloatValue ¶
GetFloatValue is like GetBoolValue except for float-typed (decimal number) feature flags.
func (*Client) GetFloatValueDetails ¶ added in v7.7.0
func (client *Client) GetFloatValueDetails(key string, defaultValue float64, user User) FloatEvaluationDetails
GetFloatValueDetails is like GetBoolValueDetails except for float-typed (decimal number) feature flags.
func (*Client) GetIntValue ¶
GetIntValue is like GetBoolValue except for int-typed (whole number) feature flags.
func (*Client) GetIntValueDetails ¶ added in v7.7.0
func (client *Client) GetIntValueDetails(key string, defaultValue int, user User) IntEvaluationDetails
GetIntValueDetails is like GetBoolValueDetails except for int-typed (whole number) feature flags.
func (*Client) GetKeyValueForVariationID ¶
GetKeyValueForVariationID returns the key and value that are associated with the given variation ID. If the variation ID isn't found, it returns "", nil.
func (*Client) GetStringValue ¶
GetStringValue is like GetBoolValue except for string-typed (text) feature flags.
func (*Client) GetStringValueDetails ¶ added in v7.7.0
func (client *Client) GetStringValueDetails(key string, defaultValue string, user User) StringEvaluationDetails
GetStringValueDetails is like GetBoolValueDetails except for string-typed (text) feature flags.
func (*Client) GetVariationID ¶
GetVariationID returns the variation ID (analytics) that will be used for the given key with respect to the given user, or the default value if none is found. Deprecated: This method is obsolete and will be removed in a future major version. Please use GetBoolValueDetails / GetIntValueDetails / GetFloatValueDetails / GetStringValueDetails instead.
func (*Client) GetVariationIDs ¶
GetVariationIDs returns all variation IDs (analytics) in the current configuration that apply to the given user, or Config.DefaultUser if user is nil. Deprecated: This method is obsolete and will be removed in a future major version. Please use GetAllValueDetails instead.
func (*Client) IsOffline ¶ added in v7.7.0
IsOffline returns true when the SDK is configured not to initiate HTTP requests, otherwise false.
func (*Client) Refresh ¶
Refresh refreshes the cached configuration. If the context is canceled while the refresh is in progress, Refresh will return but the underlying HTTP request will not be canceled.
func (*Client) RefreshIfOlder ¶
RefreshIfOlder is like Refresh but refreshes the configuration only if the most recently fetched configuration is older than the given age.
func (*Client) SetOffline ¶ added in v7.8.0
func (client *Client) SetOffline()
SetOffline configures the SDK to not initiate HTTP requests.
type Config ¶
type Config struct { // SDKKey holds the key for the SDK. This parameter // is mandatory. SDKKey string // Logger is used to log information about configuration evaluation // and issues. If it's nil, DefaultLogger(LogLevelWarn) will be used. // It assumes that the logging level will not be increased // during the lifetime of the client. Logger Logger // Cache is used to cache configuration values. // If it's nil, no caching will be done. Cache ConfigCache // BaseURL holds the URL of the ConfigCat CDN server. // If this is empty, an appropriate URL will be chosen // based on the DataGovernance parameter. BaseURL string // Transport is used as the HTTP transport for // requests to the CDN. If it's nil, http.DefaultTransport // will be used. Transport http.RoundTripper // HTTPTimeout holds the timeout for HTTP requests // made by the client. If it's zero, DefaultHTTPTimeout // will be used. If it's negative, no timeout will be // used. HTTPTimeout time.Duration // PollingMode specifies how the configuration is refreshed. // The zero value (default) is AutoPoll. PollingMode PollingMode // NoWaitForRefresh specifies that a Client get method (GetBoolValue, // GetIntValue, GetFloatValue, GetStringValue) should never wait for a // configuration refresh to complete before returning. // // By default, when this is false, if PollingMode is AutoPoll, // the first request may block, and if PollingMode is Lazy, any // request may block. NoWaitForRefresh bool // PollInterval specifies how old a configuration can // be before it's considered stale. If this is less // than 1, DefaultPollInterval is used. // // This parameter is ignored when PollingMode is Manual. PollInterval time.Duration // ChangeNotify is called, if not nil, when the settings configuration // has changed. // Deprecated: Use Hooks instead. ChangeNotify func() // DataGovernance specifies the data governance mode. // Set this parameter to be in sync with the Data Governance // preference on the Dashboard at // https://app.configcat.com/organization/data-governance // (only Organization Admins have access). // The default is Global. DataGovernance DataGovernance // DefaultUser holds the default user information to associate // with the Flagger, used whenever a nil User is passed. // This usually won't contain user-specific // information, but it may be useful when feature flags are dependent // on attributes of the current machine or similar. It's somewhat // more efficient to use DefaultUser=u than to call flagger.Snapshot(u) // on every feature flag evaluation. DefaultUser User // FlagOverrides holds the feature flag and setting overrides. FlagOverrides *FlagOverrides // Hooks controls the events sent by Client. Hooks *Hooks // Offline indicates whether the SDK should be initialized in offline mode or not. Offline bool }
Config describes configuration options for the Client.
type ConfigCache ¶
type ConfigCache interface { // Get reads the configuration from the cache. Get(ctx context.Context, key string) ([]byte, error) // Set writes the configuration into the cache. Set(ctx context.Context, key string, value []byte) error }
ConfigCache is a cache API used to make custom cache implementations.
type DataGovernance ¶
type DataGovernance int
DataGovernance describes the location of your feature flag and setting data within the ConfigCat CDN.
const ( // Global Select this if your feature flags are published to all global CDN nodes. Global DataGovernance = 0 // EUOnly Select this if your feature flags are published to CDN nodes only in the EU. EUOnly DataGovernance = 1 )
type ErrKeyNotFound ¶ added in v7.10.1
ErrKeyNotFound is returned when a key is not found in the configuration.
func (ErrKeyNotFound) Error ¶ added in v7.10.1
func (e ErrKeyNotFound) Error() string
type EvaluationDetails ¶ added in v7.7.0
type EvaluationDetails struct { Data EvaluationDetailsData Value interface{} }
EvaluationDetails holds the additional evaluation information along with the value of a feature flag or setting.
type EvaluationDetailsData ¶ added in v7.7.0
type EvaluationDetailsData struct { Key string VariationID string User User IsDefaultValue bool Error error FetchTime time.Time MatchedEvaluationRule *RolloutRule MatchedEvaluationPercentageRule *PercentageRule }
EvaluationDetailsData holds the additional evaluation information of a feature flag or setting.
type Flag ¶ added in v7.3.0
type Flag interface { // Key returns the flag's key. Key() string // GetValue returns the flag's value. It will always // return the appropriate type for the flag (never nil). GetValue(snap *Snapshot) interface{} // GetValueDetails returns the evaluation details // along with the flag's value. Its Value field always // have the appropriate type for the flag. GetValueDetails(snap *Snapshot) EvaluationDetails }
Flag is the interface implemented by all flag types.
type FlagOverrides ¶ added in v7.5.0
type FlagOverrides struct { // Behavior describes how the overrides should behave. Default is LocalOnly. Behavior OverrideBehavior // Values is a map that contains the overrides. // Each value must be one of the following types: bool, int, float64, or string. Values map[string]interface{} // FilePath is the path to a JSON file that contains the overrides. // The supported JSON file formats are documented here: https://configcat.com/docs/sdk-reference/go/#json-file-structure FilePath string // contains filtered or unexported fields }
FlagOverrides describes feature flag and setting overrides. With flag overrides you can overwrite the feature flags downloaded from the ConfigCat CDN with local values.
With Values, you can set up the SDK to load your feature flag overrides from a map.
With FilePath, you can set up the SDK to load your feature flag overrides from a JSON file.
type FloatEvaluationDetails ¶ added in v7.7.0
type FloatEvaluationDetails struct { Data EvaluationDetailsData Value float64 }
FloatEvaluationDetails holds the additional evaluation information along with the value of a decimal number flag.
type FloatFlag ¶
type FloatFlag struct {
// contains filtered or unexported fields
}
func (FloatFlag) Get ¶
Get reports the value of the flag with respect to the given snapshot. It returns the flag's default value if snap is nil or the key isn't in the configuration.
func (FloatFlag) GetValueDetails ¶ added in v7.7.0
func (f FloatFlag) GetValueDetails(snap *Snapshot) EvaluationDetails
GetValueDetails implements Flag.GetValueDetails.
func (FloatFlag) GetWithDetails ¶ added in v7.7.0
func (f FloatFlag) GetWithDetails(snap *Snapshot) FloatEvaluationDetails
GetWithDetails returns the evaluation details along with the flag's value. It returns FloatEvaluationDetails with the flag's default value if snap is nil or the key isn't in the configuration.
type Hooks ¶ added in v7.7.0
type Hooks struct { // OnFlagEvaluated is called each time when the SDK evaluates a feature flag or setting. OnFlagEvaluated func(details *EvaluationDetails) // OnError is called when an error occurs inside the ConfigCat SDK. OnError func(err error) // OnConfigChanged is called, when a new config.json has downloaded. OnConfigChanged func() }
Hooks describes the events sent by Client.
type IntEvaluationDetails ¶ added in v7.7.0
type IntEvaluationDetails struct { Data EvaluationDetailsData Value int }
IntEvaluationDetails holds the additional evaluation information along with the value of a whole number flag.
type IntFlag ¶
type IntFlag struct {
// contains filtered or unexported fields
}
func (IntFlag) Get ¶
Get reports the value of the flag with respect to the given snapshot. It returns the flag's default value if snap is nil or the key isn't in the configuration.
func (IntFlag) GetValueDetails ¶ added in v7.7.0
func (f IntFlag) GetValueDetails(snap *Snapshot) EvaluationDetails
GetValueDetails implements Flag.GetValueDetails.
func (IntFlag) GetWithDetails ¶ added in v7.7.0
func (f IntFlag) GetWithDetails(snap *Snapshot) IntEvaluationDetails
GetWithDetails returns the evaluation details along with the flag's value. It returns IntEvaluationDetails with the flag's default value if snap is nil or the key isn't in the configuration.
type Logger ¶
type Logger interface { // GetLevel returns the current logging level. GetLevel() LogLevel Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warnf(format string, args ...interface{}) Errorf(format string, args ...interface{}) }
Logger defines the interface this library logs with.
func DefaultLogger ¶
DefaultLogger creates the default logger with specified log level (logrus.New()).
type OverrideBehavior ¶ added in v7.5.0
type OverrideBehavior int
OverrideBehavior describes how the overrides should behave.
type PercentageRule ¶ added in v7.7.0
type PollingMode ¶
type PollingMode int
PollingMode specifies a strategy for refreshing the configuration.
const ( // AutoPoll causes the client to refresh the configuration // automatically at least as often as the Config.PollInterval // parameter. AutoPoll PollingMode = iota // Manual will only refresh the configuration when Refresh // is called explicitly, falling back to the cache for the initial // value or if the refresh fails. Manual // Lazy will refresh the configuration whenever a value // is retrieved and the configuration is older than // Config.PollInterval. Lazy )
type RolloutRule ¶ added in v7.7.0
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot holds a snapshot of the ConfigCat configuration. A snapshot is immutable once taken.
A nil snapshot is OK to use and acts like a configuration with no keys.
func NewSnapshot ¶ added in v7.1.0
NewSnapshot returns a snapshot that always returns the given values.
Each entry in the values map is keyed by a flag name and holds the value that the snapshot will return for that flag. Each value must be one of the types bool, int, float64, or string.
The returned snapshot does not support variation IDs. That is, given a snapshot s returned by NewSnapshot: - s.GetKeyValueForVariationID returns "", nil. - s.GetVariationID returns "". - s.GetVariationIDs returns nil.
func (*Snapshot) GetAllKeys ¶
GetAllKeys returns all the known keys in arbitrary order.
func (*Snapshot) GetAllValueDetails ¶ added in v7.7.0
func (snap *Snapshot) GetAllValueDetails() []EvaluationDetails
GetAllValueDetails returns values along with evaluation details of all feature flags and settings.
func (*Snapshot) GetAllValues ¶ added in v7.5.0
GetAllValues returns all keys and values in freshly allocated key-value map.
func (*Snapshot) GetKeyValueForVariationID ¶
GetKeyValueForVariationID returns the key and value that are associated with the given variation ID. If the variation ID isn't found, it returns "", nil.
func (*Snapshot) GetValue ¶
GetValue returns a feature flag value regardless of type. If there is no value found, it returns nil; otherwise the returned value has one of the dynamic types bool, int, float64, or string.
To use obtain the value of a typed feature flag, use one of the typed feature flag functions. For example:
someFlag := configcat.Bool("someFlag", false) value := someFlag.Get(snap)
func (*Snapshot) GetValueDetails ¶ added in v7.7.0
func (snap *Snapshot) GetValueDetails(key string) EvaluationDetails
GetValueDetails returns the value and evaluation details of a feature flag or setting with respect to the current user, or nil if none is found.
func (*Snapshot) GetVariationID ¶
GetVariationID returns the variation ID that will be used for the given key with respect to the current user, or the empty string if none is found. Deprecated: This method is obsolete and will be removed in a future major version. Please use GetValueDetails instead.
func (*Snapshot) GetVariationIDs ¶
GetVariationIDs returns all variation IDs in the current configuration that apply to the current user. Deprecated: This method is obsolete and will be removed in a future major version. Please use GetAllValueDetails instead.
type StringEvaluationDetails ¶ added in v7.7.0
type StringEvaluationDetails struct { Data EvaluationDetailsData Value string }
StringEvaluationDetails holds the additional evaluation information along with the value of a string flag.
type StringFlag ¶
type StringFlag struct {
// contains filtered or unexported fields
}
func String ¶
func String(key string, defaultValue string) StringFlag
String is like Bool but for string-valued flags.
func (StringFlag) Get ¶
func (f StringFlag) Get(snap *Snapshot) string
Get reports the value of the flag with respect to the given snapshot. It returns the flag's default value if snap is nil or the key isn't in the configuration.
func (StringFlag) GetValue ¶ added in v7.3.0
func (f StringFlag) GetValue(snap *Snapshot) interface{}
GetValue implements Flag.GetValue.
func (StringFlag) GetValueDetails ¶ added in v7.7.0
func (f StringFlag) GetValueDetails(snap *Snapshot) EvaluationDetails
GetValueDetails implements Flag.GetValueDetails.
func (StringFlag) GetWithDetails ¶ added in v7.7.0
func (f StringFlag) GetWithDetails(snap *Snapshot) StringEvaluationDetails
GetWithDetails returns the evaluation details along with the flag's value. It returns StringEvaluationDetails with the flag's default value if snap is nil or the key isn't in the configuration.
func (StringFlag) Key ¶ added in v7.2.0
func (f StringFlag) Key() string
Key returns the name of the flag as passed to String.
type User ¶
type User interface{}
The User interface represents the user-specific data that can influence configcat rule evaluation. All Users are expected to provide an Identifier attribute. A User value is assumed to be immutable once it's been provided to configcat - if it changes between feature flag evaluations, there's no guarantee that the feature flag results will change accordingly (use WithUser instead of mutating the value).
The configcat client uses reflection to determine what attributes are available:
If the User value implements UserAttributes, then that method will be used to retrieve attributes.
Otherwise the implementation is expected to be a pointer to a struct type. Each public field in the struct is treated as a possible comparison attribute.
If a field's type implements a `String() string` method, the field will be treated as a textual and the String method will be called to determine the value.
If a field's type is map[string] string, the map value is used to look up any custom attribute not found directly in the struct. There should be at most one of these fields.
Otherwise, a field type must be a numeric type, a string type, a []byte type or a github.com/blang/semver.Version.
If a rule uses an attribute that isn't available, that rule will be treated as non-matching.
type UserAttributes ¶
UserAttributes can be implemented by a User value to implement support for getting arbitrary attributes.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package configcattest provides an HTTP handler that can be used to test configcat scenarios in tests.
|
Package configcattest provides an HTTP handler that can be used to test configcat scenarios in tests. |
internal
|
|
wireconfig
Package wireconfig holds types and constants that define the representation of the ConfigCat configuration as transmitted over the wire from the ConfigCat CDN.
|
Package wireconfig holds types and constants that define the representation of the ConfigCat configuration as transmitted over the wire from the ConfigCat CDN. |