Skip to main content

uuid.go

uuid.go - Overview

  1. Overview

This file defines types and functions for managing UUIDs within a context, primarily for authorization purposes. It includes functionality to extract a UUID from a request (represented as a string), store it in a context, and retrieve it from a context.

  1. Detailed Documentation

type uuidKey struct{}

  • Purpose: This is an empty struct used as a key for storing the UUID in the context. Using an unexported type prevents key collisions.

type UUID struct {}

  • Purpose: Represents a UUID. Currently, it's an empty struct, likely intended to hold UUID-related methods.

func NewUUID() *UUID

  • Purpose: Creates a new UUID object.
  • Parameters: None
  • Returns: *UUID - A pointer to a newly created UUID struct.

func (u *UUID) ContextFromRequest(ctx context.Context, values ...string) (context.Context, error)

  • Purpose: Extracts a UUID from the provided string values and adds it to the context. It iterates through the values, using the first non-empty string as the UUID.
  • Parameters:
    • ctx context.Context: The context to which the UUID will be added.
    • values ...string: A variadic list of strings, potentially containing the UUID.
  • Returns:
    • context.Context: A new context with the UUID stored within it, or the original context if no valid UUID is found.
    • error: An error if no non-empty string is found in the values slice, indicating a missing authorization header.

func NewContextWithUUID(ctx context.Context, uuid string) context.Context

  • Purpose: Creates a new context with the given UUID stored under the uuidKey.
  • Parameters:
    • ctx context.Context: The parent context.
    • uuid string: The UUID to store in the context.
  • Returns: context.Context - A new context containing the UUID.

func UUIDFromContext(ctx context.Context) (string, bool)

  • Purpose: Retrieves the UUID from the context.
  • Parameters:
    • ctx context.Context: The context from which to retrieve the UUID.
  • Returns:
    • string: The UUID, if found in the context.
    • bool: True if the UUID was found, false otherwise.
  1. Code Examples

None.

Include in Getting Started: NO