package domain

import (
	"context"
	"errors"
)

// Define our custom key type
type key int

// declare our key value
const userKey key = 0

type User struct {
	ID   int
	Name string
}

func FromContext(ctx context.Context) (*User, error) {
	u, ok := ctx.Value(userKey).(*User)
	if !ok {
		return nil, errors.New("user not found")
	}

	return u, nil
}

func WithContext(ctx context.Context, u *User) context.Context {
	return context.WithValue(ctx, userKey, u)
}
