Giter Club home page Giter Club logo

box's Introduction

๐Ÿ“ฆ box

A lightweight wrapper making Go interfaces serialisable so they can be used as arguments for Temporal workflows and activities.

Usage

Suppose I have the following interface for a message request:

type MessageRequest interface {
    SendMessage() error
    ScheduleMessage(time.Time) error
}

With two concrete implementations:

type SlackMessageRequest struct {
    RecipientID string
    Content []byte
    ...
}

type EmailRequest struct {
    To string
    Subject string
    CC string
    Body string
    ...
}

As part of some Temporal workflow, I wish to send either kind of message using an activity. Ideally, I would want the activity to be type-agnostic w.r.t. the message request, and simply interact through the interface:

func (h *CommHandler) HandleMessageDelivery(ctx context.Context, msgRequest MessageRequest) error {
    ...
}

However, arguments to Temporal activities are serialised and de-serialised behind the scenes, and so that intuitive solution would not work: a concrete JSON embedding of one of the implementations of the interface cannot be directly de-serialised into an interface instance.

By slightly changing the activity code, Box solves this problem:

func (h *CommHandler) HandleMessageDelivery(ctx context.Context, msgRequest Box[MessageRequest]) error {
    err := msgRequest.Unbox(&SlackMessageRequest{}, &EmailRequest{})
    ...
    err = msgRequest.Data.SendMessage()
    ...
}

With Box, de-serialisisation of the representation of the struct implementing the interface MessageRequest is deferred until I explicitly Unbox it, providing concrete types that satisfy the interface and might be inside the Box.

After Unbox-ing, my activity can work with the interface as if it received it as an argument directly.

Creating a new Box

box, err := NewBox[MessageRequest](&SlackMessageRequest{
    ...
})

How a concrete type is chosen for Unbox-ing

Unbox iteratively attempts to de-serialise the raw JSON representation of the object inside the box into each of the concrete types given, stopping at the first success.

What objects can be put inside a Box

An object can be boxed if it satisfies the following interface:

type Boxable interface {
	Unbox(json.RawMessage) error
}

A straight-forward way to implement this interface is to delegate to the default json.Unmarshal function.

box's People

Contributors

natexcvi avatar

Stargazers

Shir Magen avatar  avatar DBI avatar  avatar Igor Kheifetz avatar Jeffrey Tang avatar Alon Gat avatar Moshe Beladev avatar  avatar Or Elimelech avatar Tal Benyunes avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.