...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldcontext/kind.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldcontext

     1  package ldcontext
     2  
     3  import (
     4  	"github.com/launchdarkly/go-sdk-common/v3/ldattr"
     5  	"github.com/launchdarkly/go-sdk-common/v3/lderrors"
     6  )
     7  
     8  // Kind is a string type set by the application to describe what kind of entity a Context
     9  // represents. The meaning of this is completely up to the application. When no Kind is
    10  // specified, the default is [DefaultKind].
    11  //
    12  // For a multi-context (see [NewMultiBuilder]), the [Context.Kind] is always [MultiKind];
    13  // there is a specific Kind for each of the individual Contexts within it.
    14  type Kind string
    15  
    16  const (
    17  	// DefaultKind is a constant for the default Kind of "user".
    18  	DefaultKind Kind = "user"
    19  
    20  	// MultiKind is a constant for the Kind that all multi-contexts have.
    21  	MultiKind Kind = "multi"
    22  )
    23  
    24  // Used internally to enforce validation and defaulting logic. Per the users-to-contexts spec,
    25  // valid characters in "kind" are ASCII alphanumerics, period, hyphen, and underscore, it
    26  // cannot be the string "kind", and in a single context it cannot be the string "multi".
    27  func validateSingleKind(kind Kind) (Kind, error) {
    28  	switch kind {
    29  	case "":
    30  		return DefaultKind, nil
    31  
    32  	case MultiKind:
    33  		return "", lderrors.ErrContextKindMultiForSingleKind{}
    34  
    35  	case Kind(ldattr.KindAttr):
    36  		return "", lderrors.ErrContextKindCannotBeKind{}
    37  
    38  	default:
    39  		for _, ch := range kind {
    40  			if (ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch < '0' || ch > '9') &&
    41  				ch != '.' && ch != '_' && ch != '-' {
    42  				return "", lderrors.ErrContextKindInvalidChars{}
    43  			}
    44  		}
    45  		return kind, nil
    46  	}
    47  }
    48  

View as plain text