operation

package
v1.0.0-beta.85 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package operation provides an abstraction for RPC-style APIs.

Implementations are generally business logic functions that take a request and return a response. Consumers of operations are transport layers, such as HTTP handlers or gRPC services.

The operation layer allows you to separate business logic from transport concerns as well as to compose and reuse transport and operation-agnostic logic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Name

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

Name returns the name of the operation from the context (if any).

func NewLogHandler

func NewLogHandler(handler slog.Handler) slog.Handler

NewLogHandler returns a new slog.Handler

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func (Handler) Enabled

func (h Handler) Enabled(ctx context.Context, level slog.Level) bool

func (Handler) Handle

func (h Handler) Handle(ctx context.Context, record slog.Record) error

func (Handler) WithAttrs

func (h Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (Handler) WithGroup

func (h Handler) WithGroup(name string) slog.Handler

type Middleware

type Middleware[Request any, Response any] func(Operation[Request, Response]) Operation[Request, Response]

Middleware is a chainable function that wraps an Operation.

Example
package main

import (
	"context"

	"github.com/openmeterio/openmeter/pkg/framework/operation"
)

func mw1[Request any, Response any](next operation.Operation[Request, Response]) operation.Operation[Request, Response] {
	return func(ctx context.Context, request Request) (Response, error) {
		return next(ctx, request)
	}
}

func mwchain[Request any, Response any](op operation.Operation[Request, Response]) operation.Operation[Request, Response] {
	chain := operation.Chain[Request, Response](mw1)

	return chain(op)
}

func main() {
	mwchain(exampleOperation)
}
Output:

func Chain

func Chain[Request any, Response any](outer Middleware[Request, Response], others ...Middleware[Request, Response]) Middleware[Request, Response]

Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.

type Operation

type Operation[Request any, Response any] func(ctx context.Context, request Request) (Response, error)

Operation is the fundamental building block of RPC-style APIs. It represents a single operation that can be performed by a caller.

Example
package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/openmeterio/openmeter/pkg/framework/operation"
)

type ExampleRequest struct {
	Name string
}

type ExampleResponse struct {
	Greeting string
}

func exampleOperation(ctx context.Context, request ExampleRequest) (ExampleResponse, error) {
	if request.Name == "" {
		return ExampleResponse{}, errors.New("name is required")
	}

	return ExampleResponse{Greeting: "Hello, " + request.Name}, nil
}

func main() {
	var op operation.Operation[ExampleRequest, ExampleResponse] = exampleOperation

	resp, err := op(context.Background(), ExampleRequest{Name: "World"})
	if err != nil {
		panic(err)
	}

	fmt.Print(resp.Greeting)
}

// func ExampleOperation() {
// 	var op operation.Operation[ExampleRequest, ExampleResponse] = operationImpl{}
//
// 	resp, err := op.Do(context.Background(), ExampleRequest{Name: "World"})
// 	if err != nil {
// 		panic(err)
// 	}
//
// 	fmt.Print(resp.Greeting)
// 	// Output: Hello, World
// }

// func ExampleOperationFunc() {
// 	var op operation.Operation[ExampleRequest, ExampleResponse] = operation.OperationFunc[ExampleRequest, ExampleResponse](exampleOperation)
//
// 	resp, err := op.Do(context.Background(), ExampleRequest{Name: "World"})
// 	if err != nil {
// 		panic(err)
// 	}
//
// 	fmt.Print(resp.Greeting)
// 	// Output: Hello, World
// }
//
// func ExampleNew() {
// 	op := operation.New(exampleOperation)
//
// 	resp, err := op.Do(context.Background(), ExampleRequest{Name: "World"})
// 	if err != nil {
// 		panic(err)
// 	}
//
// 	fmt.Print(resp.Greeting)
// 	// Output: Hello, World
// }
Output:

Hello, World

func AsNoResponseOperation

func AsNoResponseOperation[Request any](f func(ctx context.Context, request Request) error) Operation[Request, any]

AsNoResponseOperation wraps a func (context.Context, request Request) error typed function as an operation useful for Delete like methods

func Compose

func Compose[Request any, Intermediate any, Response any](op1 Operation[Request, Intermediate], op2 Operation[Intermediate, Response]) Operation[Request, Response]

Compose can be used to chain two operations together (e.g. get something, then update it).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL