Categorygithub.com/swaggest/usecase
modulepackage
1.3.1
Repository: https://github.com/swaggest/usecase.git
Documentation: pkg.go.dev

# README

Use Case Interactor

Build Status Coverage Status GoDevDoc Code lines Comments

This module defines generalized contract of Use Case Interactor to enable The Clean Architecture in Go application.

Clean Architecture

Why?

Isolating transport layer from business logic reduces coupling and allows better control on both transport and business sides. For example the application needs to consume AMQP events and act on them, with isolated use case interactor it is easy to trigger same action with HTTP message (as a part of developer tools).

Use case interactors declare their ports and may serve as a source of information for documentation automation.

This abstraction is intended for use with automated transport layer, for example see REST.

Usage

Input/Output Definitions

// Configure use case interactor in application layer.
type myInput struct {
    Param1 int    `path:"param1" description:"Parameter in resource path." multipleOf:"2"`
    Param2 string `json:"param2" description:"Parameter in resource body."`
}

type myOutput struct {
    Value1 int    `json:"value1"`
    Value2 string `json:"value2"`
}

Classic API

u := usecase.NewIOI(new(myInput), new(myOutput), func(ctx context.Context, input, output interface{}) error {
    var (
        in  = input.(*myInput)
        out = output.(*myOutput)
    )

    if in.Param1%2 != 0 {
        return status.InvalidArgument
    }

    // Do something to set output based on input.
    out.Value1 = in.Param1 + in.Param1
    out.Value2 = in.Param2 + in.Param2

    return nil
})

Generic API with type parameters

With go1.18 and later (or gotip) you can use simplified generic API instead of classic API based on interface{}.

u := usecase.NewInteractor(func(ctx context.Context, input myInput, output *myOutput) error {
    if in.Param1%2 != 0 {
        return status.InvalidArgument
    }

    // Do something to set output based on input.
    out.Value1 = in.Param1 + in.Param1
    out.Value2 = in.Param2 + in.Param2

    return nil
})

Further Configuration And Usage

// Additional properties can be configured for purposes of automated documentation.
u.SetTitle("Doubler")
u.SetDescription("Doubler doubles parameter values.")
u.SetTags("transformation")
u.SetExpectedErrors(status.InvalidArgument)
u.SetIsDeprecated(true)

Then use configured use case interactor with transport/documentation/etc adapter.

For example with REST router:

// Add use case handler to router.
r.Method(http.MethodPost, "/double/{param1}", nethttp.NewHandler(u))

# Packages

Package status defines a list of canonical statuses.

# Functions

As finds the first Interactor in Interactor's chain that matches target, and if so, sets target to that Interactor value and returns true.
NewInteractor creates generic use case interactor with input and output ports.
NewIOI creates use case interactor with input, output and interact action function.
Wrap decorates Interactor with Middlewares.

# Constants

ErrInvalidType is returned on port type assertion error.

# Structs

Error is an error with contextual information.
Info exposes information about use case.
IOInteractor is an interactor with input and output.
IOInteractorOf is an IOInteractor with parametrized input/output types.
OutputWithEmbeddedWriter implements streaming use case output.
OutputWithNoContent is embeddable structure to provide conditional output discard state.
WithInput is an embeddable implementation of HasInputPort.
WithOutput is an embeddable implementation of HasOutputPort.

# Interfaces

HasDescription declares description.
HasExpectedErrors declares errors that are expected to cause use case failure.
HasInputPort declares input port.
HasIsDeprecated declares status of deprecation.
HasName declares title.
HasOutputPort declares output port.
HasTags declares tags of use cases group.
HasTitle declares title.
Interactor orchestrates the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.
Middleware creates decorated use case interactor.
OutputWithWriter defines output with streaming writer.

# Type aliases

ErrorCatcher is a use case middleware that collects non empty errors.
Interact makes use case interactor from function.
MiddlewareFunc makes Middleware from function.