go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/migration/context.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package migration
     9  
    10  import "context"
    11  
    12  type suiteKey struct{}
    13  
    14  // WithSuite adds a suite as a value to a context.
    15  func WithSuite(ctx context.Context, suite *Suite) context.Context {
    16  	return context.WithValue(ctx, suiteKey{}, suite)
    17  }
    18  
    19  // GetContextSuite gets a suite from a context as a value.
    20  func GetContextSuite(ctx context.Context) *Suite {
    21  	value := ctx.Value(suiteKey{})
    22  	if typed, ok := value.(*Suite); ok {
    23  		return typed
    24  	}
    25  	return nil
    26  }
    27  
    28  type labelsKey struct{}
    29  
    30  // WithLabel adds a label to the context
    31  func WithLabel(ctx context.Context, label string) context.Context {
    32  	return context.WithValue(ctx, labelsKey{}, append(GetContextLabels(ctx), label))
    33  }
    34  
    35  // GetContextLabels gets a group from a context as a value.
    36  func GetContextLabels(ctx context.Context) []string {
    37  	value := ctx.Value(labelsKey{})
    38  	if typed, ok := value.([]string); ok {
    39  		return typed
    40  	}
    41  	return nil
    42  }